Effective use of Timers in Rules

Dear Rich,

thank you so much for this detailed answer. It helped me alot and I appreciate it very much.

My working :slight_smile: rule looks like this in the moment:

rule "Fensteralarm Gäste-WC Offen"
when
    Item EG_Fenster_Gaeste_WC changed
then
        GaesteWC_timer?.cancel // the ? will skip this line if GaesteWC_timer is null
        Thread::sleep(3000)
        
        logInfo("GaesteWC_timer", "Fenster ist " + EG_Fenster_Gaeste_WC.state)

        switch EG_Fenster_Gaeste_WC.state {
            case "OPEN": {
                GaesteWC_timer = createTimer(now.plusMinutes(5), [| 
                    EG_Hue_bloom_Wohnzimmer.sendCommand(HSBType::RED) 
                ])
            }
            case "TILTED": {
                GaesteWC_timer = createTimer(now.plusMinutes(7), [| 
                    EG_Hue_bloom_Wohnzimmer.sendCommand(HSBType::GREEN)
                ])
            }
            case "CLOSED": {
                EG_Hue_bloom_Wohnzimmer.sendCommand(OFF)
            }
        }
end

I also tried your alarm clock example: Alarmclock whit blinking Light

I found some errors eg. lightsBlinkingTimer (in the rule “Alam Lights”) vs. lightBlinkingTimer (in the rule “Stop Alarm”)
the extra S caused me some headache, but I found it! :slight_smile: I’m slowly getting better in it.

This rule worked for me:

rule "Alam Lights"
when
    Item Alarm_MasterEvent received command ON
then
    if(lightsBlinkingTimer != null) {
        lightsBlinkingTimer.cancel
        lightsBlinkingTimer = null
    }

    lightsBlinkingTimer = createTimer(now.plusSeconds(1), [|
        EG_Hue_bloom_Wohn_Switch.sendCommand(if(EG_Hue_bloom_Wohn_Switch.state == ON) OFF else ON)
        lightsBlinkingTimer.reschedule(now.plusSeconds(1))
    ])
end

rule "Stop Alarm"
when
    Item Alarm_MasterEvent received command OFF
then
    if(lightsBlinkingTimer != null) {
        lightsBlinkingTimer.cancel
        lightsBlinkingTimer = null
    }
end

I set up a EG_Hue_bloom_Wohn_Switch as a switch item because the EG_Hue_bloom_Wohnzimmer as a Color item didn’t seems to respond on ON/OFF. I am still wondering why. Without the IF-statement, ON/OFF is working on the Color item, see above in my first rule.

This toggle alarm will of course in the next step be integrated in my "Fensteralarm Gäste-WC Offen" rule.

The only thing I still have to figure out is, how to save the color item status before the “red alarm” to rewrite it when the window will be closed again.

Thanks again so much,
Timo