Page 1 of 1

property of active timer(name)

Posted: Wednesday 10 April 2019 20:12
by pvklink
Does dzvents has a property for the active timer, by example: Tuinirrigatie_gazon instead of the value of this timer

I like to shorten my script, by replacing all the IF ELSIF END by:
something like dz.devices(item.name).switchOn().checkFirst()
item.name must be: ' Tuinirrigatie_gazon' for example

Code: Select all

--local Tuinirrigatie_border_links        = 'at 19:00 on mon,tue,wed,thu,fri,sat,sun'
local Tuinirrigatie_border_rechts       = 'at 19:15 on mon,tue,wed,thu,fri,sat,sun'
local Tuinirrigatie_gazon               = 'at 20:05 on mon,tue,wed,thu,fri,sat,sun'
--local Tuinirrigatie_planten            = 'at 19:45 on mon,tue,wed,thu,fri,sat,sun'

return {
	on =    {timer = {Tuinirrigatie_border_links,Tuinirrigatie_border_rechts,Tuinirrigatie_gazon,Tuinirrigatie_planten}},
        
    logging =   { level   = domoticz.LOG_DEBUG ,                  
                  marker  = "Timers irrigatie"},
              
    execute = function(dz,item,info)

    if (item.trigger == Tuinirrigatie_border_links) then
        dz.devices('Tuinirrigatie_border_links').switchOn().checkFirst()  
    elseif (item.trigger == Tuinirrigatie_border_rechts) then
        dz.devices('Tuinirrigatie_border_rechts').switchOn().checkFirst()  
    elseif (item.trigger == Tuinirrigatie_gazon) then
        dz.devices('Tuinirrigatie_gazon').switchOn().checkFirst()  
    elseif (item.trigger == Tuinirrigatie_planten) then
        dz.devices('Tuinirrigatie_planten').switchOn().checkFirst()  
    end        

    dz.log("Script: " .. info.scriptName .. " is aangezet ", dz.LOG_INFO)

end
}

Re: property of active timer(name)

Posted: Thursday 11 April 2019 9:15
by waaren
pvklink wrote: Wednesday 10 April 2019 20:12 Does dzvents has a property for the active timer, by example: Tuinirrigatie_gazon instead of the value of this timer
No, the timer is a type String value. In your example contained in a local user variable.

but.. if you declare this user variable without preceding it with 'local ' it becomes a global user variable and as such part of the '_G' Global table.
Using this Global table you can access the variable names and contents.

This is not considered good programming for more then one reason but it can be done. See below script for an example.

Code: Select all

  
Tuinirrigatie_border_links    = 'at 09:02 on mon,tue,wed,thu,fri,sat,sun'
Tuinirrigatie_border_rechts   = 'at 09:03 on mon,tue,wed,thu,fri,sat,sun'
Tuinirrigatie_gazon           = 'at 08:56 on mon,tue,wed,fri,sat,sun'
Tuinirrigatie_planten         = 'at 08:58'

return {	
			on =    {
						timer = {
									Tuinirrigatie_border_links,
									Tuinirrigatie_border_rechts,
									Tuinirrigatie_gazon,
									Tuinirrigatie_planten
								}
					},
        
    logging =   { level   = domoticz.LOG_DEBUG ,                  
                  marker  = "Timers irrigatie"},
              
    execute = function(dz, item)
		
        local function getActiveTimerVarName()
            local counterG, counterF = 0,0
            for k,v in pairs(_G) do
                counterG = counterG + 1
                if tostring(v):find("at ") == 1 then 
                    counterF = counterF + 1
                    if dz.time.matchesRule(v) then
                        dz.log("Variable " .. k .. " is used as time trigger",dz.LOG_DEBUG)
                        dz.log(counterG .. " entries in _G considered",dz.LOG_DEBUG)
                        dz.log(counterF .. " vars starting with 'at ' found",dz.LOG_DEBUG)    
                        return k
                    end
                end
            end
            
        end 
        
        dz.devices(getActiveTimerVarName()).switchOn()
    end
}

Re: property of active timer(name)

Posted: Thursday 11 April 2019 9:49
by pvklink
Thanks, still learning!
After all your help, you have to see how all my devices(cams, alarms, x10, rfx, zwave, smoke/watersensors, switches, audio, chromecast, google home, weather comes together with dashtics.