[SOLVED] Timer for Motion sensor

Good Morning

At first sorry for my bad english…

In my stairwell i have two sonoff touch which are triggering a relay for the lighting. This relay i can set from 1 up to x-minutes. After this time the lights / the relay going off.

Now I’ve bought myself a PIR motion sensor to trigger the relay too.

I need help for the rule because i need a special timer.

My Items

Switch UF_Eingang_Licht  "Licht Eingangsbereich" {mqtt=">[mosquitto:cmnd/EG/FL/LichtV/POWER3:command:*:default],<[mosquitto:tele/EG/FL/LichtV/STATE:state:JSONPATH($.POWER3)]"}
String Bridge_SZ {mqtt="<[mosquitto:tele/1OG/EZ/Bridge/RESULT:state:JSONPATH($.RfReceived.RfKey)]"}

The Switch give a 2ms impuls to the relay to turn it on for the time I setup. The problem is that I can give the next impuls only after 30sec ore more to reschedule the relay.

The String recieve the RFData from the Motion sensor

Now I’ll need a correct rule which will do the following

var val mtimer (timer in seconds for the motion detector)

rule Bewegung
when Bridge_SZ change (this happens every second when motion is in the stairwell)
then
 check state of mtimer
  when 
   mtimer is  0 (the relay is off) or mtimer is >30sec (the last triggering of the relay is more then 30 seconds past)
  then
   sendCommand(UF_Eingang_Licht, "ON") (send an impuls to turn on the light or reschedule the relay)
   reschedule mtimer (to start the minimum 30 seconds again)
 when
  mtimer is < 30sec do nothing and leave the rule (the lights are still on, waiting for the next motion update)

Thank you for your help

With kind regards
Daniel

Perhaps you could find some inspiration in this post. It is for a similar scenario.

Hi noppes123

The problem is that I haven’t a switch for the motion detector.

var Timer timer = null

rule "Flur Motion Sensor ON" 
when 
    Item Flur_MotionSensorPresence changed from OFF to ON
then
    if(timer === null || timer.hasTerminated) {
        timer.cancel
        timer = null
        FlurSchalter.sendCommand(ON)
        logInfo("Motion Flur", "Bewegung erkannt -> Licht an")
    }
    else {
        timer.cancel
        timer = null
        logInfo("Motion Flur", "Bewegung erkannt -> Timer gelöscht")
    }
end

rule "Flur Motion Sensor OFF"
when
    Item Flur_MotionSensorPresence changed from ON to OFF
then
    logInfo("Motion Flur", "Keine Bewegung mehr erkannt -> Timer gestartet")
    timer = createTimer(now.plusSeconds(10), [ |
        timer.cancel
        timer = null
        FlurSchalter.sendCommand(OFF)
        logInfo("Motion Flur", "Timer abgelaufen -> Licht aus")
    ])
end

In my case the motion detector send RFData to Sonoff RFBridge, like many other RFDevices too.
When the Bridge recieved the data from the sensor I emulate the data to a RFKey in my case RFKey 3.
Furthermore with the sent 2ms Impuls the relay turn on for example 1min and turn off automatically.

var val mtimer (timer in seconds for the motion detector)

rule ChangeRFKeySZ //Fernbedienung SZ
when
    Item Bridge_SZ changed
 then
    switch (Bridge_SZ.state) {
        case 1 : publish("mosquitto", "cmnd/2OG/JZ/Rolladen/POWER1", "TOGGLE") //FB JZ
        case 2 : publish("mosquitto", "cmnd/2OG/JZ/Rolladen/POWER2", "TOGGLE") //FB JZ
        case 3: { check state of mtimer
                        when 
                         mtimer is  0 (the relay is off) or mtimer is >30sec (the last triggering of the relay is more 
                         then 30 seconds past)
                        then
                         sendCommand(UF_Eingang_Licht, "ON") (send an impuls to turn on the light or reschedule the relay)
                         reschedule mtimer (to start the minimum 30 seconds again)
                        when
                         mtimer is < 30sec do nothing and leave the rule (the lights are still on, waiting for the next motion update)}
    }
    Bridge_SZ.postUpdate(0)
end

That means if there is a motion detected the RFKey changed to 3 every time. And every time it must been verifying if the light / relay is OFF or ON, and when the relay is on if the last triggering is 30 seconds past.

OK I tried a little bit and now it works fine

val Timer mtimer = null

rule ChangeRFKeySZ //Fernbedienung SZ
when
    Item Bridge_SZ changed
 then
    switch (Bridge_SZ.state) {
        case 1 : publish("mosquitto", "cmnd/2OG/JZ/Rolladen/POWER1", "TOGGLE") //FB JZ
        case 2 : publish("mosquitto", "cmnd/2OG/JZ/Rolladen/POWER2", "TOGGLE") //FB JZ
        case 3 : {
             if(mtimer === null || mtimer.hasTerminated) {
                publish("mosquitto", "cmnd/1OG/EZ/LampeEss/POWER", "TOGGLE")
                logInfo("Motion Flur", "Bewegung erkannt -> Licht an")
                mtimer = createTimer(now.plusSeconds(30), [ |
                 mtimer.cancel
                 mtimer = null
                logInfo("Motion Flur", "Timer abgelaufen -> Neuauslösung möglich")
                ])
                }
            }
    }
    Bridge_SZ.postUpdate(0)
end

The rule starts only when mtimer is 0 and turn the light on. It also start the timer for 30sec and when the time is over the timer is set to 0 again.

Thanks noppes123 for your hint.

With kind regards

Daniel

1 Like

If I understand you correctly this time :grin:, the update of the RFKey to ‘3’ is a motion detected signal. And there is no need for sending an OFF command because the Sonoff device (or the Solid State Relay?) has a built-in timer to turn it off. Right?

Just a wild idea:
You could create a ‘virtual switch’ Item that switches to ON when ‘3’ is received, which expires to OFF 30 seconds after the last ON command is received, like this:

Switch MotionDetected {expire="30s,command=OFF"}

And a few very simple rules to deal with switch state changes and initialization:

rule "System start initialization"
when
    System started
then
    if (MotionDetected.state == NULL)
        MotionDetected.postUpdate(OFF) //This does not trigger the OFF-rule
end
//Happens when RFKey '3' is received after >30s
rule 'Fernbedienung SZ ON'
when
    Item MotionDetected changed from OFF to ON
then
    UF_Eingang_Licht.sendCommand(ON)
end
// Only triggered after 30s time-out of MotionDetected Switch
rule 'Fernbedienung SZ OFF'
when
    Item MotionDetected changed from ON to OFF
then
    UF_Eingang_Licht.sendCommand(OFF)
end

This should work based on the fact that only a change from OFF to ON triggers the ‘ON-rule’, not any consecutive ON’s. I haven’t tested it, though…

BTW, the MotionDetected Switch could possibly be set directly with the Item definition by transforming the received status ‘3’ into ‘ON’, without the need for an intermediate virtual switch.