Openhab 4.0.4
Hi All,
I’m sure this is a simple one but I’m really struggling.
I want to write a rule that:-
- When the doorbell is rung:-
- The rule remembers / holds the state of a Switch (whether it is on or off)
- Turns the switch on for a period of time
- returns the Switch to whichever state it was in before 3)
I’ve no issue with the timer logic, but I’m struggling with what seems to be the simplest part - setting the switch back to it’s initial state.
This is the rule:-
// Doorbell related rules
rule "Someone has rung the doorbell"
when
Item RingDoorbell changed from OFF to ON
then
sendBroadcastNotification("Someone rang the doorbell...")
logInfo("org.openhab","Someone rang the doorbell...")
RingDoorbell.postUpdate(OFF)
if ( doorbellTimer === null || doorbellTimer.hasTerminated() ) {
logInfo("org.openhab","... and it's dark")
// Get the initial state of the items we're going to change
var SwitchItem initialState_SP_Lights_Garage
initialState_SP_Lights_Garage = SP_Lights_Garage //.state as Switch
logInfo("org.openhab","initialState_SP_Lights_Garage: " + initialState_SP_Lights_Garage)
SP_Lights_Garage.sendCommand(ON)
if (doorbellTimer !== null) {
doorbellTimer.cancel
doorbellTimer = null
}
// Wait for 5 minutes
doorbellTimer = createTimer(now.plusMinutes(1), [|
// RETURN STUFF TO PREVIOUS STATE
logInfo("org.openhab","Returning items to initial state")
if (initialState_SP_Lights_Garage.state == ON) {
logInfo("org.openhab","SP_Lights_Garage.sendCommand(ON)")
SP_Lights_Garage.sendCommand(ON)
} else {
logInfo("org.openhab","SP_Lights_Garage.sendCommand(OFF)")
SP_Lights_Garage.sendCommand(OFF)
}
doorbellTimer = null // reset the timer
])
} else {
doorbellTimer.reschedule(now.plusMinutes(10))
}
end
and this is the log output:-
2023-12-20 08:51:03.019 [INFO ] [penhab.core.model.script.org.openhab] - Someone rang the doorbell...
2023-12-20 08:51:03.022 [INFO ] [penhab.core.model.script.org.openhab] - ... and it's dark
2023-12-20 08:51:03.024 [INFO ] [penhab.core.model.script.org.openhab] - initialState_SP_Lights_Garage: SP_Lights_Garage (Type=SwitchItem, State=OFF, Label=Outside Garage Lights, Category=light)
2023-12-20 08:51:03.028 [INFO ] [penhab.core.model.script.org.openhab] - TP_Link_KP105_1.sendCommand('ON')
2023-12-20 08:52:03.027 [INFO ] [penhab.core.model.script.org.openhab] - Returning items to initial state
2023-12-20 08:52:03.032 [INFO ] [penhab.core.model.script.org.openhab] - SP_Lights_Garage.sendCommand(ON)
So whist the initialState_SP_Lights_Garage variable appears to capture the initial state as being OFF, it fails to return the SP_Lights_Garage Switch to being in a state of OFF at the end (and the lights physically stay on). There seems to be something wrong here:-
if (**initialState_SP_Lights_Garage.state == ON**) {
logInfo("org.openhab","SP_Lights_Garage.sendCommand(ON)")
SP_Lights_Garage.sendCommand(ON)
} else {
logInfo("org.openhab","SP_Lights_Garage.sendCommand(OFF)")
SP_Lights_Garage.sendCommand(OFF)
}
Would really appreciate any help / pointers as this one is driving me mad!
Many thanks
Steve