Timer for lights

Hi All,

I try to switch off my lights, after 5 min when the hue motion detector recognized some motion via the expire binding.
This works as expected, but at the same time, the timer should be reseted to 5 min again if the momtion detector recognize a motion in between. Unfortunately, it switches off the light after 5 min and does not reset the timer.

light.rules

rule "Ankleidezimmer motion"
when
    Item OG1_ak_motion received update ON
then
    OG1_ak_motion_timer.sendCommand(ON)
end

rule "Ankleidezimmer motion timer"
when
    Item OG1_ak_motion_timer changed to OFF
then
    if 
    (OG1_ak_li_decke.state == ON) OG1_ak_li_decke.sendCommand(OFF)
end

light.items

Switch OG1_ak_motion "Bewegungsmelder Ankleidezimmer [%s]" <motion> (gOG1_ak) {channel="hue:0107:0017884b70dd:12:presence"}

Switch OG1_ak_motion_timer { expire="5m,state=OFF" }

Could you let me know, if my code is correct or if I do have here a personal misunderstanding.

Thanks

You should be able to apply this to solve your problem:

You don’t need a helper item:

Switch OG1_ak_motion "Bewegungsmelder Ankleidezimmer [%s]" <motion> (gOG1_ak) {channel="hue:0107:0017884b70dd:12:presence"}
Switch OG1_ak_li_decke { channel="...,expire="5m,command=OFF" }


rule "Ankleidezimmer motion"
when
    Item OG1_ak_motion received update ON
then
    OG1_ak_li_decke.sendCommand(ON)
end

Link the expire command (not state) directly to your switch, it will go to OFF after 5 minutes and will be rescheduled after each ON of your motion sensor.

Hi, this is the rule I use for my kitchen counter lights. it looks pretty similar to what you’re trying to do. I hope it helps :slight_smile:

var Timer KitchenCounterTimer = null

rule “Turn on lights when motion triggered”
when
Item KitchenPIR received update
then
if(KitchenPIR.state == ON) {
if(KitchenCounter.state != ON) KitchenCounter.sendCommand(OnOffType.ON)
logInfo(“KitchenLights.rules”, “PIR Actuated KitchenCounter ON Sent and Timer Started”)
if (KitchenCounterTimer !== null) KitchenCounterTimer .cancel
KitchenCounterTimer = createTimer(now.plusMinutes(10), [ |
KitchenCounter.sendCommand(OnOffType.OFF)
logInfo(“KitchenLights.rules”, “Timer expired KitchenCounter OFF Sent”)
KitchenCounterTimer = null
] )
}
end

Thanks

@ Johan_H, Jon_Tree: I study several posts, where it was mention to use the expire binding instead - less code

@sihui: Understood what you mean, but if I study your code proposal correctly, it switches on the light at each movement - something, I don’t want to have, as this room have a open connection to our sleeping - and I don’t want to have full light at every movement in our bed ;->

I created a helper item with

Switch OG1_ak_motion_timer { expire="5m,state=OFF" }

Is it not the same then in your example?

No.

{ expire="5m,command=OFF" }
1 Like

If I will replace ‘state’ with ‘command’, my issue should be fixed?

Only if you use my example. I still don’t understand your initial question, though.

Hi dascrip,

Maybe I didn’t understand what you’re trying to achieve :slight_smile: The rule I use only turns the light on if it is not on already and sets a timer to turn it back off after ten minutes. If another motion is detected within that ten minutes it resets the timer back to ten minutes to start again so the light will turn off after no motion is detected for ten minutes.

Quite simple:

  • Switch on the light manually.
  • Reset the timer to 5 minutes at motion detection.
  • Switch off light after 5 minutes if no further motion is detected.
  • If further motion is detected in between, reset the time to 5 minutes again.