[SOLVED] Help with Motion sensor goes too off too quick / canceling a timer

Hi everyone

I have a motion sensor in my living room this changes from off too on very fast and is unreliable for telling me if there is anyone occupying the living room when you are relaxing and haven’t moved it goes too off but your still there

I would like too fix this so any help or pointers appreciated

(WHAT I WAS THINKING)

A proxy item with timers but I couldent visualise the way too do this?

This is something I need too learn now and I have asked about this before but couldn’t get my head around it but I’m slightly more experienced now so hopefully get it down this time as a few of my rules use timers that could also do with a cancel featute

I know this is something like add a proxy item if sensor goes on change proxy item too on if it changes too off start a timer and update the proxy item too off after the timer elapsed but if it changes back too on while the timers running cancel the timer

It’s canceling a timer that I’m mainly stuck with

Here is a good place to start: [Deprecated] Design Pattern: Motion Sensor Timer. It isn’t exactly your case but it is pretty close and it shows how to use the Timers.

If you are using a Timer then

timer?.cancel

The ? is a short hand way to say skip this line if timer is null.

If you are using Design Pattern: Expire Binding Based Timers then

MotionTimer.postUpdate(OFF)

Your overall approach is sound. I personally would recommend using the Expire binding based timers unless you need to use a different amount of time for the Timer based on conditions. Then you don’t even need Rules to handle the OFF command.

Switch Living_Room_Motion (MotionSensors) ...
Switch Living_Room_Motion_Proxy { expire="30s,command=OFF" }
...
rule "Motion sensor triggered"
when
    Member of MotionSensors changed from OFF to ON
then
    sendCommand(triggeringItem.name+"_Proxy", ON)
end

When a member of MotionSensors changes from OFF to ON, we send the ON command to the proxy. The Expire binding will sendCommand OFF to the proxy 30 seconds after the last time it receives an ON command.

1 Like

Rich I’m going too start by saying thanks again I know it was you who posted last time I asked this sort of thing hopefully learn it this time

I will try this I remember from last time you need too name your timer?

I’m not using this but it’s on my todo learn list I have been recommended too use it in other posts I tried but couldent get it too work I didnt use text files then tho and that was the problem :wink:

I’m going too give the expire binding a try and also read the design pattern

createTimer creates a Timer Object. If you want to interact with the Timer that was created you have to store that in a variable. In the line of code above, it assumes you assigned the Timer Object to a variable timer.

var Timer timer = null
...
    timer = createTimer(now.plus...

...
    timer?.cancel

So we have a global variable named “timer”. We create the Timer and assign it to “timer”. Later on we can cancel the created Timer using “timer” variable.

1 Like
var Timer timer = null

rule "Timer test"
when
    Item TestSwitch1 changed to ON
then
    logInfo("Timer", "Created")
    timer = createTimer(now.plusSeconds(25), [| 
    TestSwitch1.sendCommand ("OFF")
    ])
end


rule "timer cancel"
when
    Item TestSwitch2 changed to ON
then
    timer?.cancel
    logInfo("Timer", "Canceled")
end

Worked as expected thanks for that again :slight_smile:

i have also installed the expire binding and added a test item worked perfect Thanks for that also

but If I use the expire binding wouldn’t the proxy item turn off even when motion was still detected ?

Having a read “When the motion detector sends an ON command, create a timer. If subsequent ON commands are received reschedule the timer. When the Timer expires, turn the Occupancy Switch OFF.”

That is my use case perfectly so i’m going too use that i think

No because, as I showed in the Rule in post 2, every time there is motion the Proxy gets a new ON command. This resets the Expire binding so it will not command the Proxy to OFF until 30 seconds after the last ON command is received.

my mistake i see it now i’m still not used too rules with triggering item ect

Thanks for that i have learned two new things today :slight_smile:

and got two ways of solving my problem im going too work on it now and will mark this as solved

@rlkoshak Thanks for that i ended up using a proxy item and the expire binding

In my rules, I avoid using rule triggers like

when
     Member of MotionSensors changed from OFF to ON

and instead use

when
     Member of MotionSensors changed

Reason, so that in the cases where the sensor is continuosly triggered for a long period, the timing action (re)starts from the end of the motion.

My sensors stay triggered - some types will instead drop off after their alloted time even if motion still going on, and then re-trigger. So you might not need to do that, the timer would automatically restart.

Triggering from either ON or OFF my way is still going to go wrong if there is any chance the continuous motion signal will hold up for longer than your timer.
My own timers are tens of minutes, so that is never a problem for me.

But a way around that could be two separate rules -
Turn ON the light when motion starts.
But don’t start the timer until motion ends.

The “best” way depends on the exact behaviour of your sensors and the lengths of time involved. I understand some sensors (Zigbee?) never even send an ‘end’, only ‘start’.

Some zwave ones as well I believe.

My motion sensors are philips hue ones (zigbee)

This is not a general rule - some sensors may not, but some definitely do, and the ZigBee standard does allow for the device to tell the controller if it does or not (if I remember correctly).