Working with Item Groups in OpenHAB 3 — The “Leave-Home” Button

Johannes Schildgen
(Smart)²Home
Published in
5 min readJan 17, 2021

There are many kinds of buttons or switches that can be used to trigger a leave-home procedure. Place such a button next to your front door and you’ll never forget to turn off your lights. In this article, we will create a rule in OpenHAB 3 that turns off all lights, pauses the music, and more. Instead of using a button, you can also trigger the rule by some other event, for example, an Alexa voice command, or when the door is being locked, when no phone is connected to the WiFi network anymore, or something else.

When the button is pressed then …

Rules always consist of a “when” part and a “then” part (optionally an “if” part; and in OpenHAB, the “when” part is also optional). The “when” part is easy: When we press the button, or when the door is locked, as mentioned above.

In the “then” part, you define the action. You could now manually click through the rule-creator UI to send an OFF command to this light, and to that one, and so on.

Another approach is to work with groups here.

Groups in OpenHAB 3

A Group in OpenHAB is an item type. Other item types are Switch, Number, String, and many more. The properties of groups are:

  • A group consists of other items (also other groups) as its members
  • A group has a base type (e.g., a Switch, or a Number)

One example: You can create a group “g_temperature” of the base type Number with all temperature-sensor items as its members. In the group definition, you configure how the group’s value itself will be calculated by using an aggregation function. For the data type Number, you can use the aggregation functions Min, Max, Sum, Average. Let’s say, you have 4 numeric items in your g_temperature group with the values, 19, 20, 24, 22. Then the value of the group itself is calculated as 21,25 for the average function.

For lights, it makes sense to choose the base type “Switch”. Even if some of your lights have the type Number (because they are dimmable), it also works fine. OpenHAB will convert the Number 0 into OFF and vise versa. For the Switch base type, you can choose between multiple logical aggregation functions. The most useful ones are (1) “All ON then ON else OFF” and (2) “One ON then ON else OFF”. Let’s choose (2). This means when one light in the group is on, the group’s value is ON. Basically, this is a Max function for OFF=0 and ON=1. So, only when all lights are off, the group’s value is also OFF.

After creating the group, the next step is assigning this group to all light items. Unfortunately, you have to do this for every single item. Click on “Setting” → “Items” (or via “Model”), open an item, “Edit”, and add the group under “Parent Group(s)”.

Actually, the process of adding multiple existing items to a new group takes more time than setting an individual action for each item within the rule. But using this approach, you can later simply add new lights to the group and you do not need to touch the rule again.

Turn all off, but not this one

When you do not want to turn off all the lights, but only almost one, there are multiple ways to work with these exceptions:

  • Don’t add those exceptional lights to the group (my favorite solution)
  • Turn these lights off and directly on again within the rule (I don’t like that one)
  • Write a script, e.g. in JavaScript, that iterates over the members of the group and checks for every item whether to turn it off or not.

Often, you come to a point where simple When-this-then-that cannot solve your problem. The solution is always writing a custom action in form of a script.

Take a look at this JavaScript rule:

events.sendCommand(“g_light”, “OFF”);
events.sendCommand(“Echo_Kitchen”, “PAUSE”);

This action does not only turn off the lights, but it also pauses the music on an Amazon Echo device. Actually, this can also be done without JavaScript. But here, we can work with Copy-and-Paste and it’s extensible.

Another example rule: We loop over the members of our group “g_light” and decide whether to turn an item off or not:

var blacklist = ["Kitchen_Room"];itemRegistry.getItem(“Licht”).members.forEach(function(item) {
if(blacklist.indexOf(item.getName())>-1) { continue; }
events.sendCommand(item.getName(), “OFF”);
});

If the light is on our blacklist, we skip this item. All others will be turned off.

In the script above, we looped over the members of a group. It is also possible to loop over all items whose names match a specific pattern:

itemRegistry.getItems(“*Light*”).forEach(function(item) {
// do something
});

Another way: itemRegistry.getItemsOfType("Switch") gives us an array of all items that have the type Switch. Mind that this does not find our group g_lights from above. This item has the type “Group” and the base type “Switch”.

Conclusion

There isn’t just one way of doing it. You can create a simple rule in which you specify the actions that are executed when you press your leave-home button, or you can do it within a script. Besides just turning off your lights, you can also turn off your TV, pause the music, activate an alarm system, or shut down rollershutters. Or you play a sound or blink a light when there you left a window open.

“Ouch!”

--

--