Problem with my automation to check which window is open

Hello,

I am currently trying to create an automation that tells me which window is still open when I open my front door.
The id’s and also the “on” is correct but still the array remains empty.

I just can’t get any further. Maybe someone else has a tip for me?

Here is my code:

alias: Fenster offen beim Öffnen der Haustür
description: >-
  Prüft offene Fenster, wenn die Haustür geöffnet wird, und gibt eine Ansage
  aus.
variables:
  fenster_kontakte: # Die Fensterliste wird hier global für die Automatisierung definiert
    - entity_id: binary_sensor.badezimmer_sensor_offnung
      name: "Badezimmer Sensor"
    - entity_id: binary_sensor.schlafzimmer_sensor_offnung
      name: "Schlafzimmer Sensor"
    - entity_id: binary_sensor.terrassentur_sensor_offnung
      name: "Terrassentür Sensor"
    - entity_id: binary_sensor.schiebetur_sensor_offnung
      name: "Schiebetür Sensor"

trigger:
  - platform: state
    entity_id: binary_sensor.deine_haustuer_entitaet # <--- HIER DEINE HAUSTÜR ENTITY_ID EINFÜGEN!
    from: 'off'
    to: 'on'
conditions: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: >
              {% set open_windows_temp = [] %}
              {% for window in fenster_kontakte %}
                {% if is_state(window.entity_id, 'on') %}
                  {% set open_windows_temp = open_windows_temp + [window.name] %}
                {% endif %}
              {% endfor %}
              {{ open_windows_temp | length > 0 }}
        sequence:
          - service: tts.cloud_say
            data:
              cache: false
              entity_id: media_player.hub_buro # <--- HIER DEINEN MEDIAPLAYER EINFÜGEN!
              message: >
                {% set open_windows_temp = [] %}
                {% for window in fenster_kontakte %}
                  {% if is_state(window.entity_id, 'on') %}
                    {% set open_windows_temp = open_windows_temp + [window.name] %}
                  {% endif %}
                {% endfor %}

                {% if open_windows_temp | length == 1 %}
                  Achtung, das {{ open_windows_temp[0] }} ist noch offen.
                {% elif open_windows_temp | length > 1 %}
                  Achtung, folgende Fenster sind noch offen:
                  {% for i in range(open_windows_temp | length) %}
                    {% if i == open_windows_temp | length - 1 and open_windows_temp | length > 1 %}
                      und das {{ open_windows_temp[i] }}
                    {% else %}
                      {{ open_windows_temp[i] }}{% if i < open_windows_temp | length - 2 %},{% endif %}
                    {% endif %}
                  {% endfor %}.
                {% endif %}
      - conditions: []
        sequence:
          - service: tts.cloud_say
            data:
              cache: false
              entity_id: media_player.hub_buro # <--- HIER DEINEN MEDIAPLAYER EINFÜGEN!
              message: > # <--- HIER DAS ">" HINZUFÜGEN!
                {% set open_windows_temp = [] %}
                {% for window in fenster_kontakte %}
                  {% if is_state(window.entity_id, 'on') %}
                    {% set open_windows_temp = open_windows_temp + [window.name] %}
                  {% endif %}
                {% endfor %}
                Alle Fenster sind geschlossen. Die Länge ist {{ open_windows_temp | length }}. Das ist super.
mode: single

the comments in the code are in German.

There are a couple issues…

  1. It is best not to use in-line comments, put them on the line above or below. If you are using the UI editor, there is no reason to use them at all since they will not be saved. Use aliases instead.
  2. NEVER place a comment after a multi-line indicator like > or |, it will break your template. If you need to comment in a template, you must use the Jinja comment delimiters {# #}.
  3. The way you have set up your loops will not work… to extract data out a loop’s local scope in Jinja you would need to use a namespace.

However, HA’s template engine has grown a lot in the past couple years and loops are not necessary for any of what you are doing, you can just use the built-in filters. If you define your variables before the Choose action, you do not need to repeat the same templates over and over:

alias: Fenster offen beim Öffnen der Haustür
description: >-
  Prüft offene Fenster, wenn die Haustür geöffnet wird, und gibt eine Ansage
  aus.
variables:
  fenster_kontakte: 
  # Die Fensterliste wird hier global für die Automatisierung definiert
    - entity_id: binary_sensor.badezimmer_sensor_offnung
      name: "Badezimmer Sensor"
    - entity_id: binary_sensor.schlafzimmer_sensor_offnung
      name: "Schlafzimmer Sensor"
    - entity_id: binary_sensor.terrassentur_sensor_offnung
      name: "Terrassentür Sensor"
    - entity_id: binary_sensor.schiebetur_sensor_offnung
      name: "Schiebetür Sensor"

trigger:
  - platform: state
    entity_id: binary_sensor.deine_haustuer_entitaet 
    # --- HIER DEINE HAUSTÜR ENTITY_ID EINFÜGEN!
    from: 'off'
    to: 'on'
conditions: []
action:
  - variables:
      open_window_ents: |
        {{ fenster_kontakte | map(attribute='entity_id') 
        | select('is_state', 'on') | list }}
      open_window_temp: |
        {{ fenster_kontakte | selectattr('entity_id', 'in', open_window_ents) 
        | map(attribute='name') | list }}
      open_window_count: "{{ open_window_ents | count }}"
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ open_window_count > 0 }}"
        sequence:
          - service: tts.cloud_say
            data:
              cache: false
              entity_id: media_player.hub_buro 
              # --- HIER DEINEN MEDIAPLAYER EINFÜGEN!
              message: >
                {%- if open_window_count == 1 %}
                  Achtung, das {{ open_window_temp[0] }} ist noch offen
                {%- else %}
                  Achtung, folgende Fenster sind noch offen:
                  {{' und '.join((open_window_temp | join(', ')).rsplit(', ', 1))}}
                {%- endif %}.
      - conditions:
          - condition: template
            value_template: "{{ open_window_count == 0 }}"
        sequence:
          - service: tts.cloud_say
            data:
              cache: false
              entity_id: media_player.hub_buro 
              # --- HIER DEINEN MEDIAPLAYER EINFÜGEN!
              message: > 
                Fenster sind geschlossen. Die Länge ist {{ open_window_count }}. Das ist super.
mode: single

Hey, thank you very much! That works well, it recognizes that one or more windows are open.

But I think there is still a small error with the open_windows_temp. Either this value is empty or the output cannot speak out this value.

Do you have any ideas here?

Check the automation Trace. Click on the symbol in the graph for the variables action …

image

Under the “Changed Variables” section it should list the output for each of the templates. What does it show.

Never mind that, I found the problem. I had a typo in one of the variables… I’ve fixed it in my previous post above.

Works like a charm!

Thank you very much!!!