Time triggers and hard-coding times

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

Moderator: leecollings

Post Reply
pmniemiDomFor
Posts: 8
Joined: Sunday 05 April 2020 12:39
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Contact:

Time triggers and hard-coding times

Post 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
Domoticz 2020.1, Raspberry Pi, Buster, Z-wave, Aeotec Z-stick GEN5
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Time triggers and hard-coding times

Post 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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
pmniemiDomFor
Posts: 8
Joined: Sunday 05 April 2020 12:39
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Contact:

Re: Time triggers and hard-coding times

Post by pmniemiDomFor »

Oh, you can do like that? I thought the "locals" must be in the execute function!

Thanks for your advice.

-Peik
Domoticz 2020.1, Raspberry Pi, Buster, Z-wave, Aeotec Z-stick GEN5
amaiorov41
Posts: 2
Joined: Saturday 09 March 2019 3:34
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Time triggers and hard-coding times

Post 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?)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Time triggers and hard-coding times

Post 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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
amaiorov41
Posts: 2
Joined: Saturday 09 March 2019 3:34
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Time triggers and hard-coding times

Post by amaiorov41 »

Thanks, I'll try
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest