Page 1 of 1

Only trigger when state is changed

Posted: Tuesday 16 October 2018 21:28
by denman091
Hello,

I'm very new to DzVents, i have read the wiki.
Now i'm trying to set up a script to turn on my led strip in the kitchen.

Sow; I have a motion sensor (Motion Kitchen), who sends every 10 seconds an "on" command as long as there is motion. But it does not send a "off" command when there is no motion. So I set up the "OffDelay" in de device settings. So when there is no motion the "Motion Sensor" device goes "Off" after 30 seconds.
What i want is: When "Motion Kitchen" switch from "Off" to "On" the led strip (Kitchen Led) turnt "On". When "Motion Kitchen" Switch to "Off" the "Kitchen Led" turns off after 60 seconds.
But what i see in the log is that the event is triggerd every 10 seconds when the Motion sensor sends a "On" command. This is unnecessary in my opinion. Is it possible to only trigger if the state of the Motion Kitchen changes from "Off" to "On"?

Now i have the following code:

Code: Select all

 return {
   on = {
      devices = {
         'Motion Kitchen'
      }
   },
   execute = function(domoticz, switch)
      if (switch.state == 'On') then
         domoticz.devices('Kitchen Led').switchOn()
         domoticz.log('Beweging in de keuken. Led AAN')
      else
         domoticz.devices('Kitchen Led').switchOff().afterMin(1)
         domoticz.log('Geen beweging in de keuken. Led UIT over 1 minuut')
      end
   end
}

Re: Only trigger when state is changed

Posted: Wednesday 17 October 2018 6:56
by dannybloe
Well, dzVents gets the trigger from Domoticz. Every time your sensor sends an On to Domoticz this script is triggered due to the on-rule. So the only thing you can do is to check for the this in your script (or get a better motion detector that only sends state changes).

Re: Only trigger when state is changed

Posted: Wednesday 17 October 2018 7:07
by waaren
denman091 wrote: Tuesday 16 October 2018 21:28 ... I have a motion sensor (Motion Kitchen), who sends every 10 seconds an "on" command as long as there is motion. But it does not send a "off" command when there is no motion. So I set up the "OffDelay" in de device settings. So when there is no motion the "Motion Sensor" device goes "Off" after 30 seconds.
What i want is: When "Motion Kitchen" switch from "Off" to "On" the led strip (Kitchen Led) turnt "On". When "Motion Kitchen" Switch to "Off" the "Kitchen Led" turns off after 60 seconds.
But what i see in the log is that the event is triggerd every 10 seconds when the Motion sensor sends a "On" command. This is unnecessary in my opinion. Is it possible to only trigger if the state of the Motion Kitchen changes from "Off" to "On"?
It depends a bit on what you consider as "triggered". dzVents offers the possibility to attach a function to the active = section. See the wiki subject on the active = section In this "active =" section you can write some logic to check if your script should be executed now or not. So in theory it is possible but effectively you are writing new code doing the same as what can be done in the execute = section. I my opinion for this case this is not efficient nor transparant.
What you can do here tough is add the method/function checkFirst() to the switchOn() part like

Code: Select all

domoticz.devices('Kitchen Led').switchOn().checkFirst()
causing domoticz only to send the on signal to the device when needed. If you want to get rid of the logline and prevent the Led to go off even after someone re-entered the kitchen then code this part like.

Code: Select all

if (switch.state == 'On') then
        if domoticz.devices('Kitchen Led').state ~= "On" then
            domoticz.devices('Kitchen Led').cancelQueuedCommands()  -- Remove the schedule set by .switchOff().afterMin(1) part
            domoticz.devices('Kitchen Led').switchOn()
            domoticz.log('Beweging in de keuken. Led AAN')
        end
else

Re: Only trigger when state is changed

Posted: Wednesday 17 October 2018 7:14
by dannybloe
But the script will still be executed whenever there's an On sent from the motion detector to Domoticz.

On the other hand, you can also just do this every time an on state occurs: domoticz.devices('Kitchen Led').forMin(1) and skip the off part (and the checking). Each time the on triggers the timer will restart counting for 1 minute.

Only problem with these kind of scripts is that you can never switch off or on the light manually. I have made some scripts that specifically stores the actor of the light (who or what turned it on) and if the light was turned on using the switch then the motion events are ignored. But that's a different story :-)