How to 'createTimer' and 'Timer.cancel' when item change state?

No, GarateDoor_Timer will only receive an OFF command when it expires

Hello,

Interesting but I’ve added the following in the cancellation rule…as you need to cancel a running and not terminated timer but thanks for the Code, ive used it and worked well…just adapted with the below

when
etc…
then
if(garagedoor2Timer === null || garagedoor2Timer.hasTerminated)
{
return;
}
garagedoor2Timer.cancel() //Terminate timer if door is closed before the timer ends

end

There’s a “cheat” for that
garagedoor2Timer?.cancel()
The ? does magic, if variable null will just skip. Otherwise, issues cancel.

Thanks for the update! :slightly_smiling_face:

I’ve ended up with the working following code to cancel, especially that a simple re-initiation to Null was needed for the rule to run properly in the next cycle. I am actually monitoring a smaller door with shorter close / open period!

rule “Cancel telegram Notification | Garagedoor 2 open for 30 min.”
when
Item Power changed from ON to OFF
then
logInfo(“notifications”, “Fridge Closed”)
if(garagedoor2Timer === null || garagedoor2Timer.hasTerminated)
{
logInfo(“notifications”, “Returning”)
return;
}

logInfo("notifications", "reschedule")
//garagedoor2Timer.cancel() //Terminate timer if door is closed before the timer ends
garagedoor2Timer.cancel()
garagedoor2Timer = null
return;

end