Skip to content

Instantly share code, notes, and snippets.

@nickbalch
Last active April 26, 2017 17:09
Show Gist options
  • Save nickbalch/20dcb5e7e22fb57f5041b7ab864c501e to your computer and use it in GitHub Desktop.
Save nickbalch/20dcb5e7e22fb57f5041b7ab864c501e to your computer and use it in GitHub Desktop.
Openhab presence rules for a living area and office. Takes input from PIRs, records presence, and turns on / off lights
rule "Working from home"
when
Item Presence_HomeOffice received command ON or
Item Presence_Living received command OFF
then
if (Presence_HomeOffice.state == ON && SCENE_LateNight.state != ON && Presence_Living.state != ON && GF_Living_Lights.state != OFF && SCENE_Watching.state != ON && SCENE_Evening.state != ON) {
//** if only home office is occupied, turn off lights **//
sendCommand(ALL_GF_LIGHTS,OFF)
}
end
rule "GF empty"
when
Item GF_Presence changed to OFF
then
if (SCENE_Evening.state != ON && SCENE_Watching.state != ON && SCENE_WeekdayMorning.state != ON && SCENE_Nightlight.state !=ON) {
logInfo("presence.rules","Ground Floor Empty - turning off lights")
}
end
Group:Switch:OR(ON,OFF) Presence "Presence [MAP(presence.map):%s]" (Status)
Group Room_Presence (Presence)
Group:Switch:OR(ON,OFF) GF_Presence "Ground Floor [%s]" (Presence)
Group:Switch:OR(ON,OFF) FF_Presence "First Floor [%s]" (Presence)
Switch Presence_Living "Living Room Presence" (Room_Presence,GF_Presence)
Switch Presence_Kitchen "Kitchen Presence" (Room_Presence,GF_Presence)
Switch Presence_Dining "Dining Room Presence" (Room_Presence,GF_Presence)
Switch Presence_Cupboard "Computer Cupboard Presence" (Room_Presence,GF_Presence)
Switch Presence_Utility "Utility Presence" (Room_Presence,GF_Presence)
Switch Presence_HomeOffice "Home Office Presence" (Room_Presence,FF_Presence)
Switch Nightlightmode
import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.joda.time.*
var Timer timer_livingroom
var Timer timer_office
var Timer timer_nightlights
rule "Working from home"
when
Item Presence_HomeOffice received command ON or
Item Presence_Living received command OFF
then
logInfo("presence.rules","Sakhy "+Sakhy_Home.state)
logInfo("presence.rules","Nick "+Nick_Home.state)
logInfo("presence.rules","Lamps "+GF_Living_Lights.state)
logInfo("presence.rules","Watching "+SCENE_Watching.state)
logInfo("presence.rules","Evening "+SCENE_Evening.state)
if (Presence_HomeOffice.state == ON && SCENE_LateNight.state != ON && Presence_Living.state != ON && GF_Living_Lights.state $
println("OFFOFF")
sendCommand(GF_Living_Lamps,OFF)
}
end
rule "LivingRoom Presence and nightlight"
when
Item GF_Living_PIR changed
then
if (PIRSensor_GF_Living.state==ON && SCENE_WeekdayMorning != ON && SCENE_Dark.state == ON && Light_GF_Side_Zwave.state == OFF && Night.state != ON && (Presence_Living.state == OFF || Presence_Living.state == Uninitialized)) {
//** Nightlight: Turn on a light if movement is detected, room is empty, and no lights are on, and no one is already in the room, and it's dark ** //
logInfo("presence.rules","Nightlight mode")
sendCommand(Light,ON)
postUpdate(SCENE_Nightlight,ON)
}
if (PIRSensor_GF_Living.state==ON && (Presence_Living.state == OFF || Presence_Living.state == Uninitialized)) {
//** Presence: mark room as occupied, which is assumed to be the case if PIR remains triggered for 360 seconds ** //
logInfo("presence.rules","Living presence rule - timer for on state")
timer_livingroom = createTimer(now.plusSeconds(360)) [|
sendCommand(Presence_Living,ON)
]
} else {
if(timer_livingroom!=null) {
timer_livingroom.cancel
timer_livingroom=null
}
if (PIRSensor_GF_Living.state==OFF && Presence_Living.state == ON && SCENE_WeekdayMorning != ON) {
//** Turn off lights once room is empty - unless lights are on because it's morning **//
//** PIR is waiting for 5 minutes anyway, perhaps no need for a timer but to avoid flapping ** //
logInfo("presence.rules","Living presence rule creating an off timer loop")
timer_livingroom = createTimer(now.plusSeconds(120)) [
sendCommand(Presence_Living,OFF)
sendCommand(Light,ON)
postUpdate(SCENE_Nightlight,ON)
}
end
rule "NightLights OFF"
when
Item PIRSensor_GF_Living changed to OFF or
Item Presence_Living changed to OFF
then
if (SCENE_Nightlight.state == ON && Presence_Living.state != ON) {
//** turn off the nightlight as long as other lights in the same room are not on
if (Light_1.state == 0 && Light_2.state == OFF && Light_3.state == OFF) {
logInfo("presence.rules","Nightlight rule turned off lights")
sendCommand(Light,OFF)
}
postUpdate(SCENE_Nightlight,OFF)
}
end
rule "HomeOffice Presence"
when
Item PIRSensor_FF_Office changed
then
if (PIRSensor_FF_Office.state==ON && ( Presence_HomeOffice.state == OFF || Presence_HomeOffice.state == Uninitialized)) {
logInfo("presence.rules","office presence rule - kicking off timer for on state")
timer_office = createTimer(now.plusSeconds(360)) [|
sendCommand(Presence_HomeOffice,ON)
]
} else {
if(timer_office!=null) {
logInfo("presence.rules","HomeOffice - first else and now Cancelling timer")
timer_office.cancel
timer_office=null
}
if (PIRSensor_FF_Office.state==OFF && (Presence_HomeOffice.state == ON)) {
logInfo("presence.rules",("office presence rule - kicking off timer for off state")
timer_office = createTimer(now.plusSeconds(60)) [|
sendCommand(Presence_HomeOffice,OFF)
]
} else {
if(timer_office!=null) {
timer_office.cancel
timer_office = null
}
}
end
Group Scene (Status)
Switch SCENE_Watching (Scene) //** when TV is on **//
Switch SCENE_LateNight (Scene) //** someone stayed up late! **//
Switch SCENE_Evening (Scene) //** after work **//
Switch SCENE_Nightlight (Scene) // ** for when nightlight is triggered ** //
Switch SCENE_NightNight (Scene) // ** everyone's asleep ** //
Switch SCENE_WeekdayMorning (Scene) // ** morning mode, breakfast, getting ready for work/school **//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment