Announce any time any door or window opened (like ADT)

I’ve seen a few ‘similar but not the same’ posts around so decided to aggregate all the various things I learned, through this community, here about how to achieve this in the hopes it could be useful for someone else. There may be simpler ways of doing this but this is the approach I settlend on.

The idea is that any time any “opening” is opened, an announcement is made (in my case with google tts to all living area speakers in the main house) stating which door/window was opened. There are similar posts which use the state change trigger of a group, however for multiple doors/windows being opened, this way will only announce the first door/window which changed the groups status.

Firstly create a Binary Sensor group in the helpers section and add all the door and window sensors to be monitored to this group. I’ve called mine “all_poolhouse_openings”. This group is a great way of monitoring multiple devices in multiple automations without having to trawl through and define every device individually in each automation anytime you add / move a device.

Now we want to monitor this group and return which of these have changed to opened and return the most recently (in the last 3 seconds) opened door or window.
Again in the helpers section create a Template Sensor (not a binary sensor). I’ve called mine “last_opened_poolhouse_opening”. Don’t need to set a unit of measure, device class or state class.
Add the below to the template.

{% set x = expand('binary_sensor.all_poolhouse_openings') | selectattr('state', 'eq', ['on','open']) | sort(attribute='last_changed', reverse=true) | list %}
{{ (x[0].entity_id if now() - x[0].last_changed < timedelta(seconds=3) else '') if x | count > 0 else '' }}

Go and open a door (or several) and the preview should now show the most recently opened entity (for a few seconds before resetting).

To create the announcement automation we set our trigger as the state change of the “last_opened_poolhouse_opening” from blank to anything, and retrieve the friendly name of the openeing and perform the announcement. Here is my full automation yaml.

alias: Announce Any Poolhouse Opening Opened
trigger:
  - platform: state
    entity_id:
      - sensor.last_poolhouse_opening_opened
    to: null
    from: ""
condition:
 action:
  - service: media_player.volume_set
    data:
      volume_level: 0.2
    target:
      entity_id: media_player.main_house_living_areas
    enabled: true
  - service: tts.google_translate_say
    data:
      cache: false
      entity_id: media_player.main_house_living_areas
      message: |-
        {% set open_openings = states | selectattr('entity_id', 'in',
            states('sensor.last_poolhouse_opening_opened')) | map(attribute='name') | list %}
            {% if open_openings | length == 1 %} {{ open_openings[0] }} opened. {% endif %}
mode: single

This will announce something along the lines of “Poolhouse wetroom door opened.”.

My next step is to check if the speaker group is already playing music and if so will leave the volume as it is. I only added the volume adjustment to avoid being screamed at by the speakers if we happened to be having a party previously and all the speakers are still set to a high volume.

1 Like