Automation #5: Window Alert

Hi guys!

Another example :slightly_smiling_face:

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.

Automation #5: Window Opened Alert
Notify me when a window has been open for more than an hour

In this example, I will use the Expire Binding to fire a notification after an hour.


Contact Bathroom_Window (gWindows) { channel="mqtt:topic:MyBroker:Home:Contact3" }
Switch Bathroom_Window_Timer (gWindowTimers) { expire="1h,command=OFF" }
Contact Bedroom_Window_A (gWindows) { channel="mqtt:topic:MyBroker:Home:Contact4" }
Contact Bedroom_Window_B (gWindows) { channel="mqtt:topic:MyBroker:Home:Contact5" }
Switch Bedroom_Window_A_Timer (gWindowTimers) { expire="1h,command=OFF" }
Switch Bedroom_Window_B_Timer (gWindowTimers) { expire="1h,command=OFF" }

Rules DSL Implementation

rule "(DSL) Window open for more than an hour"
    Member of gWindows changed
    val sensor = triggeringItem
    // Get associated timer based on the item naming pattern
    val timer = gWindowTimers.members.findFirst[ t | =="_Timer" ] as SwitchItem

    if(sensor.state == OPEN) {
    } else {
        if(timer.state == ON  ) {

rule "Timer expired for a Window Contact Sensor"
    Member of gWindowTimers received command OFF
    // Send notification

Jython implementation

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

@rule("(Py) Window open for more than an hour")
@when("Member of gWindows changed")
def window_x_open(event):
    timerX = ir.getItem(event.itemName+"_Timer")
    if event.itemState == OPEN:
        #Set/Reset timer for event.itemName
        events.sendCommand(timerX, ON)
        if timerX.state == ON:
            # cancel timer
            events.postUpdate(timerX, OFF)

@rule("(Py) Timer expired for a Window Contact Sensor")
@when("Member of gWindowTimers received command OFF")
def timer_expired(event):
    # send notification

Happy coding!

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