# Effective use of Timers in Rules

**URL:** <https://community.openhab.org/t/effective-use-of-timers-in-rules/25016>\
**Category:** Beginners\
**Created:** [March 14, 2017, 9:44pm UTC](https://community.openhab.org/t/effective-use-of-timers-in-rules/25016 "2017-03-14T21:44:50Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ion3](https://community.openhab.org/letter_avatar_proxy/v4/letter/i/a5b964/32.png) [@Ion3](https://community.openhab.org/u/Ion3)\
**Post date:** [March 14, 2017, 9:44pm UTC](https://community.openhab.org/t/effective-use-of-timers-in-rules/25016/1 "2017-03-14T21:44:51Z")

</div>

Hi,

for a while I used openHAB rarly, now as OH2 should be stable, I started over again. Now as I start clean, I would like to learn how to write more effective and clever rules.

I have a window and would like to indicate with my hue, if it stays open for a longer period. At the moment I have three rules and it is working:

```php
var Timer GaesteWC_timer = null	

rule "Fensteralarm Gäste-WC Offen"
when
    Item EG_Fenster_Gaeste_WC changed to OPEN
then
			logInfo("GaesteWC_timer","Fenster ist OFFEN")
			GaesteWC_timer = createTimer(now.plusMinutes(5), [|
					EG_Hue_bloom_Wohnzimmer.sendCommand(HSBType::RED)
		])
end

rule "Fensteralarm Gäste-WC TILTED"
when
    Item EG_Fenster_Gaeste_WC changed to TILTED
then
			logInfo("GaesteWC_timer","Fenster ist TILTED")
			GaesteWC_timer = createTimer(now.plusMinutes(7), [|
					EG_Hue_bloom_Wohnzimmer.sendCommand(HSBType::GREEN)
		])
end

rule "Fensteralarm Gäste-WC Closed"
when
	Item EG_Fenster_Gaeste_WC changed to CLOSED
then
        if(GaesteWC_timer != null) {
        	EG_Hue_bloom_Wohnzimmer.sendCommand(OFF)
            logInfo("GaesteWC_timer","Canceling the Timer")
            GaesteWC_timer.cancel()
            GaesteWC_timer = null
   	    }
end

```

I believe this can be done more easily with less of code. I cannot figure it out. This is not working:

```php
var Timer GaesteWC_timer = null	

rule "Fensteralarm Gäste-WC"

when
    Item EG_Fenster_Gaeste_WC changed
then
	if(EG_Fenster_Gaeste_WC == "OPEN") {
			logInfo("GaesteWC_timer","Fenster ist OFFEN")
			GaesteWC_timer = createTimer(now.plusMinutes(5), [|
					EG_Hue_bloom_Wohnzimmer.sendCommand(HSBType::RED)
		])
	}
	if(EG_Fenster_Gaeste_WC == "TILTED") {
			logInfo("GaesteWC_timer","Fenster ist TILTED")
			GaesteWC_timer = createTimer(now.plusMinutes(7), [|
					EG_Hue_bloom_Wohnzimmer.sendCommand(HSBType::GREEN)
		])
	}
	else {
        if(GaesteWC_timer != null) {
            logInfo("GaesteWC_timer","Canceling the Timer")
            GaesteWC_timer.cancel()
            GaesteWC_timer = null
   	    }
	}
end

```

Is somebody able to give me a hint?

Thank you,  
Timo

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [March 14, 2017, 10:13pm UTC](https://community.openhab.org/t/effective-use-of-timers-in-rules/25016/2 "2017-03-14T22:13:37Z")

</div>

> [@Ion3](#):
>
> I would like to learn how to write more effective and clever rules.

See the [Design Pattern](https://community.openhab.org/tags/designpattern) postings. That is where I and others document all our cleverness. 😉

> [@Ion3](#):
>
> I believe this can be done more easily with less of code. I cannot figure it out. This is not working:

First, let’s look for problems. You do not say in what way this Rule is not working so it will be a challenge.

The first problem I see is you need to access the state of your Item using .state. I.e. `EG_Fenster_Gaeste_WC.state == "OPEN"`

That is probably the biggest source of your error.

Next, before creating a new Timer you should check to see if there already is a Timer. I’m not sure if the states go CLOSED → TILTED → OPEN or CLOSED → OPEN → TILTED but in either case, if it transitions from OPEN → TILTED in less than five minutes you will end up with two Timers running. For example, if you go from OPEN to TILTED then the Hue will go RED in five minutes then GREEN in seven minutes. I assume in this case you would only want it to go to GREEN in seven minutes, skipping the RED.

Thus it will always immediately cancel the timer when the window is OPEN.

So I would add code to always cancel the timer and create a new one based on the current state of the EG\_Fenster\_Gaeste\_WC Item.

```auto
then
        GaesteWC_timer?.cancel // the ? will skip this line if GaesteWC_timer is null

	if(EG_Fenster_Gaeste_WC.state == "OPEN") {
			logInfo("GaesteWC_timer","Fenster ist OFFEN")
			GaesteWC_timer = createTimer(now.plusMinutes(5), [|
					EG_Hue_bloom_Wohnzimmer.sendCommand(HSBType::RED)
		])
	}
	if(EG_Fenster_Gaeste_WC.state == "TILTED") {
			logInfo("GaesteWC_timer","Fenster ist TILTED")
			GaesteWC_timer = createTimer(now.plusMinutes(7), [|
					EG_Hue_bloom_Wohnzimmer.sendCommand(HSBType::GREEN)
		])
	}
end

```

The next error I see is you should have an `else if` for the TILTED if statement. Otherwise, the code will execute the else clause which cancels the timer every time the window is OPEN. This is how the code executes when the window is OPEN.

The window is open so set the timer.  
Check to see if the window is Tilted. If not cancel the timer.

Now for reducing lines of code. My suggestion to always cancel and recreate the Timer is a good first start. You can further reduce the lines of code using a switch statement and combine your log statements which would make your rule look like this:

```auto
then
        GaesteWC_timer?.cancel // the ? will skip this line if GaesteWC_timer is null
        
        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 "CLOSED": {
                GaesteWC_timer = createTimer(now.plusMinutes(7), [| 
                    EG_Hue_bloom_Wohnzimmer.sendCommand(HSBType::GREEN)
                ])
            }
        }
end

```

Looking better. Now if you really really want as few lines of code as possible, you can further reduce this if you don’t mind long lines. The case statements are essentially one-liners so we can eliminate the curly brackets and spacing of the lambdas which results in:

```auto
then
        GaesteWC_timer?.cancel // the ? will skip this line if GaesteWC_timer is null
        
        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 "CLOSED": GaesteWC_timer = createTimer(now.plusMinutes(7), [|EG_Hue_bloom_Wohnzimmer.sendCommand(HSBType::GREEN)])
        }
end

```

---

<div class="post-metadata">

**Author:** ![Ion3](https://community.openhab.org/letter_avatar_proxy/v4/letter/i/a5b964/32.png) [@Ion3](https://community.openhab.org/u/Ion3)\
**Post date:** [March 15, 2017, 9:49pm UTC](https://community.openhab.org/t/effective-use-of-timers-in-rules/25016/3 "2017-03-15T21:49:59Z")

</div>

Dear Rich,

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

My working 🙂 rule looks like this in the moment:

```php
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](https://community.openhab.org/t/alarmclock-whit-blinking-light/8891/2)

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! 🙂 I’m slowly getting better in it.

This rule worked for me:

```php
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

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [March 15, 2017, 9:58pm UTC](https://community.openhab.org/t/effective-use-of-timers-in-rules/25016/4 "2017-03-15T21:58:45Z")

</div>

> [@Ion3](#):
>
> rewrite it when the window will be closed again.

You have three options:

1. Save the color to another Item before changing it and restore it from that Item when the Window closes.

2. Set up persistence and use `EG_Hue_bloom_Wohn_Switch.previousState(true)` to get at the previous color

3. Save the HSBType to a global var and restore from that.

---

<div class="post-metadata">

**Author:** ![Ion3](https://community.openhab.org/letter_avatar_proxy/v4/letter/i/a5b964/32.png) [@Ion3](https://community.openhab.org/u/Ion3)\
**Post date:** [March 15, 2017, 10:05pm UTC](https://community.openhab.org/t/effective-use-of-timers-in-rules/25016/5 "2017-03-15T22:05:40Z")

</div>

Rich, can you give me more details to 1 & 3?

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [March 15, 2017, 10:12pm UTC](https://community.openhab.org/t/effective-use-of-timers-in-rules/25016/6 "2017-03-15T22:12:12Z")

</div>

1. 

```auto
Color LastLightColor

```

```auto
case "OPEN": {
    GaesteWC_timer = createTimer(now.plusMinutes(5), [| 
        LastLightColor.postUpdate(EG_HUE_bloom_Wohnzimmer.state)
        EG_Hue_bloom_Wohnzimmer.sendCommand(HSBType::RED) 
    ])
}

...
case "CLOSED": {
    EG_Hue_bloom_Wohnzimmer.sendCommand(LastLightColor.state)
}

```

1. 

```auto
var HSBType lastColor = null

...
case "OPEN": {
    GaesteWC_timer = createTimer(now.plusMinutes(5), [| 
        lastColor = EG_HUE_bloom_Wohnzimmer.state
        EG_Hue_bloom_Wohnzimmer.sendCommand(HSBType::RED) 
    ])
}

...
case "CLOSED": {
    EG_Hue_bloom_Wohnzimmer.sendCommand(lastcolor)
}

```

It’s a shame you are not interested in 2. That would be my preferred solution. I find you get more capabilty for less code and complexity when you take advantage of the features of OH to solve your problems.

---

<div class="post-metadata">

**Author:** ![Ion3](https://community.openhab.org/letter_avatar_proxy/v4/letter/i/a5b964/32.png) [@Ion3](https://community.openhab.org/u/Ion3)\
**Post date:** [March 15, 2017, 10:32pm UTC](https://community.openhab.org/t/effective-use-of-timers-in-rules/25016/7 "2017-03-15T22:32:21Z")

</div>

I thought that previousState would not be a good idea, as I wanted the lamp to blink acc. to your “Alam Lights” example. Because of toggling every second ON/OFF the previousState is very uncertain.

If I am wrong, please also present solution 2! 🙂

The second reason for avoiding option 2 was: I have not worked with persistence so far and the Add-ons in OH2 are still 1.9.0 so I did not want the risk to go insane.

To re-use code, do you think it would be a good idea to use your option 1 to call the `Alarm_MasterEvent` like this:

```csv
Color LastLightColor

```

```php
case "OPEN": {
    GaesteWC_timer = createTimer(now.plusMinutes(5), [| 
        LastLightColor.postUpdate(EG_HUE_bloom_Wohnzimmer.state)
        Alarm_MasterEvent.sendCommand(ON)
    ])
}

...
case "CLOSED": {
    EG_Hue_bloom_Wohnzimmer.sendCommand(LastLightColor.state)
    Alarm_MasterEvent.sendCommand(OFF)
}

```

---

<div class="post-metadata">

**Author:** ![Ion3](https://community.openhab.org/letter_avatar_proxy/v4/letter/i/a5b964/32.png) [@Ion3](https://community.openhab.org/u/Ion3)\
**Post date:** [March 15, 2017, 10:43pm UTC](https://community.openhab.org/t/effective-use-of-timers-in-rules/25016/8 "2017-03-15T22:43:15Z")

</div>

I got the following error in the log:

```csv
java.lang.RuntimeException: The name 'EG_HUE_bloom_Wohnzimmer' cannot be resolved to an item or type.

```

This was my code:

```php
var HSBType lastColor = null

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.plusSeconds(10), [| 
                    lastColor = EG_HUE_bloom_Wohnzimmer.state
                    Alarm_MasterEvent.sendCommand(ON) 
                ])
            }
            case "TILTED": {
                GaesteWC_timer = createTimer(now.plusSeconds(10), [| 
                    lastColor = EG_HUE_bloom_Wohnzimmer.state
                    Alarm_MasterEvent.sendCommand(ON)
                ])
            }
            case "CLOSED": {
                Alarm_MasterEvent.sendCommand(OFF)
                EG_Hue_bloom_Wohnzimmer.sendCommand(lastcolor)
            }
        }
end

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

```

Can you see the problem?

---

<div class="post-metadata">

**Author:** ![Ion3](https://community.openhab.org/letter_avatar_proxy/v4/letter/i/a5b964/32.png) [@Ion3](https://community.openhab.org/u/Ion3)\
**Post date:** [March 15, 2017, 11:00pm UTC](https://community.openhab.org/t/effective-use-of-timers-in-rules/25016/9 "2017-03-15T23:00:20Z")

</div>

The same happens for option 1:

```csv
java.lang.RuntimeException: The name 'EG_HUE_bloom_Wohnzimmer' cannot be resolved to an item or type.

```

This was the code:

In items:

```csv
Color LastLightColor

```

In rules:

```php
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.plusSeconds(10), [| 
                    LastLightColor.postUpdate(EG_HUE_bloom_Wohnzimmer.state)
                    Alarm_MasterEvent.sendCommand(ON) 
                ])
            }
            case "TILTED": {
                GaesteWC_timer = createTimer(now.plusSeconds(10), [| 
                    LastLightColor.postUpdate(EG_HUE_bloom_Wohnzimmer.state)
                    Alarm_MasterEvent.sendCommand(ON)
                ])
            }
            case "CLOSED": {
                Alarm_MasterEvent.sendCommand(OFF)
                EG_Hue_bloom_Wohnzimmer.sendCommand(LastLightColor.state)
            }
        }
end

```

…and because of this error I believe the rule is not executed any further. The `Alarm_MasterEvent` is not changing state.

Any idea?

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [March 15, 2017, 11:11pm UTC](https://community.openhab.org/t/effective-use-of-timers-in-rules/25016/10 "2017-03-15T23:11:00Z")

</div>

> [@Ion3](#):
>
> The same happens for option 1:
> 
> java.lang.RuntimeException: The name ‘EG\_HUE\_bloom\_Wohnzimmer’ cannot be resolved to an item or type.

You might have a guess at this. It means it couldn’t find an Item of that name.  
In your original rule you have  
EG\_Hue\_bloom\_Wohnzimmer  
Note case of Hue not HUE

---

<div class="post-metadata">

**Author:** ![Ion3](https://community.openhab.org/letter_avatar_proxy/v4/letter/i/a5b964/32.png) [@Ion3](https://community.openhab.org/u/Ion3)\
**Post date:** [March 15, 2017, 11:26pm UTC](https://community.openhab.org/t/effective-use-of-timers-in-rules/25016/11 "2017-03-15T23:26:05Z")

</div>

Brilliant! It works! Thank you Rossko57!

In items:

```csv
Color LastLightColor

```

In rules:

```php
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.plusSeconds(10), [| 
                    LastLightColor.postUpdate(EG_Hue_bloom_Wohnzimmer.state)
                    Alarm_MasterEvent.sendCommand(ON) 
                ])
            }
            case "TILTED": {
                GaesteWC_timer = createTimer(now.plusSeconds(10), [| 
                    LastLightColor.postUpdate(EG_Hue_bloom_Wohnzimmer.state)
                    Alarm_MasterEvent.sendCommand(ON)
                ])
            }
            case "CLOSED": {
                Alarm_MasterEvent.sendCommand(OFF)
                EG_Hue_bloom_Wohnzimmer.sendCommand(LastLightColor.state)
            }
        }
end

rule "Alam Lights"
when
    Item Alarm_MasterEvent received command ON
then
    if(lightsBlinkingTimer != null) {
        lightsBlinkingTimer.cancel
        lightsBlinkingTimer = null
    }
    EG_Hue_bloom_Wohnzimmer.sendCommand(HSBType::RED)
        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 learned a lot!

Still some ideas to option 2?

> [@Ion3](#):
>
> I thought that previousState would not be a good idea, as I wanted the lamp to blink acc. to your “Alam Lights” example. Because of toggling every second ON/OFF the previousState is very uncertain.
> 
> If I am wrong, please also present solution 2! 🙂
> 
> The second reason for avoiding option 2 was: I have not worked with persistence so far and the Add-ons in OH2 are still 1.9.0 so I did not want the risk to go insane.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [March 16, 2017, 3:50am UTC](https://community.openhab.org/t/effective-use-of-timers-in-rules/25016/12 "2017-03-16T03:50:05Z")

</div>

> [@Ion3](#):
>
> I thought that previousState would not be a good idea, as I wanted the lamp to blink acc. to your “Alam Lights” example. Because of toggling every second ON/OFF the previousState is very uncertain.

That does make it more complicated. You could use historicState(now.minusMinutes(X)) where X is how long ago the lights started blinking. But once you do that you really are not gaining much by using Persistence.

> [@Ion3](#):
>
> To re-use code, do you think it would be a good idea to use your option 1 to call the Alarm\_MasterEvent like this:

If I understand correctly, I actually wrote that up as a Design Pattern ([Separation of Behaviors](https://community.openhab.org/t/design-pattern-separation-of-behaviors/15886)).

---

<div class="post-metadata">

**Author:** ![Ion3](https://community.openhab.org/letter_avatar_proxy/v4/letter/i/a5b964/32.png) [@Ion3](https://community.openhab.org/u/Ion3)\
**Post date:** [March 18, 2017, 2:38pm UTC](https://community.openhab.org/t/effective-use-of-timers-in-rules/25016/13 "2017-03-18T14:38:34Z")

</div>

Dear Rich,  
dear rossko57,  
dear Forum,

thank you very much. I learned a lot and would like to share some of my learnings:

As my window has three states (TILTED, OPEN, CLOSED) I had the issue, if I moved from OPEN to TILTED, it lost the origin status of the light. First I had the following solution. I added

```php
if(Alarm_MasterEvent.state == OFF) Alarm_MasterEvent.sendCommand(ON)

```

in the Timer. Then I realized to put

```php
    Item Alarm_MasterEvent changed to ON

```

instead of

```php
    Item Alarm_MasterEvent received command ON

```

in the when section of the ’`rule "Alam Lights"`. This seems to have the same function.

Furthermore I realized that

```php
var HSBType lastColor = null

```

does not work. It gave me a mismatch error message in the Eclipse SmartHome Designer. I tried it, it practically did not work. By not defining the type of variable, it worked. I have no explanation for that.

My code looks like that in the moment and my next step will be to add more windows. 🙂

```php
var lastColor = null

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.plusSeconds(5), [|Alarm_MasterEvent.sendCommand(ON)])
            }
            case "TILTED": {
                GaesteWC_timer = createTimer(now.plusSeconds(7), [|Alarm_MasterEvent.sendCommand(ON)])
            }
            case "CLOSED": {
        		Alarm_MasterEvent.sendCommand(OFF)               
            }
        }
end

rule "Alam Lights"
when
    Item Alarm_MasterEvent changed to ON
then
    if(lightsBlinkingTimer != null) {
        lightsBlinkingTimer.cancel
        lightsBlinkingTimer = null
    }
    lastColor = EG_Hue_bloom_Wohnzimmer.state
    Thread::sleep(500)
    EG_Hue_bloom_Wohnzimmer.sendCommand(HSBType::RED)
        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
	EG_Hue_bloom_Wohnzimmer.sendCommand(lastColor)
    if(lightsBlinkingTimer != null) {
        lightsBlinkingTimer.cancel
        lightsBlinkingTimer = null
    }
end

```

If you should have anything to add, I am always happy to learn from you!

Greetings,  
Timo

---

<div class="post-metadata">

**Author:** ![Marty56](https://community.openhab.org/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@Marty56](https://community.openhab.org/u/Marty56)\
**Post date:** [March 19, 2017, 12:17pm UTC](https://community.openhab.org/t/effective-use-of-timers-in-rules/25016/14 "2017-03-19T12:17:06Z")

</div>

You may shorten your code

> [@Ion3](#):
>
> ```
> if(lightsBlinkingTimer != null) {
> lightsBlinkingTimer.cancel
> lightsBlinkingTimer = null
> }
> 
> ```

as Rich has already suggested by

```csv
lightsBlinkingTimer?.cancel

```

---

<div class="post-metadata">

**Author:** ![antares2001](https://community.openhab.org/letter_avatar_proxy/v4/letter/a/6bbea6/32.png) [@antares2001](https://community.openhab.org/u/antares2001)\
**Post date:** [December 1, 2017, 4:52pm UTC](https://community.openhab.org/t/effective-use-of-timers-in-rules/25016/15 "2017-12-01T16:52:57Z")

</div>

@rlkoshak your `timerName?.cancel` is a great tip! Saved me so much hassle! Thank you!

---

<div class="post-metadata">

**Author:** ![Ion3](https://community.openhab.org/letter_avatar_proxy/v4/letter/i/a5b964/32.png) [@Ion3](https://community.openhab.org/u/Ion3)\
**Post date:** [March 1, 2019, 11:07pm UTC](https://community.openhab.org/t/effective-use-of-timers-in-rules/25016/16 "2019-03-01T23:07:24Z")

</div>

> [@Ion3](#):
>
> .sendCommand(HSBType::RED)

Does anybody have a clue how to update the rule?

It seems the HUE-Binding has changed a lot. I’m running on openHAB 2.3.0 now.

---

<div class="post-metadata">

**Author:** ![jensilein](https://community.openhab.org/letter_avatar_proxy/v4/letter/j/5f9b8f/32.png) [@jensilein](https://community.openhab.org/u/jensilein)\
**Post date:** [August 9, 2019, 9:10pm UTC](https://community.openhab.org/t/effective-use-of-timers-in-rules/25016/17 "2019-08-09T21:10:08Z")

</div>

Hi, when I use the

timerName?.cancel command in a rule I get the following error message in openhab.log and the rule doesn\*t work at all.

```auto
2019-08-09 22:35:53.739 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'WZ_Alle_Rollos': 'cancel' is not a member of 'Object'; line 31, column 2, length 18

```

What is the reason for that and how can it be solved?

---

<div class="post-metadata">

**Author:** ![noppes123](https://community.openhab.org/user_avatar/community.openhab.org/noppes123/32/115976_2.png) [@noppes123](https://community.openhab.org/u/noppes123)\
**Post date:** [August 9, 2019, 9:18pm UTC](https://community.openhab.org/t/effective-use-of-timers-in-rules/25016/18 "2019-08-09T21:18:53Z")

</div>

You probably haven’t declared the `timerName` as a `Timer` variable and it is interpreted as an instance of `Object` instead.

---

<div class="post-metadata">

**Author:** ![Ion3](https://community.openhab.org/letter_avatar_proxy/v4/letter/i/a5b964/32.png) [@Ion3](https://community.openhab.org/u/Ion3)\
**Post date:** [January 12, 2021, 7:26pm UTC](https://community.openhab.org/t/effective-use-of-timers-in-rules/25016/19 "2021-01-12T19:26:40Z")

</div>

Does anybody have an idea how to transfer the rules into Openhab 3.0?

The main rule checks for the change of the window item and starts a timer based on the three cases. After the timer has expired, it sends the command ON to the virtual item Alarm\_MasterEvent:

```
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), [|Alarm_MasterEvent.sendCommand(ON)])

            }

            case "TILTED": {

                GaesteWC_timer = createTimer(now.plusMinutes(7), [|Alarm_MasterEvent.sendCommand(ON)])

            }

            case "CLOSED": {

                Alarm_MasterEvent.sendCommand(OFF)               

            }

        }

end

```

Changes of the item Alarm\_MasterEvent will be handled in these two rules:

```
rule "Alam Lights"

when

    Item Alarm_MasterEvent changed to ON

then

    if(lightsBlinkingTimer != null) {

        lightsBlinkingTimer.cancel

        lightsBlinkingTimer = null

    }

    EG_Hue_bloom_Wohn_LastColor.postUpdate(EG_Hue_bloom_Wohnzimmer.state) //tracking state by item

    EG_Hue_bloom_Wein_LastColor.postUpdate(EG_Hue_bloom_Weinschrank.state)

    Thread::sleep(500)

    EG_Hue_bloom_Wohnzimmer.sendCommand(HSBType::RED)

    EG_Hue_bloom_Weinschrank.sendCommand(HSBType::RED)

        lightsBlinkingTimer = createTimer(now.plusSeconds(1), [|

                EG_Hue_bloom_Wohn_Switch.sendCommand(if(EG_Hue_bloom_Wohn_Switch.state == ON) OFF else ON)

                EG_Hue_bloom_Wein_Switch.sendCommand(if(EG_Hue_bloom_Wein_Switch.state == ON) OFF else ON)

        lightsBlinkingTimer.reschedule(now.plusSeconds(1))

    ])

end

```

and

```
rule "Stop Alarm"

when

    Item Alarm_MasterEvent received command OFF

then

    EG_Hue_bloom_Wohnzimmer.sendCommand(EG_Hue_bloom_Wohn_LastColor.state) // posting state of lastColor item

    EG_Hue_bloom_Weinschrank.sendCommand(EG_Hue_bloom_Wein_LastColor.state)

    if(lightsBlinkingTimer != null) {

        lightsBlinkingTimer.cancel

        lightsBlinkingTimer = null

    }

end

```

I have no clue how to set this up in OH3. The start is easy:

 ![Change_State](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/2/4/24b392fec62baed605e6c0cff0a2fe14909304fa.png)

Then I tried this code:

 ![Code](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/7/3/739895d18c936d31397e69b299f8e148c4aee332.png)

And checked for the status change of item Alarm\_MasterEvent:

 ![item_Alarm_MasterEvent](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/6/8/68f8520cf873bff22a4e80d04e86460dd7117ee9.png)

Analyse tracks no status change, the event log shows no changes and the rule delivers no errors.

Can anybody help? I would love to do it with Blockly but it seems not to have timers at all.

Thanks,  
Timo

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [January 12, 2021, 8:38pm UTC](https://community.openhab.org/t/effective-use-of-timers-in-rules/25016/20 "2021-01-12T20:38:38Z")

</div>

> [@Ion3](#):
>
> Then I tried this code:

There are two problems with the code you tried.

1. GaesteWC\_timer is a global variable. I know of no way to define a global variable like that in Rules DSL in the UI. If you have rules that use a global variable like this, you are better off keeping it in .rules files. This is even more the case if that global variable is used by more than one Rule.

2. The “then” on the first line renders this script syntactically invalid. Script Actions should not include “rule”, “when”, the triggers, “then” and “end”.

And if you are creating Timers anyway, why the sleep for three seconds?

[Next page](https://community.openhab.org/t/effective-use-of-timers-in-rules/25016.md?page=2)
