# Help with a timer rule

**URL:** <https://community.openhab.org/t/help-with-a-timer-rule/24393>\
**Category:** Scripts & Rules\
**Created:** [March 4, 2017, 8:35pm UTC](https://community.openhab.org/t/help-with-a-timer-rule/24393 "2017-03-04T20:35:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous.one](https://community.openhab.org/user_avatar/community.openhab.org/anonymous.one/32/56272_2.png) [@anonymous.one](https://community.openhab.org/u/anonymous.one)\
**Post date:** [March 4, 2017, 8:35pm UTC](https://community.openhab.org/t/help-with-a-timer-rule/24393/1 "2017-03-04T20:35:14Z")

</div>

I have the following rule setup:

```php
rule "Timer stair_light"
when
    Item stair_light changed to ON
then
    if(stair_light.state==ON) {
        if(timer==null) {
            // first ON command, so create a timer to turn the light off again
            timer = createTimer(now.plusSeconds(10)) [|
                stair_light.sendCommand(OFF)
                logInfo("/var/log/openhab2/openhab.log", "stair_light turned off automatically by timer")
            ]
        } else {
            // subsequent ON command, so reschedule the existing timer
            timer.reschedule(now.plusSeconds(10))
        }
    } else if(stair_light.state==OFF) {
        // remove any previously scheduled timer
        if(timer!=null) {
            timer.cancel
            timer = null
        }
    }
end

```

This works great (only have it set to 10 seconds for testing) if and **only** if OpenHab sends the command to turn the lightswitch on. If the actual lightswitch is turned on manually, the timer never turns the light off; I’m not sure if this is by design or something I have mis-configured within OpenHab.

Here is how the item is setup:

> Switch stair\_light “Stair Light” (AllLights) { zwave=“5:command=switch\_binary,refresh\_interval=15” }

Light switch is a GE 12727, and the z-wave controler is an Aeotec gen 5 USB stick, OpenHab 2.0 on a raspberry Pi 3 Model B.

Any help would help would with what I might be missing would be fantastic! 😩

---

<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 4, 2017, 8:45pm UTC](https://community.openhab.org/t/help-with-a-timer-rule/24393/2 "2017-03-04T20:45:39Z")

</div>

> [@anonymous.one](#):
>
> If the actual lightswitch is turned on manually …

look in the log, do you see your Item stair\_light getting an update? is that ON?

---

<div class="post-metadata">

**Author:** ![anonymous.one](https://community.openhab.org/user_avatar/community.openhab.org/anonymous.one/32/56272_2.png) [@anonymous.one](https://community.openhab.org/u/anonymous.one)\
**Post date:** [March 4, 2017, 9:08pm UTC](https://community.openhab.org/t/help-with-a-timer-rule/24393/3 "2017-03-04T21:08:06Z")

</div>

```
2017-03-04 12:00:13.260 [ItemCommandEvent] - Item 'stair_light' received command ON
2017-03-04 12:00:13.275 [ItemStateChangedEvent] - stair_light changed from OFF to ON

```

That is from my event log and I should of mentioned I was checking via /rest/items/stair\_light to see the state if the light switch is manually turned on.

---

<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 4, 2017, 10:26pm UTC](https://community.openhab.org/t/help-with-a-timer-rule/24393/4 "2017-03-04T22:26:59Z")

</div>

When diagnosing, sprinkle some more logInfo e.g. first line of rule to see if it is triggered,and which if-else paths are followed.

Your timer only gets nulled when stair\_light changes to ON and stair\_light is OFF - and if the timer isn’t null you don’t make a new one.  
Consider changing your logic, perhaps trigger the rule on any change.

---

<div class="post-metadata">

**Author:** ![anonymous.one](https://community.openhab.org/user_avatar/community.openhab.org/anonymous.one/32/56272_2.png) [@anonymous.one](https://community.openhab.org/u/anonymous.one)\
**Post date:** [March 4, 2017, 11:16pm UTC](https://community.openhab.org/t/help-with-a-timer-rule/24393/5 "2017-03-04T23:16:17Z")

</div>

The rule used to be set to `Item stair_light changed`, I switched to be a state of ON to try and rule out issues with the timer variable.

Most of my confusion comes from this working as expected when OpenHab turns on the light, and knows the state is set to ON when I manually change the switch…to me this is a binary state and should behave the same way regardless of how the state changes.

I have even tried the expire binding and reproduced my issue. However testing again with the zwave1 binding instead of zwave and the expire binding just solved my issue. 😠 Guess I will revisit timers when I obtain more automation devices, thanks for your time @rossko57
