Optimizing Home Automation with Semantics: A Success Story

I’ve recently been diving into the world of smart home automation, and I wanted to share a success story that might inspire others looking to enhance their setups. My journey began with a simple yet effective goal: to create a rule that automatically turns off the lights in an empty room. While this might seem straightforward, it involved a bit of trial and error, especially as I transitioned from older OpenHAB versions to the latest OHv3.2.

The challenge was to leverage the power of Location metadata—a feature I’d heard about but hadn’t fully utilized before. I have several groups of lights and occupancy switches spread across my home, and I wanted to ensure that the system could dynamically adjust based on who was in which room. After some research and experimentation, I managed to create two rules that work seamlessly together.

The first rule updates the room occupancy status, while the second handles the light automation. Both utilize the getLocation() method to determine the room’s location and trigger the appropriate actions. Here’s a simplified version of what I came up with:

plaintext
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

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)) [|
if (triggeringItem.state == OFF) {
Light.sendCommand(OFF)
logInfo(“Room Occupancy Update”, triggeringItem.name + " is now empty. Switching off " + Light.name)
timer = null
}
]
end

This setup has been a game-changer for me. It not only simplifies my daily life but also reduces energy waste. I’m particularly proud of how the system handles edge cases, like someone briefly leaving the room to grab something. The built-in delay ensures that lights aren’t turned off prematurely.

I’d love to hear from others who’ve tackled similar projects or have suggestions for improvement. Whether you’re a coding novice like me or a seasoned pro, sharing insights can help us all push the boundaries of what’s possible with smart home automation. Happy tinkering! :rocket: