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.
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
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))
}
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
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
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.