Only trigger when state is changed

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

Moderator: leecollings

Post Reply
denman091
Posts: 30
Joined: Monday 31 July 2017 15:22
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: Pijnacker
Contact:

Only trigger when state is changed

Post 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
}
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Only trigger when state is changed

Post 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).
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Only trigger when state is changed

Post 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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Only trigger when state is changed

Post 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 :-)
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Post Reply

Who is online

Users browsing this forum: Amazon [Bot] and 1 guest