Page 1 of 1

Timer only when device is on

Posted: Saturday 12 November 2022 17:05
by Cyroq
Hi, is it possible to only run a timer script when a specific device is on? Of course I could run a timer script and put everything in an if-statement like

Code: Select all

if (dz.devices('devicename') == 'On') then
...
end
but that floods my log file big time if the timer is run every minute 24/7. The script will in reality only be active 10 minutes a day, so it runs 10 times. It is triggered by a (dummy) device.

Re: Timer only when device is on

Posted: Saturday 12 November 2022 18:06
by mgugu

Re: Timer only when device is on

Posted: Saturday 12 November 2022 19:32
by boum
You can also change your script to trigger only on your dummy device and when triggered to On, execute a buncho of dz.emitEvent('customname', {...}).afterMin(X).
You can have another script with customEvents trigger. Or do that in your device script too.

You can also execute a single custom event in the first script, and in the second one, trigger it again as long as some condition is met.

Re: Timer only when device is on

Posted: Sunday 13 November 2022 23:47
by Neutrino
I'm using this :

Code: Select all

return {
	active = function(domoticz)
		return (domoticz.devices('devicename').state == 'On')
	end,
	--active = true,
	on = {
		timer = {'every minute'},
	},

Re: Timer only when device is on

Posted: Monday 14 November 2022 20:02
by Cyroq
Thanks @Neutrino, that's a neat solution! Works perfectly.
@boum I'm hesitant using AfterMin() because it's complex to cancel the operation mid way, if I'd want that. But I didn't know about the customEvents trigger, might come in handy later 8-)