Open door/windows check and report via tts

God, I’m lost
I’ve been trying to get an automation check my binary sensors for my doors and windows at 10pm if they’re open or close and announce via notify.alexa_media which ones are open (or give it a ‘all clear’ if they’re closed).
I’ve obviously missing something, but I can’t get an ‘open’ notification.

Here’s the code:

alias: Check Doors and Windows at 10 PM
trigger:
  - platform: time
    at: "22:00:00"
condition: []
action:
  - variables:
      open_entities: >
        {%- set entities = [
          'binary_sensor.back_door',
          'binary_sensor.front_door',
          'binary_sensor.kitchen_door',
          'binary_sensor.living_room_patio_door',
          'binary_sensor.kitchen_window',
          'binary_sensor.study_window',
          'binary_sensor.utility_room_window',
          'binary_sensor.wc_window'
        ] -%}
        {%- set open_entities = entities | selectattr('state', 'eq', 'on') | list -%}
        {{ open_entities }}
  - choose:
      - conditions: "{{ open_entities | length > 0 }}"
        sequence:
          - service: notify.alexa_media
            data:
              target:
                - media_player.alexa_kitchen_2
                - media_player.bedroom
              message: >
                The following entities are open: 
                {{ open_entities | join(', ', attribute='entity_id') }}
      - conditions: "{{ open_entities | length == 0 or not open_entities }}"
        sequence:
          - service: notify.alexa_media
            data:
              target:
                - media_player.alexa_kitchen_2
                - media_player.bedroom
              message: "All windows and doors are closed"

Please help, why are the open sensors not changing the open_entities with the ones that are “on” status? It always return a value of 0

Have you seen what status the windows give when open? It may be “on” or “open” or even “in”

I think this may work:

selectattr(‘state’,‘in’,[‘on’,‘open’])

I think you are looking for the state, not the state of an attribute…
Maybe not…


        {%- set entities = [
          'binary_sensor.back_door',
          'binary_sensor.front_door',
          'binary_sensor.kitchen_door',
          'binary_sensor.living_room_patio_door',
          'binary_sensor.kitchen_window',
          'binary_sensor.study_window',
          'binary_sensor.utility_room_window',
          'binary_sensor.wc_window'
        ] -%}
        {%- set open_entities = entities | selectattr('state', 'eq', 'on') | list -%}
        {{ open_entities }}

Pull that into developer - template and play until you fix it.
Open your Home Assistant instance and show your template developer tools.

To quote Ziggy Stardust, I’m an absolute beginner, so I deeply apologise in advance for any lame comment.
This is what Github co-pilot tells me about this piece of code.

The provided code selection appears to be written in YAML syntax. YAML is a human-readable data serialization format commonly used for configuration files.
In this code selection, we have a function called selectattr being used. The selectattr function is typically used in YAML templates or filters to select specific attributes or values from a collection of objects.
The selectattr function takes three arguments: the attribute name, the operator, and the value to compare against. In this case, the attribute name is 'state', the operator is 'eq' (which stands for “equals”), and the value is 'on'.
What this code is doing is selecting objects that have the attribute 'state' equal to 'on'. It filters out any objects that do not meet this condition and returns a new collection with only the selected objects.

My reading of that is that selectattr is looking for the attribute ‘state’ of the binary_sensor. Is that a right assumption? I’m not sure how to understand your message to be honest :expressionless:

Checked and rechecked - is ‘on’ (open) and ‘off’ (closed)

I use this to message me which windows are open. My “downstairs windows” is a group helper.

  message: >
        {% set open_doors = states | selectattr('entity_id',
        'in',state_attr('binary_sensor.downstairs_windows','entity_id')) |
        selectattr('state','in',['on','open']) | map(attribute='name') | list %}
        {% if open_doors | length == 1 %} The {{ open_doors[0] }} is open. {%
        else %}
          The {{ open_doors[:-1] | join(' , ') }}{{' ,' if open_doors | length > 2 else ' '}} and {{ open_doors[-1]}} are open.
        {% endif %} 
1 Like

Thanks for this great piece of code!