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.

Items

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"
when
    Member of gWindows changed
then 
    val sensor = triggeringItem
    // Get associated timer based on the item naming pattern
    val timer = gWindowTimers.members.findFirst[ t | t.name == sensor.name+"_Timer" ] as SwitchItem

    if(sensor.state == OPEN) {
        timer.sendCommand(ON) 
    } else {
        if(timer.state == ON  ) {
            timer.postUpdate(OFF)
        }
    } 
end

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

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)
    else:
        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!
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