Motion detection getting me crazy

Hi all,
I have a problem that starts to get really annoying and eventually getting me crazy - and I shouldn’t mention my wife’s annoyance…lol.

I have a motion detector that is getting triggered correctly and activate the “GF Bathroom NightLight” lamp by the rule “GF Bathroom Motion ON”
The “GF_Bathroom_NightLight” turns on almost immediately by the rule.
The problem consists by irregular shutdowns during the timer time of 10 Minutes, the light switches on, off, on, off and so on.
The motion detector is also sending an off command, but is not directly connected to the lamp so that shouldn’t be an issue, the only thing that activates the “GF_Bathroom_NightLight” is the rule “GF_Bathroom_Motion ON”. I have altered the code for this rule for months in a variety of iterations without any luck.
One thing that I’m curious off is the strange behavior for powering lamps on/off by sending out 3 commands where the first is “shut off” the second “turn on” and the third “shut off” , the same goes for turning a lamp on but then it starts with on-off-on, why is the system reversing the second command?

If you take a look in the log, you can see that the lamp activates, but shuts down almost immediately.

So if any one of you experienced user could give me a hint in the right direction, I’ll be really glad.

thx

/Jocke

Switch    GF_Bathroom_Motion             "Motion Sensor"                <motion>                   (GF_Bathroom, gMotion)
Switch    GF_Bathroom_NightLight         "Night Light"                  <light>                    (GF_Bathroom, gLight)
  • Rules code related to the issue
var Timer timer_bathroom = null    

rule "GF_Bathroom_Motion ON"
when
    Item GF_Bathroom_Motion changed from OFF to ON      
then
    sendCommand(GF_Bathroom_NightLight, ON)
    timer_bathroom = createTimer(now.plusMinutes(10)) [|
        sendCommand(GF_Bathroom_NightLight, OFF)
        timer_bathroom = null
    ]
    
end
  • If logs where generated please post these here using code fences:
2018-03-16 14:49:49.176 [vent.ItemStateChangedEvent] - GF_Bathroom_Motion changed from OFF to ON
2018-03-16 14:49:49.188 [GroupItemStateChangedEvent] - gMotion changed from OFF to ON through GF_Bathroom_Motion
2018-03-16 14:49:49.201 [ome.event.ItemCommandEvent] - Item 'GF_Bathroom_NightLight' received command ON
2018-03-16 14:49:49.270 [vent.ItemStateChangedEvent] - GF_Bathroom_NightLight changed from OFF to ON
2018-03-16 14:49:49.274 [vent.ItemStateChangedEvent] - GF_Bathroom_NightLight changed from ON to OFF
2018-03-16 14:49:49.388 [vent.ItemStateChangedEvent] - GF_Bathroom_NightLight changed from OFF to ON
2018-03-16 14:49:53.228 [ome.event.ItemCommandEvent] - Item 'GF_Bathroom_NightLight' received command OFF
2018-03-16 14:49:53.262 [vent.ItemStateChangedEvent] - GF_Bathroom_NightLight changed from ON to OFF
2018-03-16 14:49:53.279 [vent.ItemStateChangedEvent] - GF_Bathroom_NightLight changed from OFF to ON
2018-03-16 14:49:55.804 [vent.ItemStateChangedEvent] - GF_Bathroom_NightLight changed from ON to OFF
2018-03-16 14:50:01.849 [ome.event.ItemCommandEvent] - Item 'GF_Bathroom_NightLight' received command OFF

Here is what could happen with that Rule as written.

  1. GF_Bathroom_Motion changes from OFF to ON
  2. “GF_Bathroom_Motion” Rule triggers
  3. Send ON command to GF_Bathroom_NightLight
  4. Set a 10 minute timer
  5. GF_Bathroom_Motion changed from ON to OFF
  6. GF_Bathroom_Motion changed from OFF to ON
  7. “GF_Bathroom_Motion” Rule triggers
  8. Send ON command to GF_Bathroom_NightLight
  9. Set a 10 minute timer
  10. The first 10 minute timer triggers and sends the OFF command to the light
  11. The second 10 minute timer triggers and sends the OFF command tot he light

The Rule doesn’t take into account that there is already a Timer running so no matter what happens, the light will turn off in 10 minutes after the first time the Rule triggers.

At a minimum you need to check to see if there is already a Timer running and reschedule it.

if(timer_bathroom != null) {
    timer_bathroom.reschedule(now.plustMinutes(10))
}
else {
    ...
}

That will reset the Timer to go off for subsequent motion detections rather than setting a new Timer for every event.

A comprehensive writeup is Design Pattern: Motion Sensor Timer

Thank you for the swift reply, I’ll try your suggestion and let you know the result.
/Jocke

In addition to the previous post accounting for the timer state, another thing you might want to take into account is the on/off state of your light. Always remember that the local user of the light knows what they want and if they are already there, have the on/off/brightness set, changing it at all will just annoy them,

Example:

  • The user walks into the room - light triggers on, but the user then dims light to their desired brightness. The motion sensor triggers again and it goes full bright - repeat…
  • My lights trigger with motion sensors to different brightness levels dependent on time of day. You don’t want to blast your eyes if using the bathroom in the middle of the night. If the user then brightens the light and the motion retriggers, it will dim again.
  • There are 1M more I’m sure.

I’d suggest wrapping another IF statement around your rule with the following (syntax might be screwed up but you get the idea)

If (GF_Bathroom_NightLight.state = off){
    #your rule
}

Triggering a light from a motion sensor is one of the most basic things people want to do when they start with home automation. Frankly, its much more complex than it sounds. Yep, you can pop on a light, but accounting for all of the extra things affect if you really want it to turn on and to what brightness level adds quite a bit of complexity.

Rich you solved my issue, thanks a lot! you made my day, I changed the code like this!

Tom you are absolutely right, I’ll will implement an IF statement checking the time before powering the lights, at least the other ones not yet configured with rules, they have adjustable brightness, this one is a low power ON/OFF LED so it wouldn’t hurt your eyes.

/Jocke

var Timer timer_bathroom = null    

rule "GF_Bathroom_Motion ON"
when
    Item GF_Bathroom_Motion changed from OFF to ON      
then

if(timer_bathroom !== null) {
    timer_bathroom.reschedule(now.plusMinutes(10))
    logInfo("GF_Bathroom_Motion.rules", "RESETING TIMER")
}
else {
    sendCommand(GF_Bathroom_NightLight, ON)
    logInfo("GF_Bathroom_Motion.rules", "SETING TIMER")
    timer_bathroom = createTimer(now.plusMinutes(10)) [|
        sendCommand(GF_Bathroom_NightLight, OFF)
        timer_bathroom = null
        logInfo("GF_Bathroom_Motion.rules", "CANCEL TIMER")
    ]
}
end