Template sensor delayed update - can't figure out why?

Hey guys -

I have the following two template sensors:

    - name: "open_door_count"
      state: >
        {{ states | selectattr('domain', 'in', ['binary_sensor', 'cover']) | selectattr('state', 'in', ['on','open']) | selectattr('attributes.device_class', 'in', ['door','garage']) | rejectattr('entity_id', 'equalto', 'cover.waffles_charge_port_door') | map(attribute='name') | list | count }}

    - name: "open_door_alert_message"
      state: >
        {% set doors = states | selectattr('domain', 'in', ['binary_sensor', 'cover']) | selectattr('state', 'in', ['on','open']) | selectattr('attributes.device_class', 'in', ['door','garage']) | rejectattr('entity_id', 'equalto', 'cover.waffles_charge_port_door') | map(attribute='name') | list %}
        {% set qty = doors | count %}
        {% set p1 = 'are' if qty > 1 else 'is' %}
        {% if qty == 0 %} 
        No doors are open.
        {% else %}
        Alert: {{' and '.join((doors | join(', ')).rsplit(', ', 1))}} {{p1}} open.
        {% endif %}

For some reason, after I leave the garage and the doors are closed, the template values still show as though they are open. Here is the relevant history:

In it, you can see that even though garage 1i and 1o are closed, the template values still show as though they are open. 10-20 minutes later the templates update to reflect that they are closed, but I can’t figure out why the delay.

Any ideas? What am I missing?
Thanks,
Billy

Templates that access the entire states object or the states of an entire domain (like cover) are rate limited to update only once every minute.

Use groups to avoid this so that the template updates whenever a group member’s state changes.

1 Like

Oh! Roger that. I’ll give that a whirl. Thanks

Actually - just to check, is it possible to pipe a group of Covers and a group of binary_sensors together?

I implemented it this way because several doors come through as covers and some are just binary_sensors.

Is there a simple, single template using a cover group and a binary_sensor group that could achieve what I’m doing by looking for covers that are open or binary sensors that are on within the states object?

Thanks again for the help!

If you want optimized template entities, make a template entity that gathers the correct entities, and then feed that to another that dispositions off the state. This will give you live updates without caring about throttling.

template:
- triggers:
  - trigger: homeassistant
    event: start
  - platform: event
    event_type: event_template_reloaded
  variables:
    entities: >
      {{ states | selectattr('domain', 'in', ['binary_sensor', 'cover'])
                | selectattr('attributes.device_class', 'in', ['door','garage'])
                | map(attribute='entity_id')
                | reject('in', ['cover.waffles_charge_port_door'])
                | list }}
  sensor:
  - name: All Doors
    state: >
      {{ entities | count }}
    attributes:
      entity_id: "{{ entities }}"

- sensor:
  - name: "open_door_count"
    state: >
      {{ (states('sensor.all_doors', 'entity_id') or []) | select('is_state', ['on', 'open']) | list | count }}
    attributes:
      alert: >
        {% set entities = states('sensor.all_doors', 'entity_id') or [] %}
        {% set entities = entities | select('is_state', ['on','open']) | list %}
        {% set qty = entities | count %}
        {% set p1 = 'are' if qty  > 1 else 'is' %}
        {% if qty == 0 %} 
        No doors are open.
        {% else %}
        Alert: {{' and '.join((doors | join(', ')).rsplit(', ', 1))}} {{p1}} open.
        {% endif %}