Page 1 of 1

Timer trigger depending on device state. [SOLVED]

Posted: Sunday 14 July 2019 7:27
by darkdude
I need to make an event where if a certain device is on for at least 15 consecutive minutes another device will be triggered. I couldn't find a way to do it with just devices. I made a timer which runs every minute and if device 1 is still on, +1 is added to a counter. If it's off the counter resets.

Code: Select all

return {

active = true,

	on = { 
	      timer = {'every 1 minutes'}
	    },
	   
	data = {
	    counter = { initial = 0 }
	    },
       

	execute = function(domoticz, _)

   if (domoticz.devices("POMPA ZBIORNIKA").state == "On" and domoticz.devices("HISTEREZA_NAPELNIANIA").state == "On" and domoticz.data.counter <15) then
       
       domoticz.data.counter = domoticz.data.counter + 1
        domoticz.log (domoticz.data.counter)
       
   elseif (domoticz.devices("POMPA ZBIORNIKA").state ~= "On" or domoticz.devices("HISTEREZA_NAPELNIANIA").state ~= "On") then 
       
        domoticz.data.counter = 0 
         
          domoticz.log (domoticz.data.counter)
        
    elseif  (domoticz.data.counter == 15) then 
        
        domoticz.devices("HISTEREZA_NAPELNIANIA").switchOff().checkFirst()
          domoticz.log (domoticz.data.counter)
       
	    end
	end
}

This one works (the probing resolution is really bad, but it's enough in this case). However this whole event may occur only a few times a year. If possible I don't want a script to run every minute for a whole year just for this.

Is it possible to activate this script only if a switch.state == "On" (not on a change of state)?

Cheers!

Re: Timer trigger depending on device state.

Posted: Sunday 14 July 2019 8:12
by waaren
darkdude wrote: Sunday 14 July 2019 7:27 I need to make an event where if a certain device is on for at least 15 consecutive minutes another device will be triggered. I couldn't find a way to do it with just devices. I made a timer which runs every minute and if device 1 is still on, +1 is added to a counter. If it's off the counter resets.

Is it possible to activate this script only if a switch.state == "On" (not on a change of state)?
Try this.

Code: Select all

return {
    active = function(domoticz)
         return domoticz.devices('your switch').active
    end,
    
    on = { 
          timer = {'every 1 minutes'}
        },
       
    data = {
        counter = { initial = 0 }
        },
       

    execute = function(domoticz, _)

   if (domoticz.devices("POMPA ZBIORNIKA").state == "On" and domoticz.devices("HISTEREZA_NAPELNIANIA").state == "On" and domoticz.data.counter <15) then
       
       domoticz.data.counter = domoticz.data.counter + 1
        domoticz.log (domoticz.data.counter)
       
   elseif (domoticz.devices("POMPA ZBIORNIKA").state ~= "On" or domoticz.devices("HISTEREZA_NAPELNIANIA").state ~= "On") then 
       
        domoticz.data.counter = 0 
         
          domoticz.log (domoticz.data.counter)
        
    elseif  (domoticz.data.counter == 15) then 
        
        domoticz.devices("HISTEREZA_NAPELNIANIA").switchOff().checkFirst()
          domoticz.log (domoticz.data.counter)
       
        end
    end
}
            

Re: Timer trigger depending on device state. [SOLVED]

Posted: Sunday 14 July 2019 8:43
by darkdude
Yep, that does it.

Thanks!