Like many of the Openhab users I was searching a presence detection rule. In my last topic I try to solve a timer issue and got nice and smart help from @shutterfreak.
Solution you can find here: Switching light by daytime and presence
For me as newbee/advanced many rules for presence detection/ coming home seemed very complex. So I tried my way. And I think I was successfull. Time will show it.
What I want:
- A solution for not switching the light, if network connection from my devices got lost and connected again
- A solution for having other lights switched by the time of day
- Switching light of after leaving
What I use:
- Openhab 2.4
- Raspberry 4
- Design pattern Time of Day
- Openhab Network Binding
- my light binding (Homematic Binding)
ITEMS
Network-Items:
Group:Switch:OR(ON,OFF) gPresence "Anwesenheit"
Switch PingS10PlusOnline "Online" {channel="network:pingdevice:s10plus:online"}
Number PingS10PlusLatency "Pingzeit" {channel="network:pingdevice:s10plus:latency"}
DateTime PingS10PlusLastseen "Zuletzt gesehen" {channel="network:pingdevice:s10plus:lastseen"}
Switch PingS8PlusOnline "Online" {channel="network:pingdevice:s8plus:online"}
Number PingS8PlusLatency "Pingzeit" {channel="network:pingdevice:s8plus:latency"}
DateTime PingS8PlusLastseen "Zuletzt gesehen" {channel="network:pingdevice:s8plus:lastseen"}
Light-Items:
Group:Switch:OR(ON, OFF) gHMFlurLights "Alle Lichter Flur [(%d)]"
Switch HM_SwitchLight1 "HM Flur 1" <light> (gHMFlurLights) {channel="..."}
Switch HM_SwitchLight2 "HM Flur 2" <light> (gHMFlurLights) {channel="..."}
Switch HM_SwitchLight3 "HM Flur 3" <light> (gHMFlurLights) {channel="..."}
RULES
import org.openhab.model.script.actions.Timer
// This timer will take care of turning off presence:
var Timer timer_presence = null
rule "gPresence schalten"
when
Item PingS10PlusOnline changed or
Item PingS8PlusOnline changed
then
if(PingS10PlusOnline.state == ON || PingS8PlusOnline.state == ON){
// Cancel timer_presence if it was running
timer_presence?.cancel()
gPresence.sendCommand(ON)
}
else
timer_presence = createTimer(now.plusSeconds(30), [ |
sendCommand(gPresence, OFF)
timer_presence = null
])
end
rule "Flurlicht schalten bei Anwesenheit"
when
Item gPresence changed
then
switch (gPresence.state) {
case ON:
{
// Process time of day
switch (vTimeOfDay.state) {
case "MORNING",
case "DAY",
case "AFTERNOON":
{
gHMFlurLights.sendCommand(ON)
}
case "EVENING",
case "NIGHT",
case "BED":
{
HM_SwitchLight2.sendCommand(ON)
}
}
}
case OFF:
{
sendCommand(gHMFlurLights, OFF)
}
}
end
At the end I created a small Basic UI Sitemap for checking the function.
sitemap presence label="Anwesenheit"
{
Frame label="Geräte Präsenz"{
Switch item=PingS10PlusOnline label="Galaxy S10 [%s]"
Switch item=PingS8PlusOnline label="Galaxy S8 [%s]"
}
Frame label="gPresence ON/OFF"
{
Switch item=gPresence label="Anwesenheit [%s]"
Switch item=gHMFlurLights
Switch item=HM_SwitchLight1
Switch item=HM_SwitchLight2
Switch item=HM_SwitchLight3
}
}