Help with time delay that can be canceled

if my garage door is open for more than 5min then sent message through notifymyandroid

i know how to create an timer event, but the timer needs to be canceled if garage door closes

whats the correct code for this type of event?

rule "Garageport Alarm"
when
Item Garaportsensor changed to OPEN
then
(???){notifyMyAndroid(“GaragePort”, “ÖPPEN”)}
end

Use two rules in the same rules file. Rule one when the contact opens that starts the timer, and rule two when the contact closes that kills the timer.

I have a door sensor on my fridge, so that if somebody (cough the wife cough) wedges it open I want to know after five minutes. But if it’s less than that, don’t bother me.

First, the rule to run the timer if the fridge opens:

rule "kitchen fridge open."
when
    Item KITCHEN_FRIDGE_CONTACT changed to OPEN
then
    {
        logInfo("openhab","Kitchen fridge door opened.  Starting timer.")

        Kitchen_Fridge_Timer_Start_Time = now
          
        KITCHEN_FRIDGE_DOOR_TIMER = createTimer(now.plusMinutes(Kitchen_Fridge_Door_Timeout)) 
            [|
                logInfo("openhab","Kitchen fridge door timer expired.  Sending email.")
                Mail_Subject = "Kitchen fridge/freezer door left open!"
          
                var long Time_Difference_In_Minutes = (now.millis - Kitchen_Fridge_Timer_Start_Time.millis) / 60000

                Mail_Message = String::format ("The kitchen fridge door has been left ajar for %1$s minutes!!",Time_Difference_In_Minutes)                
                
                sendMail(Mail_Destination,Mail_Subject,Mail_Message)
                
 KITCHEN_FRIDGE_DOOR_TIMER.reschedule(now.plusMinutes(Kitchen_Fridge_Door_Timeout))
            ]
    }
end

Then a separate rule to kill the timer if the fridge shuts:

rule "kitchen door shut, kill kitchen timer"
when
    Item KITCHEN_FRIDGE_CONTACT changed to CLOSED
then
    {
        logInfo("openhab","Kitchen fridge door closed.  Killing timer.")

        KITCHEN_FRIDGE_DOOR_TIMER.cancel
        KITCHEN_FRIDGE_DOOR_TIMER = null
        Mail_Subject = null
        Mail_Message = null
    }
end

I find a simpler pattern is;

var Timer doorTimer = null

rule "Notify on door open"
when
    Item Door_Contact changed
then
    if (doorTimer != null)
        doorTimer.cancel()

    if (Door_Contact.state == OPEN) {
        doorTimer = createTimer(now.plusMinutes(5)) [|
             // do notification
        ]
    }
end

This works well since you only need a single rule. When the door is opened, the timer is created. If the door is closed before the timeout then the timer is cancelled. Otherwise the timer fires and you get notified.

Simple but effective.

thanks for the help
couldn’t really get my head round that !=null statement

after a lot of trial and error i got this to work for me

rule "test timer"
when

     Item GaragePortSensor received update
then 
if (GaragePortSensor.state == 100){timertest = createTimer(now.plusSeconds(10)) [|notifyMyAndroid("GaragePort", "ÖPPEN")]}

if (GaragePortSensor.state == 0) {timertest.cancel()}

end