Complex window sensor automation

Hi,

I have 8 windows sensors and 3 Alexa Echos. I want to start a timer of 10m when I open a window and want Alexa to say “Window <window_name> is open for 10m”. If windows stays open, I want to repeat the check every 5m.

This basic automation is already working:

- id: '1614159378665'
  alias: Check Office Window
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.openclose_15
    to: 'on'
    for: 00:10:00
  condition: []
  action:
  - service: notify.alexa_media_kuche
    data:
      data:
        type: tts
      message: The office window is open for 10m.
  - service: notify.alexa_media_schlafzimmer
    data:
      data:
        type: tts
      message: The office window is open for 10m.
  - service: notify.alexa_media_bad
    data:
      data:
        type: tts
      message: The office window is open for 10m.
  mode: single

Issues I see here are:

  • I want to avoid, creating a new automation for each window.
  • Notification will fire when I close the window before the 10m timer runs out.
  • Need to notify each Alexa Echo separately.
  • How can I repeat the check, when the sensor is still open?

Thoughts:
I could use a group to check the state of all windows, but this would not give me specific names in my notification.

In a programmatically world I would start a timer of 10m. When the timer runs out, I would check all window states and return an array of open windows. Then I would loop through the array and fire the Alexa notifications for each devise. Then I’d run a 5m loop and repeat the check until the return array is empty.

For those who are interested, I got it to work:

I created a group in the groups.yaml for all window sensors:

all_windows:
  entities:
    - binary_sensor.openclose_20
    - binary_sensor.openclose_21
    - binary_sensor.openclose_19
    - binary_sensor.openclose_15
    - binary_sensor.openclose_23
    - binary_sensor.openclose_24
    - binary_sensor.openclose_16
    - binary_sensor.openclose_22

I created a timer in the configuration.yaml:

timer:
  timer_window:
    duration: "00:10:00"

I created an automation stat starts the timer, when any window is open.

- alias: Windows Open
  description: ''
  trigger:
  - platform: state
    entity_id: group.all_windows
    to: 'on'
  condition: []
  action:
  - service: timer.start
    data: {}
    entity_id: timer.timer_window
  mode: single

Another animation cancels the timer:

- id: '1614244148124'
  alias: Windows Close
  description: ''
  trigger:
  - platform: state
    entity_id: group.all_windows
    to: 'off'
  condition: []
  action:
  - service: timer.cancel
    data: {}
    entity_id: timer.timer_window
  mode: single

I created a script, that handles the notification:

notify_window_open:
  alias: Notify Window Open
  sequence:
  - service: media_player.volume_set
    data:
      volume_level: "0.9"
    entity_id: media_player.bathroom, media_player.sleeping_room, media_player.kitchen
  - service: notify.alexa_media_bathroom
    data:
      data:
        type: tts
      message: "Window {% if is_state(\"binary_sensor.openclose_15\", \"on\") -%}\n\
        \  Office 1,\n{%- endif %} {% if is_state(\"binary_sensor.openclose_16\"\
        , \"on\") -%}\n  Bedroom,\n{%- endif %} {% if is_state(\"binary_sensor.openclose_19\"\
        , \"on\") -%}\n  Office 2,\n{%- endif %} {% if is_state(\"binary_sensor.openclose_20\"\
        , \"on\") -%}\n  Bathroom,\n{%- endif %} {% if is_state(\"binary_sensor.openclose_21\"\
        , \"on\") -%}\n  Bathroom Guest,\n{%- endif %} {% if is_state(\"binary_sensor.openclose_22\"\
        , \"on\") -%}\n  Living Room,\n{%- endif %} {% if is_state(\"binary_sensor.openclose_23\"\
        , \"on\") -%}\n  Kitchen 1,\n{%- endif %} {% if is_state(\"binary_sensor.openclose_24\"\
        , \"on\") -%}\n  Kitchen 2\n{%- endif %} open since 10 minutes..\n"
  - service: notify.alexa_media_sleeping_room
    data:
      data:
        type: tts
      message: "Window {% if is_state(\"binary_sensor.openclose_15\", \"on\") -%}\n\
        \  Office 1,\n{%- endif %} {% if is_state(\"binary_sensor.openclose_16\"\
        , \"on\") -%}\n  Bedroom,\n{%- endif %} {% if is_state(\"binary_sensor.openclose_19\"\
        , \"on\") -%}\n  Office 2,\n{%- endif %} {% if is_state(\"binary_sensor.openclose_20\"\
        , \"on\") -%}\n  Bathroom,\n{%- endif %} {% if is_state(\"binary_sensor.openclose_21\"\
        , \"on\") -%}\n  Bathroom Guest,\n{%- endif %} {% if is_state(\"binary_sensor.openclose_22\"\
        , \"on\") -%}\n  Living Room,\n{%- endif %} {% if is_state(\"binary_sensor.openclose_23\"\
        , \"on\") -%}\n  Kitchen 1,\n{%- endif %} {% if is_state(\"binary_sensor.openclose_24\"\
        , \"on\") -%}\n  Kitchen 2\n{%- endif %} open since 10 minutes..\n"
  - service: notify.alexa_media_kitchen
    data:
      data:
        type: tts
      message: "Window {% if is_state(\"binary_sensor.openclose_15\", \"on\") -%}\n\
        \  Office 1,\n{%- endif %} {% if is_state(\"binary_sensor.openclose_16\"\
        , \"on\") -%}\n  Bedroom,\n{%- endif %} {% if is_state(\"binary_sensor.openclose_19\"\
        , \"on\") -%}\n  Office 2,\n{%- endif %} {% if is_state(\"binary_sensor.openclose_20\"\
        , \"on\") -%}\n  Bathroom,\n{%- endif %} {% if is_state(\"binary_sensor.openclose_21\"\
        , \"on\") -%}\n  Bathroom Guest,\n{%- endif %} {% if is_state(\"binary_sensor.openclose_22\"\
        , \"on\") -%}\n  Living Room,\n{%- endif %} {% if is_state(\"binary_sensor.openclose_23\"\
        , \"on\") -%}\n  Kitchen 1,\n{%- endif %} {% if is_state(\"binary_sensor.openclose_24\"\
        , \"on\") -%}\n  Kitchen 2\n{%- endif %} open since 10 minutes..\n"
  - service: media_player.volume_set
    data:
      volume_level: "0.5"
    entity_id: media_player.bathroom, media_player.sleeping_room, media_player.kitchen
  mode: single

Then there is the automation that fires, when the timer finishes:

  alias: Fenster-Erinnerung
  description: ''
  trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.timer_window
  condition:
  - condition: state
    entity_id: group.all_windows
    state: 'on'
  action:
  - service: script.notify_window_open
    data: {}
    entity_id: media_player.bathroom
  - service: timer.start
    entity_id: timer.timer_window
    data: {}
  mode: single

How is it working? When the timer starts and all windows close before the timer finishes, it gets canceled and no notification will be send.
When the timer finishes, and windows are still open. the notification will be fired and the timer will be restarted and this will happen until every window is close.

I am looking forward for your ideas and improvements.
What I will implement next, is a boolean switch “Window Check” as second condition for the notification. Thus I am able to disable the window check. I will add a second timer, that will enable the disable function after 2 hours, so that I don’t forget to re-enable the check.

1 Like