Help with Timer function

I have a difficulty of cancelling a running timer. So the idea is when I turn ON a Switch, I also set up a timer that will turn it back OFF after 10 seconds. In case I turn the switch OFF manually within this 10 seconds interval, I want the previously create timer to be cancelled, but it seems that it doesn’t work this way for me. The timer is always null and if I switch ON/OFF 4 times, within a short period, 4 timers are created and executed.

rule "Siren switch"
when
    Item siren01_manual_switch received command
then
    var Timer  timer = null

    switch receivedCommand {
            case ON: {
                    logInfo("test01","case ON")
            }
            case OFF: {
                    logInfo("test01","case OFF")
                    timer?.cancel()
            }
    }


    logInfo("siren01","timer current state: " + timer)

    if (timer === null) {
            timer   = createTimer(now.plusSeconds( 10 ))  [|
                    logInfo("siren01","timer turning off " + timer)
                    postUpdate(siren01_manual_switch, OFF) ]
            logInfo("siren01","Setting timer" + timer)
    }
end

2021-02-15 15:49:00.902 [INFO ] [clipse.smarthome.model.script.test01] - case ON
2021-02-15 15:49:00.902 [INFO ] [lipse.smarthome.model.script.siren01] - timer current state: null
2021-02-15 15:49:00.903 [INFO ] [lipse.smarthome.model.script.siren01] - Setting timerorg.eclipse.smarthome.model.script.internal.actions.TimerImpl@2454d020
2021-02-15 15:49:01.284 [INFO ] [clipse.smarthome.model.script.test01] - case OFF
2021-02-15 15:49:01.285 [INFO ] [lipse.smarthome.model.script.siren01] - timer current state: null
2021-02-15 15:49:01.286 [INFO ] [lipse.smarthome.model.script.siren01] - Setting timerorg.eclipse.smarthome.model.script.internal.actions.TimerImpl@3d0f3c7f
2021-02-15 15:49:01.711 [INFO ] [clipse.smarthome.model.script.test01] - case ON
2021-02-15 15:49:01.711 [INFO ] [lipse.smarthome.model.script.siren01] - timer current state: null
2021-02-15 15:49:01.712 [INFO ] [lipse.smarthome.model.script.siren01] - Setting timerorg.eclipse.smarthome.model.script.internal.actions.TimerImpl@3be5ed7a
2021-02-15 15:49:02.156 [INFO ] [clipse.smarthome.model.script.test01] - case OFF
2021-02-15 15:49:02.156 [INFO ] [lipse.smarthome.model.script.siren01] - timer current state: null
2021-02-15 15:49:02.157 [INFO ] [lipse.smarthome.model.script.siren01] - Setting timerorg.eclipse.smarthome.model.script.internal.actions.TimerImpl@66b54392

2021-02-15 15:49:10.903 [INFO ] [lipse.smarthome.model.script.siren01] - timer turning off org.eclipse.smarthome.model.script.internal.actions.TimerImpl@2454d020
2021-02-15 15:49:11.285 [INFO ] [lipse.smarthome.model.script.siren01] - timer turning off org.eclipse.smarthome.model.script.internal.actions.TimerImpl@3d0f3c7f
2021-02-15 15:49:11.712 [INFO ] [lipse.smarthome.model.script.siren01] - timer turning off org.eclipse.smarthome.model.script.internal.actions.TimerImpl@3be5ed7a
2021-02-15 15:49:12.156 [INFO ] [lipse.smarthome.model.script.siren01] - timer turning off org.eclipse.smarthome.model.script.internal.actions.TimerImpl@66b54392

What am I doing wrong?

Declaring your timer handle variable inside the rule. Once your rule has created a timer, it exits and poof that variable has gone.
The created timer still exists, but you have no external reference to it.

You will see in many examples the trick of declaring your handle outside of any rule, making a “global” variable that survives outside of rule runs.

1 Like

oh, dear… what a silly mistake!
thanks, it works now.