# Delay in rule

**URL:** <https://community.openhab.org/t/delay-in-rule/3237>\
**Category:** Scripts & Rules\
**Created:** [October 11, 2015, 2:14pm UTC](https://community.openhab.org/t/delay-in-rule/3237 "2015-10-11T14:14:30Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![rm65453](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/97f17d/32.png) [@rm65453](https://community.openhab.org/u/rm65453)\
**Post date:** [October 11, 2015, 2:14pm UTC](https://community.openhab.org/t/delay-in-rule/3237/1 "2015-10-11T14:14:31Z")

</div>

here is my current rule  
`
rule “Motion Light ON Kitchen”`

`
when
Item Kitchen_Movement changed from CLOSED to OPEN
then
if (Kitchen_Lux.state < 30){
sendCommand(WallSwitch_Kitchen, ON)
}
end
rule “Motion Light OFF Kitchen”
when
Item Kitchen_Movement changed from OPEN to CLOSED
then
kitchen_timer = createTimer(now.plusMinutes(5)) [|
if (Kitchen_Movement.state == CLOSED){
sendCommand(WallSwitch_Kitchen, OFF)
}
else
{
if (Kitchen_Movement.state == OPEN){
need to reset timer here
}
}
]
`

`end
`

what I want to do it turn the light on if motion is detected and luminance is less than 30. that is working welll. the trouble is I want the rule to check if there has been movement in the last 5 minutes. if not then I want the light to turn off, if there was movement detected then i want the light to stay on.

---

<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:** [October 11, 2015, 3:38pm UTC](https://community.openhab.org/t/delay-in-rule/3237/2 "2015-10-11T15:38:24Z")

</div>

I would implement it as follows. Typing this from my phone so it us likely to contain errors.

var Timer timer = null

```
rule "Kitchen light motion"
when
    Item Kitchen_Movement received command
then
    if(Kitchen_Movement.state == OPEN && (timer == null || timer.has terminated)) {
        WallSwitch_Kitchen.sendcommand(ON)
        timer = create timer(now.plus minutes(5), [| WallSwitch_Kitchen.sendcommand(OFF)]
    }
    else if(Kitchen_Movement.state == OPEN) {
        timer.reschedule(now. PlusMinutes(5))
    }
End

```

---

<div class="post-metadata">

**Author:** ![holgerw](https://community.openhab.org/letter_avatar_proxy/v4/letter/h/ea666f/32.png) [@holgerw](https://community.openhab.org/u/holgerw)\
**Post date:** [October 11, 2015, 3:44pm UTC](https://community.openhab.org/t/delay-in-rule/3237/3 "2015-10-11T15:44:54Z")

</div>

I use then a global retriggered counter and a global every minute rule:

var Integer counter\_kitchen\_light = 0

rule “Motion Light ON Kitchen”  
when  
Item Kitchen\_Movement changed from CLOSED to OPEN  
then  
if (Kitchen\_Lux.state \< 30){  
sendCommand(WallSwitch\_Kitchen, ON)  
counter\_kitchen\_light = 6  
}  
end

rule “every minute”  
when  
Time cron “0 \* \* \* \* ?”  
then  
if (counter\_kitchen\_light \> 0) counter\_kitchen\_light = counter\_kitchen\_light - 1  
if (counter\_kitchen\_light == 1) sendCommand(WallSwitch\_Kitchen, OFF)  
end

Holger

---

<div class="post-metadata">

**Author:** ![rm65453](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/97f17d/32.png) [@rm65453](https://community.openhab.org/u/rm65453)\
**Post date:** [October 11, 2015, 3:52pm UTC](https://community.openhab.org/t/delay-in-rule/3237/4 "2015-10-11T15:52:00Z")

</div>

Thanks. Exactly what I needed.

---

<div class="post-metadata">

**Author:** ![rm65453](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/97f17d/32.png) [@rm65453](https://community.openhab.org/u/rm65453)\
**Post date:** [October 11, 2015, 3:52pm UTC](https://community.openhab.org/t/delay-in-rule/3237/5 "2015-10-11T15:52:54Z")

</div>

I think this will turn off the light after 5 minutes regardless of the status of the motion sensor.

---

<div class="post-metadata">

**Author:** ![rm65453](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/97f17d/32.png) [@rm65453](https://community.openhab.org/u/rm65453)\
**Post date:** [October 11, 2015, 4:28pm UTC](https://community.openhab.org/t/delay-in-rule/3237/6 "2015-10-11T16:28:58Z")

</div>

here is the cleaned up working example for reference.  
`
import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.openhab.model.script.actions.Timer
var Timer kitchen_timer = null
rule “Kitchen light motion”
when
Item Kitchen_Movement changed
then
if(Kitchen_Movement.state == OPEN && kitchen_timer == null) {
sendCommand(WallSwitch_Kitchen, ON)
kitchen_timer = createTimer(now.plusMinutes(2)) [|
sendCommand(WallSwitch_Kitchen,OFF)
kitchen_timer = null
]
}
else if (Kitchen_Movement.state == OPEN) {
kitchen_timer.reschedule(now.plusMinutes(2))
}`

`
end
 `

---

<div class="post-metadata">

**Author:** ![holgerw](https://community.openhab.org/letter_avatar_proxy/v4/letter/h/ea666f/32.png) [@holgerw](https://community.openhab.org/u/holgerw)\
**Post date:** [October 11, 2015, 4:35pm UTC](https://community.openhab.org/t/delay-in-rule/3237/7 "2015-10-11T16:35:59Z")

</div>

_I think this will turn off the light after 5 minutes regardless of the status of the motion sensor._  
Yes right, then decrement only if state = open

Holger

---

<div class="post-metadata">

**Author:** ![rm65453](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/97f17d/32.png) [@rm65453](https://community.openhab.org/u/rm65453)\
**Post date:** [October 11, 2015, 4:38pm UTC](https://community.openhab.org/t/delay-in-rule/3237/8 "2015-10-11T16:38:21Z")

</div>

this is what I have now.

`
import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.openhab.model.script.actions.Timer
var Timer kitchen_timer = null
rule “Kitchen light motion”
when
Item Kitchen_Movement changed
then
if(Kitchen_Movement.state == OPEN && kitchen_timer == null) {
if (Kitchen_Lux.state < 30){
sendCommand(WallSwitch_Kitchen, ON)
kitchen_timer = createTimer(now.plusMinutes(1)) [|
sendCommand(WallSwitch_Kitchen,OFF)
kitchen_timer = null
]
}
else {
kitchen_timer = createTimer(now.plusMinutes(1)) [|
kitchen_timer = null
]
```
   }
}
else if (Kitchen_Movement.state == OPEN) {
    kitchen_timer.reschedule(now.plusMinutes(1))
}

```
end
`

---

<div class="post-metadata">

**Author:** ![rm65453](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/97f17d/32.png) [@rm65453](https://community.openhab.org/u/rm65453)\
**Post date:** [October 11, 2015, 5:12pm UTC](https://community.openhab.org/t/delay-in-rule/3237/9 "2015-10-11T17:12:04Z")

</div>

something like this??

`
var Integer counter_kitchen_light = 0
rule “Motion Light Kitchen”
when
Item Kitchen_Movement changed from CLOSED to OPEN
then
if (Kitchen_Lux.state < 30){
sendCommand(WallSwitch_Kitchen, ON)
counter_kitchen_light = 2
}
end
rule “every minute”
when
Time cron “0 * * * * ?”
then
if (Kitchen_Movement.state == CLOSED){
if (counter_kitchen_light > 0) counter_kitchen_light = counter_kitchen_light - 1
if (counter_kitchen_light == 1) sendCommand(WallSwitch_Kitchen, OFF)
}
if (Kitchen_Movement.state == OPEN && counter_kitchen_light < 2){
counter_kitchen_light = 2
}
end
`

---

<div class="post-metadata">

**Author:** ![holgerw](https://community.openhab.org/letter_avatar_proxy/v4/letter/h/ea666f/32.png) [@holgerw](https://community.openhab.org/u/holgerw)\
**Post date:** [October 11, 2015, 5:25pm UTC](https://community.openhab.org/t/delay-in-rule/3237/10 "2015-10-11T17:25:07Z")

</div>

I would say this should work:  
If the Kitchen\_movement changed the counter is set to 6 (in rule “Motion Light Kitchen”)  
Every minute it decrement the counter if nobody detect.  
Please check with value \> 2

var Integer counter\_kitchen\_light = 6 // value \>0 shut down light too when openHAB restart

rule “Motion Light Kitchen”  
when  
Item Kitchen\_Movement changed from CLOSED to OPEN  
then  
if (Kitchen\_Lux.state \< 30){  
sendCommand(WallSwitch\_Kitchen, ON)  
counter\_kitchen\_light = 6  
}  
end

rule “every minute”  
when  
Time cron “0 \* \* \* \* ?”  
then  
if (Kitchen\_Movement.state == CLOSED){  
if (counter\_kitchen\_light \> 0) counter\_kitchen\_light = counter\_kitchen\_light - 1  
if (counter\_kitchen\_light == 1) sendCommand(WallSwitch\_Kitchen, OFF)  
}  
end

Holger

PS: i assume  
closed: nobody is in kitchen  
open: person is detect in kitchen

---

<div class="post-metadata">

**Author:** ![holgerw](https://community.openhab.org/letter_avatar_proxy/v4/letter/h/ea666f/32.png) [@holgerw](https://community.openhab.org/u/holgerw)\
**Post date:** [October 12, 2015, 3:25pm UTC](https://community.openhab.org/t/delay-in-rule/3237/11 "2015-10-12T15:25:49Z")

</div>

i have tried it with timer today, works fine for me:

`
    var Timer kitchen_timer = null
    var Integer kitchen_timeout = 3
```
rule "Kitchen light motion"
when
Item Kitchen_Movement changed to OPEN  
then    
if (Kitchen_Lux.state < 30){    
    if (kitchen_timer != null) 
    {            
        kitchen_timer.reschedule(now.plusMinutes(kitchen_timeout))
        logInfo("Kitchen","Kitchen timer rescheduled for " + kitchen_timeout + " minutes")
    }
    else
    {            
        sendCommand(WallSwitch_Kitchen, ON)
        logInfo("Kitchen","Kitchen timer create with " + kitchen_timeout + " minutes")
        kitchen_timer = createTimer(now.plusMinutes(kitchen_timeout)) 
        [|        
            if (Kitchen_Movement.state == OPEN)   
            {
                kitchen_timer.reschedule(now.plusMinutes(kitchen_timeout))
                logInfo("Kitchen","Kitchen timer triggered, but rescheduled again for " + kitchen_timeout + " minutes")                    
            } 
            else 
            {            
                sendCommand(WallSwitch_Kitchen,OFF)
                kitchen_timer = null
            }
        ]
    }
}

```
`

`end
`

Holger

---

<div class="post-metadata">

**Author:** ![rm65453](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/97f17d/32.png) [@rm65453](https://community.openhab.org/u/rm65453)\
**Post date:** [October 12, 2015, 3:38pm UTC](https://community.openhab.org/t/delay-in-rule/3237/12 "2015-10-12T15:38:21Z")

</div>

Does your kitchen light turn off after 3 minutes even if the state is OPEN?

---

<div class="post-metadata">

**Author:** ![holgerw](https://community.openhab.org/letter_avatar_proxy/v4/letter/h/ea666f/32.png) [@holgerw](https://community.openhab.org/u/holgerw)\
**Post date:** [October 12, 2015, 3:45pm UTC](https://community.openhab.org/t/delay-in-rule/3237/13 "2015-10-12T15:45:56Z")

</div>

No, it stays on.  
open means: person is detected

---

<div class="post-metadata">

**Author:** ![rm65453](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/97f17d/32.png) [@rm65453](https://community.openhab.org/u/rm65453)\
**Post date:** [October 12, 2015, 3:47pm UTC](https://community.openhab.org/t/delay-in-rule/3237/14 "2015-10-12T15:47:44Z")

</div>

I will try out this rule later today. I did not see anything in the rule that would reschedule the timer when the state remains open but the initial timer expires.

Apologize if i missed anything.

---

<div class="post-metadata">

**Author:** ![holgerw](https://community.openhab.org/letter_avatar_proxy/v4/letter/h/ea666f/32.png) [@holgerw](https://community.openhab.org/u/holgerw)\
**Post date:** [October 12, 2015, 3:48pm UTC](https://community.openhab.org/t/delay-in-rule/3237/15 "2015-10-12T15:48:42Z")

</div>

here it is:  
Timer trigger and reschedule itself:

> [@holgerw](#):
>
> if (Kitchen\_Movement.state == OPEN)  
> {  
> kitchen\_timer.reschedule(now.plusMinutes(kitchen\_timeout))  
> logInfo(“Kitchen”,“Kitchen timer triggered, but rescheduled again for " + kitchen\_timeout + " minutes”)  
> }

---

<div class="post-metadata">

**Author:** ![rm65453](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/97f17d/32.png) [@rm65453](https://community.openhab.org/u/rm65453)\
**Post date:** [October 12, 2015, 3:49pm UTC](https://community.openhab.org/t/delay-in-rule/3237/16 "2015-10-12T15:49:53Z")

</div>

but isnt that triggered when the state changes to open.

how will that fire when the state remains open after the initial timer expires?

---

<div class="post-metadata">

**Author:** ![holgerw](https://community.openhab.org/letter_avatar_proxy/v4/letter/h/ea666f/32.png) [@holgerw](https://community.openhab.org/u/holgerw)\
**Post date:** [October 12, 2015, 3:51pm UTC](https://community.openhab.org/t/delay-in-rule/3237/17 "2015-10-12T15:51:23Z")

</div>

the initial timer trigger and reschedule itself, try it out please and look at the loginfos…

---

<div class="post-metadata">

**Author:** ![rm65453](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/97f17d/32.png) [@rm65453](https://community.openhab.org/u/rm65453)\
**Post date:** [October 12, 2015, 3:51pm UTC](https://community.openhab.org/t/delay-in-rule/3237/18 "2015-10-12T15:51:50Z")

</div>

will do. thanks again for all your help.

---

<div class="post-metadata">

**Author:** ![holgerw](https://community.openhab.org/letter_avatar_proxy/v4/letter/h/ea666f/32.png) [@holgerw](https://community.openhab.org/u/holgerw)\
**Post date:** [October 12, 2015, 3:58pm UTC](https://community.openhab.org/t/delay-in-rule/3237/19 "2015-10-12T15:58:10Z")

</div>

if the timer once is started it runs outside the rule. if “expired” the timer look how the state is, if it is open it reschedule itself and again …

---

<div class="post-metadata">

**Author:** ![rm65453](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/97f17d/32.png) [@rm65453](https://community.openhab.org/u/rm65453)\
**Post date:** [October 12, 2015, 3:59pm UTC](https://community.openhab.org/t/delay-in-rule/3237/20 "2015-10-12T15:59:15Z")

</div>

that makes a lot more sense. Sorry am not too familiar with programming rules yet.

[Next page](https://community.openhab.org/t/delay-in-rule/3237.md?page=2)
