Use variable in the trigger section

Post Reply
pcdiks
Posts: 2
Joined: Sunday 24 January 2016 12:35
Target OS: Raspberry Pi
Domoticz version:
Contact:

Use variable in the trigger section

Post by pcdiks » Friday 24 November 2017 12:13

I have a uservariable that contains the time I have to get up in the morning. This user variable is updated from my Android tablet that I use to wake me in the morning. The updating of the user variable is working.
This user variable (Alarm) contains the following (example): 'vr 06:05'

My goal is to use a dzVents LUA to turn a switch on, I created the following script and I want to know if the timer part will work like this:

Code: Select all

function split(s, delimiter) 
    result = {};
    for match in (s..delimiter):gmatch("(.-)"..delimiter) do
        table.insert(result, match);
    end
    return result;
end

AlarmTotal = uservariables['Alarm']
rslt = split(AlarmTotal, " ")
TempDag = rslt[1]
Tijd = rslt[2]

if TempDag == 'ma' then Dag = "1" end
if TempDag == 'di' then Dag = "2" end
if TempDag == 'wo' then Dag = "3" end
if TempDag == 'do' then Dag = "4" end
if TempDag == 'vr' then Dag = "5" end

currenttime = tostring(os.date("%H:%M"))
currentday = tostring(os.date("%w"))

--print('Alarm Dag:    '..Dag)
--print('Alarm Tijd:   '..Tijd)
--print('Huidige Tijd: '..currenttime)
--print('Huidige Day:  '..currentday)

return {
    
	on = {
		timer = {
			'at '..Tijd..' on '..Dag,
		}
	},
	execute = function(domoticz, device)
		domoticz.log('Turn on switch', domoticz.LOG_INFO)
		domoticz.device('Alarm Trigger').SwitchOn().forMin(5)
	end
}

dannybloe
Posts: 1078
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi
Domoticz version:
Location: Ermelo
Contact:

Re: Use variable in the trigger section

Post by dannybloe » Friday 24 November 2017 18:06

Interesting! It's a combination of the old Lua with dzVents. It might work but I won't garantuee it will in the future. I would do it differently. Either use a timer function:

Code: Select all

return  {
	on = timer = {
		function(domoticz)
			-- use domoticz.variables(..) here and return true when the timer should go off
	        end
        },
        execute = function(domoticz, timer, triggerInfo)
        
        end
}
or
simple create a every minute timer and check your variable in the code.

Another problem with your code is that when a non-timer event occurs (temperature gets updated, switch etc) your code to parse the uservariable will be executed and takes up valuable cpu cycles. So your code is executed every time the module is loaded by dzVents (that is for every event!!). If you move your code to the timer function or inside your execute (if statement) it will only be executed when a timer event occurs. And you might think: but I have a timer created with a specific time stamp (at hh:mm), shouldn't that limit the amount of times the code is evaluated? The answer is: no. All timer rules are evaluated every minute. dzVents processes the rules and checks if it should run the execute function. But that checking will be done every minute.

And again, don't use any of the 'old' Lua tables as they may be unavailable someday inside dzVents context (to save memory/speed).
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

pcdiks
Posts: 2
Joined: Sunday 24 January 2016 12:35
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Use variable in the trigger section

Post by pcdiks » Saturday 25 November 2017 12:59

Thanks for the tips. I change my script and it does not use tables anymore. Because the value of my uservariable is always in the same format, I'm using the string.sub function for it. It should be a lot easier if Tasker on my Android tablet would be able to use the 'Alarm Done' event, but that does not work on all android devices and mine is one of them...

For reference, here is my script:

Code: Select all

return {

	active = {

		true,  -- either true or false, or you can specify a function
	},
	
	on = {

		-- timer riggers
		timer = {
			-- timer triggers.. if one matches with the current time then the script is executed
			function(domoticz)
                local AlarmTotal = domoticz.variables('Alarm').value
                local TempDag = string.sub(AlarmTotal,1,2)
                local Tijd = string.sub(AlarmTotal,4)
                
                if TempDag == 'ma' then Dag = "1" end
                if TempDag == 'di' then Dag = "2" end
                if TempDag == 'wo' then Dag = "3" end
                if TempDag == 'do' then Dag = "4" end
                if TempDag == 'vr' then Dag = "5" end
                if TempDag == 'za' then Dag = "6" end
                if TempDag == 'zo' then Dag = "7" end

                local currenttime = tostring(os.date("%H:%M"))
                local currentday = tostring(os.date("%w"))
                
                --domoticz.log('Alarm Dag:    '..Dag)
                --domoticz.log('Alarm Tijd:   '..Tijd)
                --domoticz.log('Huidige Tijd: '..currenttime)
                --domoticz.log('Huidige Day:  '..currentday)
                
                if Dag == currentday then
                    domoticz.log('Juiste dag')
                    if Tijd == currenttime then
                        domoticz.log('Juiste dag en tijd, zet de schakelaar aan')
                        return true
                    else
                        domoticz.log('Niet de juiste tijd')
                        return false
                    end
                else
                    domoticz.log('Niet de juiste dag')
                    return false    
                end
			end
		}
	},

	-- custom logging level for this script
	logging = {
        level = domoticz.LOG_DEBUG,
        marker = "Alarm script"
    },

	-- actual event code
	-- the second parameter is depending on the trigger
	-- when it is a device change, the second parameter is the device object
	-- similir for variables, scenes and groups
	-- for timer and security events, the second parameter is nil
	execute = function(domoticz, info)

		domoticz.log('Alarm gaat af, zet dummy switch aan...')
		domoticz.devices('Alarm Trigger').switchOn().forMin(1)
	end
}

Post Reply

Who is online

Users browsing this forum: aurban6 and 2 guests