Best practice to perform an action after 'lastUpdate' > x minutes

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
jake
Posts: 751
Joined: Saturday 30 May 2015 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Best practice to perform an action after 'lastUpdate' > x minutes

Post by jake »

I would like to turn 'Off' my receiver after Kodi is 'On' (= sitting idle) for 5 minutes. In 'good old LUA' I had a lastUpdate code in my device type of LUA file. To my surprise this always worked, although I read that Domoticz would need a trigger to do the comparison how much time has passed since last change.

Anyway, what would be best practice to do that in dzVents. I don't feel leek 24h/day checking the script every minute.
before switching the receiver 'Off', I always do some checks before I make the decision, so if not all checks are OK, I update a variable, but don't switch 'Off' the receiver. Therefore a switchOff().after_minutes(5) won't work, because I don't think such a command can be cancelled before actuation of it, right?
jake
Posts: 751
Joined: Saturday 30 May 2015 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: Best practice to perform an action after 'lastUpdate' > x minutes

Post by jake »

Anybody with a solution how to trigger a script in dzVents, 5 minutes after the lastupdate of a device?

I am wondering myself that it must be possible since every normal switch can have a delayed 'on' or 'off' without any extra coding. Domoticz therefore is able to do this check internally.

Although I don't like to have this script to bog down Domoticz every minute of the day, I did the following for a trial of my own code:

I added 'timer' to the dzVents script. The script still runs fine now, but as guessed, it gets triggered every single minute without doing any good.

Script:

Code: Select all

local BDname = "BD/DVD"
local BDlevel = 10 --Selector level to switch Onkyo input to BD/DVD
local PClevel = 50 --Selector level to switch Onkyo input to PC

return {
	active = true,                  -- set to true to activate this script 
	on = {
		'Kodi Woonkamer - Status',
		'timer',
	},

	
	execute = function(domoticz) --,kodidevice)
		local kodidevice = domoticz.devices['Kodi Woonkamer - Status']
		local kodivariable_status = domoticz.variables['KodiStatus']
		local onkyosource = domoticz.devices['Onkyo AV Receiver TX-NR535 Source']
		local onkyopower = domoticz.devices['Onkyo AV Receiver TX-NR535 Power']
		
		if (kodidevice.state == 'Audio' or kodidevice.state == 'Video') then -- and onkyosource.state ~= 'On') then 
			if (onkyopower.state ~= 'On' and onkyosource.state ~= BDname) then 
				onkyosource.switchSelector(BDlevel)
			end
			kodivariable_status.set(kodidevice.state)
		elseif (kodidevice.state == 'On' and kodidevice.lastUpdate.minutesAgo >=1 and kodivariable_status.state ~= 'Off') then
			if onkyosource.state == BDname then
				onkyosource.switchSelector(PClevel)
				onkyopower.switchOff().after_sec(10)
			end

			kodivariable_status.set("Off")
		end
	end
}
BakSeeDaa
Posts: 485
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi / ODroid
Domoticz version:

Re: Best practice to perform an action after 'lastUpdate' > x minutes

Post by BakSeeDaa »

jake wrote:I added 'timer' to the dzVents script. The script still runs fine now, but as guessed, it gets triggered every single minute without doing any good.
Before migrating to dzVents I always used a dummy switch device with a timer to make my scripts trigger. I had very few timer scripts. My reason for doing that was that I wanted to avoid unnecessary processing. Anyway, I ended up with a lot of timer dummy switch devices which put additional burden on my Raspberry pi 3.

Now when I am half way into the dzVents migration I tend to use more scripts triggered by timer events and I delete my old dummy timer switches. I haven't noted that the script execution has slowed down and I appreciate the way my scripts have been simplified since I started using dzVents.

The time Domoticz use for one of my scripts varies but a simple dzVents script may take 4/1000 secs to process on my Raspberry Pi 3 (Judging from the logs, looking at the difference in time when one scripts starts and the next one starts). If there ever is a delay in my system for a few seconds it's rather caused by Z-wave communication or such.

I always have performance in mind when writing my script but readability and simplicity comes first.

I'd like to quote Donald Knuth:
The real problem is that programmers have spent far too much time worrying about efficiency in the wrong places and at the wrong times; premature optimization is the root of all evil (or at least most of it) in programming
jake
Posts: 751
Joined: Saturday 30 May 2015 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: Best practice to perform an action after 'lastUpdate' > x minutes

Post by jake »

Thanks for your story. Who knows that all scripts are checked once a minute anyhow, than a small script, that doesn't need to be triggered is processed rather quickly. 1 Downside is that the log will be completely spammed with the dzVents logs every minutes as well, I just checked it and it indeed shows 6 lines every time.
Another downside is that the timer is very in accurate: 5 minutes can be anything between 5:01 and 5:59, depending where 'in the minute' the lastupdate was done.
Since the dummy switches can be used perfectly as timers, we know that Domoticz is capable of doing it, it is just a pity that timers are not available in coding.
I will put a request in the general dzVents thread asking if the 'timer' trigger can be made dependable, like 'timer' if xyz
BakSeeDaa
Posts: 485
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi / ODroid
Domoticz version:

Re: Best practice to perform an action after 'lastUpdate' > x minutes

Post by BakSeeDaa »

jake wrote:1 Downside is that the log will be completely spammed with the dzVents logs every minutes as well, I just checked it and it indeed shows 6 lines every time.
You may chose a default logging level for dzVents in dzVents_settings.lua

Code: Select all

	Log level = 1: Errors
	Log level = 1.5: Errors + info about the execution of individual scripts and a dump of the commands sent back to Domoticz
	Log level = 2: Errors + info
	Log level = 3: Debug info + Errors + Info (can be a lot!!!)
	Log level = 0: As silent as possible
While You are working with a script, You can make all log entries for that script show up by setting the log level to domoticz.LOG_ERROR

Code: Select all

return {
	active = true,
	on = {
		'My switch'
	},

	execute = function(domoticz, mySwitch)
		local LOG_LEVEL = domoticz.LOG_ERROR -- Script default log level. You may change this.
	
		if (mySwitch.state == 'On') then
			domoticz.log('Hey!, I am on!', LOG_LEVEL)
		end
		domoticz.log('Another log entry', LOG_LEVEL)

	end
}
Then, when You have finished dubugging that script you simply change

Code: Select all

		local LOG_LEVEL = domoticz.LOG_ERROR -- Script default log level. You may change this.
to

Code: Select all

		local LOG_LEVEL = domoticz.LOG_INFO -- Script default log level. You may change this.
EDIT: It would be very nice I believe to be able to override (per script level) the default logging level that You have set in dzVents_settings.lua :D
jake
Posts: 751
Joined: Saturday 30 May 2015 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: Best practice to perform an action after 'lastUpdate' > x minutes

Post by jake »

Thanks for the additional info, I didn't know that!
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Best practice to perform an action after 'lastUpdate' > x minutes

Post by dannybloe »

BakSeeDaa wrote: EDIT: It would be very nice I believe to be able to override (per script level) the default logging level that You have set in dzVents_settings.lua :D
Maybe you can put this in the issue tracker in github. I don't think this is hard to do.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
BakSeeDaa
Posts: 485
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi / ODroid
Domoticz version:

Re: Best practice to perform an action after 'lastUpdate' > x minutes

Post by BakSeeDaa »

dannybloe wrote:
BakSeeDaa wrote: EDIT: It would be very nice I believe to be able to override (per script level) the default logging level that You have set in dzVents_settings.lua :D
Maybe you can put this in the issue tracker in github. I don't think this is hard to do.
Sure I will! :D
jake
Posts: 751
Joined: Saturday 30 May 2015 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: Best practice to perform an action after 'lastUpdate' > x minutes

Post by jake »

I finally started playing with that 'log level thing'. My settings were on '2', therefore displaying a lot of info every time. That's done.
Secondly, I first thought that specifying the LOG_LEVEL in a script would change the log level for that specific script. Obviously not. Do I understand it correctly that by specifying the log level in the domoticz.log(message,LOG_LEVEL) it will only show in the log when the actual log level is matching LOG_LEVEL, no bigger or smaller number?
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest