Cancel Timer?

ive been pulling my hair the whole day just cant get it to work

how to i cancel/abort the timer???

the sendCommands just keep on executing! i want then to abort execution

var Timer timervakna = null

rule "Hue Vakna"
when
Item HueVakna received command ON
then
sendCommand(hue3bright,5)
sendCommand(hue3colortemp,100)
sendCommand(hue3bright,10)
{timervakna = createTimer(now.plusSeconds(4)) [|sendCommand(hue3bright,20)]}
{timervakna = createTimer(now.plusSeconds(8)) [|sendCommand(hue3bright,30)]}
{timervakna = createTimer(now.plusSeconds(12)) [|sendCommand(hue3bright,40)]}
{timervakna = createTimer(now.plusSeconds(16)) [|sendCommand(hue3bright,50)]}
{timervakna = createTimer(now.plusSeconds(20)) [|sendCommand(hue3bright,60)]}
{timervakna = createTimer(now.plusSeconds(24)) [|sendCommand(hue3bright,70)]}
{timervakna = createTimer(now.plusSeconds(28)) [|sendCommand(hue3bright,80)]}
{timervakna = createTimer(now.plusSeconds(32)) [|sendCommand(hue3bright,90)]}
{timervakna = createTimer(now.plusSeconds(36)) [|sendCommand(hue3bright,100)]}
end

rule "Hue Vakna CANCEL TIMER"
when
Item HueVakna received command OFF
then
if (timervakna !=null) {
timervakna.cancel
timervakna = null
}
end

Hello Nikola

I recommend to use Thread::sleep instead of creating timers…

Item HueVakna received command ON
then
sendCommand(hue3bright,5)
sendCommand(hue3colortemp,100)
sendCommand(hue3bright,10)
Thread::sleep(4000)
sendCommand(hue3bright,20)
Thread::sleep(4000)
sendCommand(hue3bright,30)
...
...
...
end

ok thanks Michael, i use Thread::sleep command

But i need to be able to cancel the ongoing sleep/timer command execution!

I think your main problem in regards to this is that you are creating a whole lot of timers, but you only have one variable to keep the handle to the timer:

With the current setup, you will only keep a handle to the last timer you created, consequently this is the only timer that you can cancel. All the other timers will still be running in your system until they expire.

There are lots of ways to implement this. I would probably do the following:

NOTE: To properly format code in posting use code fences:

```
code goes here
```
var boolean canceled = false

rule "Hue Vakna"
when
    Item HueVanka received command ON
then
    hue3bright.sendCommand(5)
    hue3colortemp.sendCommand(100)

    var int bright = 10

    while(bright < 100 && !canceled){
        hue3bright.sendCommand(bright))
        bright = bright + 10
        Thread::sleep(4000)
    }
    canceled = false
end

rule "Hue Vakna Cancel ON"
when
    Item HueVakna received command OFF
then
    canceled = true
end
2 Likes

that was what i was looking for

thanks for the help and your time Rich :slight_smile: