I am trying to use this Design Pattern to control a bedroom light based on a motion sensor. I have the correct rule setup to switch on the light based on motion and switch it off after a minute. I would also like to have a manual override, so that if somebody flicks the switch, the motion sensor should be ignored.
My override rule is not very clean which i need help with. As soon as the motion is detected, the proxy is switched ON which in turn switches on the light. However, this also turns ON the manual override (I have this setup incase somebody directly flicked the switch). Thus I have to switch OFF this manual override in the rule. Is there any cleaner approach than this?
2017-08-27 14:02:15.082 [INFO ] [lipse.smarthome.model.script.BedRoom] - Current status of Manual override is:OFF
2017-08-27 14:02:15.082 [INFO ] [lipse.smarthome.model.script.BedRoom] - BedRoom Motion Sensor changed to ON
2017-08-27 14:02:15.089 [INFO ] [lipse.smarthome.model.script.BedRoom] - BedRoom Proxy changed to ON
2017-08-27 14:02:15.092 [INFO ] [lipse.smarthome.model.script.BedRoom] - BedRoom Switch received a manual override ON
2017-08-27 14:02:15.094 [INFO ] [lipse.smarthome.model.script.BedRoom] - BedRoom Manual override is set to:OFF
2017-08-27 14:02:21.708 [INFO ] [lipse.smarthome.model.script.BedRoom] - Current status of Manual override is:OFF
2017-08-27 14:03:21.979 [INFO ] [lipse.smarthome.model.script.BedRoom] - BedRoom Motion Sensor changed to OFF
2017-08-27 14:03:21.986 [INFO ] [lipse.smarthome.model.script.BedRoom] - BedRoom Proxy changed to OFF
Items file:
Switch sw_BedRoomLight_proxy "BedRoom Light" <light>
Switch timer_BedRoomLight { expire="1m,command=OFF" }
Switch sw_BedRoomLight_manual
Dimmer dim_BedRoomLight "Bed Room [%.1f]" (gLights) {channel="zigbee:device:blah_switch_level"}
Switch sw_BedRoomLight {channel="zigbee:device:blah_switch_onoff"}
My rules are as follows. The ugly line is sw_BedRoomLight_manual.postUpdate(OFF) in rule “sw_BedRoomLight_proxy changed”:
rule "BedRoom Enter"
when
Item mo_BedRoom changed
then
logInfo("BedRoom","Current status of Manual override is:"+ sw_BedRoomLight_manual.state.toString())
if (sw_BedRoomLight_manual.state ==OFF) //Ignore motion sensor if the switch has been manually trigger
{
if (mo_BedRoom.state == ON){
logInfo("BedRoom","BedRoom Motion Sensor changed to ON ")
timer_BedRoomLight.postUpdate(OFF) //cancel the timer if necessary
sw_BedRoomLight_proxy.postUpdate(ON)
}
else if (mo_BedRoom.state == OFF){
if (timer_BedRoomLight.state == OFF) {
timer_BedRoomLight.sendCommand(ON) //start the timer
}
}
}
end
rule "timer_BedRoomLight expired"
when
Item timer_BedRoomLight received command OFF
then
logInfo("BedRoom","BedRoom Motion Sensor changed to OFF ")
sw_BedRoomLight_proxy.postUpdate(OFF)
end
rule "sw_BedRoomLight_proxy changed"
when
Item sw_BedRoomLight_proxy changed
then
if(sw_BedRoomLight_proxy.state == ON) {
logInfo("BedRoom","BedRoom Proxy changed to ON ")
sw_BedRoomLight.sendCommand(ON) // Turn light switch on
if (now.getHourOfDay() >7 && now.getHourOfDay() <22){ //07:00:00 to 21:59:59
dim_BedRoomLight.sendCommand(100)
}
else{
dim_BedRoomLight.sendCommand(1) //dim lights at night
}
sw_BedRoomLight_manual.postUpdate(OFF)
logInfo("BedRoom","BedRoom Manual override is set to:"+sw_BedRoomLight_manual.state.toString() )
}
else {
logInfo("BedRoom","BedRoom Proxy changed to OFF ")
sw_BedRoomLight.sendCommand(OFF) // Turn light switch off
dim_BedRoomLight.sendCommand(0)
}
end
rule "sw_BedRoomLight received update" //Manual override
when
Item sw_BedRoomLight received update
then
if(sw_BedRoomLight.state == ON ) {
logInfo("BedRoom","BedRoom Switch received a manual override ON ")
sw_BedRoomLight_manual.postUpdate(ON)
sw_BedRoomLight_proxy.postUpdate(ON)
}
else if (sw_BedRoomLight.state == OFF || dim_BedRoomLight.state == 0) {
sw_BedRoomLight_manual.postUpdate(OFF)
sw_BedRoomLight_proxy.postUpdate(OFF)
}
end