Automation #4: Smart Radiator (Generic Rule)

Hello!

This example is a generic version previous example. It is common that while you are developing the automation for your house, you need the same logic of a particular for more than a room. For example, in my previous example, I shared a rule to turn off the radiator of the living room if when one of its windows is opened. The logic of that rule applies to all rooms with a radiator and smart windows (contacts).

In OpenHAB one way to declare such generic rules is by using the Design Pattern: Associated Items. In other words, you should use a particular naming pattern to associate related items (e.g., LivingRoom_Radiator, LivingRoom_Lamp).

For this example will be using a group item called gWindows to group all window-contacts. Every window and radiator is named using the following pattern ROOM_Radiator and ROOM_Window.

As the window-contacts are the trigger of our generic rule, we use the gWindows item as our event source.

This solution is not final or complete but can help new members to start with OpenHAB (at least that’s my objective :slightly_smiling_face:). It is based on all feedback and solutions shared by members of the community to my questions in this forum.

Items

Group gWindows
Contact LivingRoom_Window_A (gWindows) { channel="mqtt:topic:MyBroker:Home:Contact1" }
Contact LivingRoom_Window_B (gWindows) { channel="mqtt:topic:MyBroker:Home:Contact2" }
Switch LivingRoom_Radiator               { channel="mqtt:topic:MyBroker:Home:Radiator1" }
Contact Bathroom_Window (gWindows) { channel="mqtt:topic:MyBroker:Home:Contact3" }
Switch Bathroom_Radiator { channel="mqtt:topic:MyBroker:Home:Radiator2" }

Rules DSL Implementation

import org.eclipse.smarthome.model.script.ScriptServiceUtil

rule "(DSL) Turn off the radiator of a room if one its windows is open" 
when   
   Member of gWindows changed to OPEN
then
    // get the name of the room where the window-contact belongs
    val roomX = triggeringItem.name.split("_").get(0)  
    // build the name of the radiator based on the room's name
    val SwitchItem radiatorX = ScriptServiceUtil.getItemRegistry?.getItem(roomX+"_Radiator") as SwitchItem
    
    if(radiatorX.state == ON){
       // Turn off the radiatorX
      radiatorX.sendCommand(OFF)
    } 
end

Jython implementation

from core.rules import rule
from core.triggers import when

@rule("(Py) Turn off the radiator of a room if one its windows is open")
@when("Member of gWindows changed to OPEN")
def window_x_open(event):
    # get the name of the room where the window-contact belongs
    roomX = event.itemName.split("_")[0]  
    # build the name of the radiator based on the room's name
    radiatorX = roomX+"_Radiator"
    if items[radiatorX] == ON:
        # Turn off the radiatorX
        events.sendCommand(radiatorX, "OFF")

Happy automation!

Humberto

Note:

  • Suggestions or recommendations to improve the implementation are welcome!
  • Do you have more complex automations that shares the same logic of this example? Please share it :slightly_smiling_face:

Other automation examples

Related Posts

2 Likes