Condition with Template loop

Hi,
I want a automation condition which enumerates not closed windows:

id: testnotification
  initial_state: 'on'
  trigger:
  - platform: state
    entity_id: group.anwesenheit_alle
    to: 'off'
  condition:
  - condition: template
    value_template: >-
      {% for item in states.group.fenster_und_tueren.attributes.entity_id if states(item) == 'closed' or states(item) == 'off' %}
        {% if loop.first %}
          {{loop.length}} < 13
        {% endif %}
      {% endfor %}
  action:
  - service: notify.ios_iphone
    data_template:
      message: testmessage

the enumeration is working correct, but the mathematical operation “<13” is not working. Any hints how to solve it?

anyone? :slight_smile:

Did you eventually figure this out?
The < 13 should be within the template brackets.
Also, thanks for this example, it solved a problem I was having :smiley:

@nicx This is the only way to do this, and it’s not a simple template:

  condition:
  - condition: template
    value_template: >-
      {{ states | selectattr('entity_id', 'in', states_attr('group.fenster_und_tueren', 'enitty_id')) | selectattr('state','in', ['closed','off']) | list | length < 13 }}

If you need help understanding it:

states is the object that has all home assistant state objects.

A state object is any domain.name device, for example light.living_room.

selectattr() is a function that allows you to select objects based on an attributes value.

the list filter turns the selectattr output (which is a generator object) into a list.

the length filter gets the count of objects in the list.

So in the example above:

the first selectattr: selectattr('entity_id', 'in', states_attr('group.fenster_und_tueren', 'enitty_id')) is searching for entity_id’s inside the state object that are inside the group group.fenster_und_tueren

The second selectattr: selectattr('state','in', ['closed','off']) is selecting searching for all states that are off / closed.

The list converts the selectattr generator object into a list

The length gets the length of the list

The > 13 verifys the list is greater than 13. (Not sure why he chose 13).

Thanks for the example and explanation, it really helped me out!

There were some typos in the example, so here is a similar example without the typos. In case anyone else finds this useful.

"{{ states | selectattr('entity_id', 'in', state_attr('group.colortemplights', 'entity_id')) | selectattr('state', 'equalto', 'on') | map(attribute='entity_id') | join(', ')}}"

What it does: show a comma separated list of entity-id’s for all lights from a certain group, that are presently turned on.

1 Like

yes, cool it is. close to what I use my scripts:

  light_notification:
    alias: 'Light notification'
    sequence:
      service_template: >
        {{ notify_entity_id }}
      data_template:
        message: >
          {% set lights_on = states.light |
             selectattr('entity_id','in',state_attr('group.all_inside_lights','entity_id'))| 
             selectattr('state','eq','on') | map(attribute='name') | list %}
          {% if lights_on | length == 0 %}
            No lights on. Sleep well..
          {% elif lights_on | length == 1 %}
            The {{ lights_on[0] }} light is on.
          {% elif lights_on | length == 2 %}
            The {{ lights_on[0] }} and {{ lights_on[1] }} lights are on.
          {% else %}
            The {{ lights_on[:-1] | join(', ') }}, and {{ lights_on[-1] }} lights are on.
          {% endif %}