Recent events had motivated me to take a look at some of my rules. I wanted to rewrite some of them to take advantage of the Location metadata. My first attempt at this was turning the lights off in a room when it is empty. I originally wrote this rule back in OH v2.4 using Rich’s Associated Items DP. Now that I have upgraded to OHv3.2 I thought the below rules would be a good place to start experimenting with the Semantics metadata features.
For this rule I have several groups of lights - _Lights (e.g. LivingRoom_Lights), and switches called _Occupancy_Switch.
My first rule is a room occupancy updater, which works with the second rule that turns the lights off in the appropriate room. Both rules use the getLocation() method…
rule "Room Occupancy Updater"
when
Member of Adults changed
then
Thread::sleep(15000)
gRoomOccupancy.members.forEach[ room |
var location = getLocation(room)
if (location.name == Chet.state.toString || location.name == Kathrin.state.toString) {
room.sendCommand(ON)
} else {
room.sendCommand(OFF)
}
]
end
and the second rule…
rule "Turn lights off when room is empty"
when
Member of gRoomOccupancy changed to OFF
then
val LightLocation = getLocation(triggeringItem)
val LightName = LightLocation.name+"_Lights"
val Light = ScriptServiceUtil.getItemRegistry.getItem(LightName)
timer = createTimer(now.plusMinutes(5)) [|. //patiently wait in case someone went to the bathroom.
if(triggeringItem.state == OFF) Light.sendCommand(OFF)
logInfo("Room Occupancy Update", triggeringItem.name + " is now empty. Switching off "+ Light.name)
timer = null
]
end
This works, so I wanted to share my success with others who might be looking for this kind of functionality. Be aware, I’m not a professional coder, I put this rule together using various examples I found here in the forum. I’m sure they can be improved. So I welcome feedback/suggestions/etc.
With this success I’m going to modify my Window Open Notification rule. The rule will nag us until we close the window (the nagging feature was a request from my wife.) I will change it to prevent the notification/nagging if the room with the open window is occupied.