Timer.cancel doesn't work

I´ve tried to cancel a timer if the movement sensor send the state 1
But it only create a new timer again and again but doesn’t cancel the old one.

its a rule

var Timer flurlichttimer

rule "Licht Flur einschalten Tag"
        Item Flurpraesenz_Status changed to 1
        if((Tag_Nacht.state== ON)&&(HelligkeitFlur.state<30)) {
            if(flurlichttimer !== null  ) {
                flurlichttimer = null


            if( flurlichttimer === null ) {
                flurlichttimer = createTimer(now.plusMinutes(1)) [| Flurlicht.sendCommand(0) ]
                flurlichttimer = null

it should start a dimmer on 100% and restart the timer every time the Sensor send 1 and if the sensor doesn’t send 1 the timer should turn off the Light. But it doesn’t work and I didn’t know what I do wrong

You want to reschedule the timer again for a certain amout of time whenever the motion sensor sends 1 again and not really cancel it, as you do want the light to go off eventually after a sepcified time, don’t you?

This will extend the timer an additional minute whenever there is motion detected. Once no more motion is detected the timer will eventually time out and turn off the light.

                if(flurlichttimer !== null  ) {

I’d also add a


when initially creating the timer, before

      flurlichttimer = null

This should also be within the timer brackets [ ], you currently have it outside, so it gets triggered immediately and not only after the timer has run out, when you would want it.

I’ve changed your indentation so that you can see better what you are doing.

If the if(), passes, you get to createTimer()
That makes a little function to be run in the future, denoted in the [ ]
Your little function is just the sendCommand
That’s all set up to happen one minute in the future.
A “handle” for that timer gets passed to your variable flurlichttimer

The next thing you do is destroy the handle you just saved.
Now you can no longer cancel the timer, you have no handle to access it.
The timer is not destroyed - it will still go off in one minutes time, and you cannot stop it, or even tell if it is running or not.

I expect you wanted to put
flurlichttimer = null
inside the timer’s [ ] function, so that it destroys the handle after the timer function has done its work.


I think reschedule didn’t match with my idea. Cause if it add every movement one extra Minute to my Timer and the movement sensor detected with an intervall off 2 seconds. It comes very quick to an very long timer.

You’re great. It works :slight_smile:

That’s not how rescheduling works. It just restarts the timer with the 1 minute countdown. So not matter how often you reschedule, in the end, once there is not more movement, the timer will time out after 1 minute.

To expand a bit on that; timers are set up for a fixed date and time. We cannot directly set a timer for one minute from now. We have to work out what now plus one minute actually is, and pass that to the timer.

Same applies to rescheduling, the secret is in your own calculations.