Notify when Window or Door Open at 10pm

Hello, I’m trying to use the following automation to send a notification when any window/door is left open at 10pm. When I run the automation, I don’t recieve a notification. I think the way it’s written, all windows/doors must be open. How do I correct?

alias: Notify Door or Windows Open 10pm
description: ""
trigger:
  - platform: time
    at: "22:00:00"
condition:
  - condition: state
    entity_id:
      - binary_sensor.den_window
      - binary_sensor.dining_room
      - binary_sensor.family_room_window_1
      - binary_sensor.family_room_window_2
      - binary_sensor.front_door
      - binary_sensor.garage_interior_door
      - binary_sensor.garage_over_head_door
      - binary_sensor.garage_side_door
      - binary_sensor.kitchen_windows
      - binary_sensor.living_rm_1
      - binary_sensor.living_rm_2
      - binary_sensor.sliding_glass_door
    state: "on"
action:
  - service: notify.pushbullet_notify
    data:
      message: Check Doors and Windows
      title: Door and/or Windows Open
mode: single

Hi, you can use the OR building block and then add your conditions within that. Example snippet of just the Condition block:

condition:
  - condition: or
    conditions:
      - condition: state
        entity_id: binary_sensor.den_window
        state: "on"
      - condition: state
        entity_id: binary_sensor.dining_room
        state: "on"

Its likely possible to tighten this up and reduce the code size, but this should work fine…

Thanks so much, that did work. Now, any idea how I can include in the message the names of states that triggered?

Remove your conditions.

Under action, add a variable and list your doors and windows in the following template.

  - variables:
      open_items: >-
        {%- set ns = namespace(doors='') -%}
        {%- for door in ['binary_sensor.den_window',
                         'binary_sensor.dining_room',
                         'binary_sensor.family_room_window_1'] -%}
          {%- if states(door, 'state') == 'on' -%}
            {%- set ns.doors -%}
        {{ ns.doors }}
        {{ door }}
            {%- endset -%}
          {%- endif -%}
        {%- endfor -%}
        {{ ns.doors }}

You now have a variable called open_items that can be checked below. If open_items != '', do your notify and put open_items in your message.

ETA a full example. I just used some lights I have.

You’ll need to change to your binary sensors. And, in the replace, change 'light.' to 'binary_sensor.'.

Depending on the notification, you might need to change <br> to \n. But, certainly play with it to figure it out.

description: ""
mode: single
trigger:
  - platform: time
    at: "00:00:00"
condition: []
action:
  - variables:
      open_items: >-
        {%- set ns = namespace(doors='') -%}
        {%- for door in ['light.living_room_lamp',
                         'light.laundry_room_light_1',
                         'light.laundry_room_light_2'] -%}
          {%- if states(door, 'state') == 'on' -%}
            {%- set ns.doors -%}
        {{ ns.doors }}
        {{ door | replace('light.', '') }}<br>
            {%- endset -%}
          {%- endif -%}
        {%- endfor -%}
        {{ ns.doors }}
  - if:
      - condition: template
        value_template: "{{ open_items != '' }}"
    then:
      - service: persistent_notification.create
        metadata: {}
        data:
          title: Open Doors/Windows
          message: "{{ open_items }}"

Also, depending on your naming convention (door or window in each entity name … which you do not have right now), you could change the loop to select only those entities and never have to change this as you add new sensors (although, doubtful you will be adding new doors or windows :slight_smile: )

3 Likes

It worked!!! Thanks so much for your time and expertise!
The line breaks did work in the HA notification, but display <br> on the companion app. \n did the same. I’ll keep experimenting.

Shawn

1 Like