[SOLVED] Conditional timer

Totaly new to this so please forgive me. I would like to create a rule that;
When a light is turned on, turn it off after 10 minutes, but only if a second light is off

The idea is, if I go into a room and turn the first light on only its means its a quick visit. But if the second light is turned on within the countdown time, don’t turn the first light off.

From reading other threads I have this but I don’t know the correct syntax.

var Timer Light_Timer = null
val Integer Light_TimeOut = 10

rule “Turn Den Light 4 OFF After 10”
when
Item DL4_Switch changed from OFF to ON
then
Light_Timer = createTimer(now.plusMinutes(Light_TimeOut))
{ if (DL1_Switch.state == OFF)
[|
DL4_Switch.sendCommand(OFF)
Light_Timer = null
]
end

Any help appreciated

Hi James,

I’m pretty much a hack, but your code looks reasonable. I test rules like that by using a simple sitemap for my Openhab phone app. That way, I can sit in my recliner and test away. (would also change the timeout to 1 minute during debugging)

The sitemap would include the following…

Switch item=DL1_Switch
Switch item=DL4_Switch

Thanks for the recommendation, I added these to the site map but still does not run.
Cheers

Does a simple rule like the following work?

rule “Test”
when
Item DL4_Switch changed to ON
then
sendBroadcastNotification(“Den Light 4 turned on”)
end

I suggest not to manage a timer yourself. Use something like this:

Switch FF_Foyer_LightSwitch “Foyer Light” (gWallSwitch, gLightSwitch)
{ channel=" zwave:device:9e4ce05e:node2:switch_binary" }
Switch FF_Foyer_LightSwitch_Timer (gWallSwitchTimer) {expire=“5m,command=OFF”}

When a light is turned on, set the associated expired timer to ON. When the timer expires, check to see if the other light is on, and turn off this light if the other light is off. You can also immediately set the timer to OFF when the second light is turned on.

I am new to OpenHab as well. Here is my current rules for the lights. It’s work in progress but hopefully you can pick out a thing or two out of the code.

Also, you should consider adding a motion sensor so you don’t have to turn on the light manually.

Thank you for your help, The below timer rule works just fine as it currently turns the light back off after 1 minute. But I need to add a condition not to turn off the light if a second light has an ON state after the timer period. Cheers

var Timer Light_Timer = null
val Integer Light_TimeOut = 1

rule “Turn Den Light 4 OFF”
when
Item DL4_Switch changed from OFF to ON
then
Light_Timer = createTimer(now.plusMinutes(Light_TimeOut))
[|
DL4_Switch.sendCommand(OFF)
Light_Timer = null
]
end

Hi yfaway, your rules look amazing. Unfortunately I don’t know any code stuff so its all guess work for me. Would you mind having a look at my timer code below and if you have time rewrite to perform the function I need.
Cheers

It may be that DL1_Switch doesn’t have a state yet. (it’s neither on or off yet - or at least as far as Openhab knows)

Try your original code, but toggle DL1_Switch on/off using your sitemap switch first. This will get DL1_Switch in the OFF state.

(If you get if working manually, you may need to use persistence so that Openhab remembers what state the switch was in when Openhab gets restarted)

A small change to the syntax got it working. Thanks yfaway for the ideas in your code. I just needed to put the condition in the timer bit not after it.

var Timer Light_Timer = null
val Integer Light_TimeOut = 1

rule “Turn Den Light 4 OFF”
when
Item DL4_Switch changed from OFF to ON
then
Light_Timer = createTimer(now.plusMinutes(Light_TimeOut))
[|
if(DL1_Switch.state == OFF)
DL4_Switch.sendCommand(OFF)
Light_Timer = null
]
end