I’m glad it helped! I use the above together with presence detection - I don’t trigger it from any UI.
In general:
- IF family members are home THEN dis-arm timers
- IF family members are not home THEN arm timers
My presence detection is based entirely on this post:
OpenHAB detects whether a family member is home based on if their phone is connected to the home wifi.
Below is how I’ve implemented it.
Prerequisites
- Network binding installed in OpenHab
- PaperUI → Add-ons → Bindings → I’m glad it helped! I use the above together with presence detection - I don’t trigger it from any UI.
Configuration files
network.things
Change the IP addresses below to those which match those of your phones.
network:pingdevice:dDadAndroid [ hostname="192.168.1.99", retry=1, timeout=5000, refreshInterval=60000 ]
network:pingdevice:dMumAndroid [ hostname="192.168.1.78", retry=1, timeout=5000, refreshInterval=60000 ]
network.items
Switch sPresence "Someone is present"
Group:Switch:AND(OFF,ON) gPresenceSensors
Switch sPresenceTimer { expire="5m,command=OFF" }
Switch sDadAndroid (gPresenceSensors) { channel="network:pingdevice:dDadAndroid:online" }
DateTime dtDadAndroid { channel="network:pingdevice:dDadAndroid:lastseen" }
Switch sMumAndroid (gPresenceSensors) { channel="network:pingdevice:dMumAndroid:online" }
DateTime dtMumAndroid { channel="network:pingdevice:dMumAndroid:lastseen" }
lights.items
Group:Switch:OR(ON,OFF) gAwayLightTimers
Switch sBedroomSideLight "Bedroom Side Light" { channel="mqtt:topic:swBedroomSideLight:switch" }
String strBedroomSideLightTimer1 "Bedroom Side Light" { channel="mqtt:topic:swBedroomSideLight:timer1" }
String strBedroomSideLightTimer2 "Bedroom Side Light" { channel="mqtt:topic:swBedroomSideLight:timer2" }
String strBedroomSideLightTimer3 "Bedroom Side Light" { channel="mqtt:topic:swBedroomSideLight:timer3" }
String strBedroomSideLightTimer4 "Bedroom Side Light" { channel="mqtt:topic:swBedroomSideLight:timer4" }
presence.rules
This is almost exactly the same as the Rules DSL in the post I linked above, just with the Items slightly re-named to match the network.items file.
rule "Reset Presence and sensors to OFF on startup"
when
System started
then
sPresence.sendCommand(OFF)
gPresenceSensors.sendCommand(OFF)
end
rule "A presence sensor was updated"
when
Item gPresenceSensors changed
then
if(sPresenceTimer.state == ON && gPresenceSensors.state == sPresence.state) {
//logInfo("presence-rules", "Timer is running but group and proxy are the same, cancelling timer")
sPresenceTimer.postUpdate(OFF)
}
else if(gPresenceSensors.state == sPresence.state) {
//logInfo("presence-rules", "No timer and both group and proxy are the same, nothing to do")
return;
}
if(gPresenceSensors.state == OFF) {
//logInfo("presence-rules", "Everyone is away, setting anti-flapping timer")
sPresenceTimer.sendCommand(ON)
}
else if(gPresenceSensors.state == ON) {
//logInfo("presence-rules", "Someone came home, setting presence to ON")
sPresence.sendCommand(ON)
}
end
rule "Presence timer expired, no one is home"
when
Item sPresenceTimer received command OFF
then
//logInfo("presence-rules", "Everyone is still away, setting presence to OFF")
sPresence.sendCommand(OFF)
end
lights.rules
//PRESENCE BASED RULES
//ARM LIGHT TIMERS
rule "Arm light timers group when presence is not detected"
when
Item sPresence changed
then
if(sPresence.state==OFF){
gAwayLightTimers.sendCommand(ON)
}
else{
gAwayLightTimers.sendCommand(OFF)
}
end
rule "Arm light timers"
when
Item gAwayLightTimers changed to ON
then
//ARM sBedroomSideLight TO TURN ON AT 1845 +/-15MINS, TURN OFF AT 1930 +/-15MINS, TURN ON 2145 /-15MINS, TURN OFF 2230+/-15MINS
strBedroomSideLightTimer1.sendCommand("{\"Arm\":1,\"Mode\":0,\"Time\":\"18:45\",\"Window\":15,\"Days\":\"1111111\",\"Repeat\":1,\"Output\":1,\"Action\":1}")
strBedroomSideLightTimer2.sendCommand("{\"Arm\":1,\"Mode\":0,\"Time\":\"19:30\",\"Window\":15,\"Days\":\"1111111\",\"Repeat\":1,\"Output\":1,\"Action\":0}")
strBedroomSideLightTimer3.sendCommand("{\"Arm\":1,\"Mode\":0,\"Time\":\"21:45\",\"Window\":15,\"Days\":\"1111111\",\"Repeat\":1,\"Output\":1,\"Action\":1}")
strBedroomSideLightTimer4.sendCommand("{\"Arm\":1,\"Mode\":0,\"Time\":\"22:30\",\"Window\":15,\"Days\":\"1111111\",\"Repeat\":1,\"Output\":1,\"Action\":0}")
end
rule "Disarm light timers"
when
Item gAwayLightTimers changed to OFF
then
//DISARM
var jsonString = "{\"Arm\":0,\"Repeat\":0}"
strBedroomSideLightTimer1.sendCommand(jsonString)
strBedroomSideLightTimer2.sendCommand(jsonString)
strBedroomSideLightTimer3.sendCommand(jsonString)
strBedroomSideLightTimer4.sendCommand(jsonString)
end
sitemap snippets
I have two sitemaps. The normal one doesn’t show any of the above. I use a debug one to monitor what’s going on. Some snippets below.
Frame label="Upstairs lights"{
Switch item=sBedroomSideLight label="Bedroom side" icon="light"
}
Frame label="Devices"{
Text item=sDadAndroid label="Dad's phone [%s]" icon="boy_3"
Text item=dtDadAndroid label="Last seen [%1$ta %1$tR]" icon="time"
Text item=sMumAndroid label="Mum's phone [%s]" icon="girl_2"
Text item=dtMumAndroid label="Last seen [%1$ta %1$tR]" icon="time"
}
Frame label="Debug"{
Switch item=sPresence label="sPresence [%s]"
Switch item=sPresenceTimer label="sPresenceTimer [%s]"
Switch item=gPresenceSensors label="gPresenceSensors [%s]"
Switch item=gAwayLightTimers label="gAwayLightTimers [%s]"
}
Hope this helps!