Page 1 of 1

Time triggers and hard-coding times

Posted: Sunday 05 April 2020 13:18
by pmniemiDomFor
Hi,

Say you have a dzVents script for switching lights and you have several triggers for both switching them on and switching off. In the execution part of the script, how do you know which trigger was actually triggered? Do you have to do a manual check with hard-coded values that was it the trigger e.g. for switchig the lights off at 09:00?

For now I have done like this:

Code: Select all

...
		timer = {
			'at sunrise',
			'at 05:30',
		}
...
	execute = function(domoticz, triggeredItem, info)
	    local gardenLights = domoticz.devices(122)
	    local Time = require('Time')
	    local currentTime = Time()
...
		
...
    	    if (currentTime.matchesRule('at 05:30')) then
    	        gardenLights.switchOn()
    	    elseif (currentTime.matchesRule('at sunrise')) then
    	        gardenLights.switchOff()
    	    end
end
It feels like bad coding having to hard-code the trigger values and do a lot of checking in the script. There surely must be a better pattern? Can you e.g. have variables and use them both as trigger values and in the script?

Regards,

Peik

Re: Time triggers and hard-coding times

Posted: Sunday 05 April 2020 15:00
by waaren
pmniemiDomFor wrote: Sunday 05 April 2020 13:18
It feels like bad coding having to hard-code the trigger values and do a lot of checking in the script. There surely must be a better pattern? Can you e.g. have variables and use them both as trigger values and in the script?

Code: Select all

local  gardenLightsOn = 'at sunrise'
local  gardenLightsOff = 'at 05:30'

...
		timer = 
		{
			gardenLightsOn, 
			gardenLightsOff, 
		},
...
	execute = function(dz)
	    local gardenLights = domoticz.devices(122)

		if dz.time.matchesRule(gardenLightsOn) then
			gardenLights.switchOn()
		else
			gardenLights.switchOff()
		end
end

Re: Time triggers and hard-coding times

Posted: Sunday 05 April 2020 21:34
by pmniemiDomFor
Oh, you can do like that? I thought the "locals" must be in the execute function!

Thanks for your advice.

-Peik

Re: Time triggers and hard-coding times

Posted: Tuesday 07 April 2020 6:53
by amaiorov41
Hello! I work on a moving chart, trying to find a solution how to activate the scene on a timer every 4 days, but I get a global Domoticz error, can anyone help?)

Re: Time triggers and hard-coding times

Posted: Tuesday 07 April 2020 7:06
by waaren
amaiorov41 wrote: Tuesday 07 April 2020 6:53 Hello! I work on a moving chart, trying to find a solution how to activate the scene on a timer every 4 days, but I get a global Domoticz error, can anyone help?)
What do you mean with "a global domoticz error" ?
Check this topic for a dzVents solution to trigger every nth day.

Re: Time triggers and hard-coding times

Posted: Tuesday 07 April 2020 7:13
by amaiorov41
Thanks, I'll try