Sensor for "Unavailable and Unknown" entities - template loop detected while processing event

Friends,
I am struggling to get this sensor stable and correctly working.
I have essentially 2 challenges:

  1. Error : “Template loop detected while processing event”
    Detailed log message:
Logger: homeassistant.helpers.template_entity
Source: helpers/template_entity.py:350
First occurred: 14:13:30 (2 occurrences)
Last logged: 14:13:30

    Template loop detected while processing event: <Event state_changed[L]: entity_id=sensor.unavailable_devices, old_state=<state sensor.unavailable_devices=4; unavail_list_entity_id=sensor.nr_of_updates, sensor.nr_of_low_batteries, sensor.afval_ophaling_vandaag_of_morgen, sensor.nr_of_calendar_events_next_5_days, friendly_name=Unavailable Devices @ 2023-08-22T14:11:30.739241+02:00>, new_state=<state sensor.unavailable_devices=1; unavail_list_entity_id=sensor.nr_of_updates, sensor.nr_of_low_batteries, sensor.afval_ophaling_vandaag_of_morgen, sensor.nr_of_calendar_events_next_5_days, friendly_name=Unavailable Devices @ 2023-08-22T14:12:30.744909+02:00>>, skipping template render for Template[{% set polled_domains = ['light', 'power', 'switch', 'binary_sensor', 'lock', 'sensor'] %} {% set polled_states = ['unavailable', 'unknown'] %} {% set reject_filter = ['sensor.unavailable_devices', 'binary_sensor.mba_aruba', 'binary_sensor.iphone_koen'] %} {{ states | selectattr('domain', 'in', polled_domains) | selectattr('state','in', polled_states) | rejectattr('entity_id', 'in', reject_filter) | rejectattr('attributes.friendly_name','search','Browser') | rejectattr('entity_id','search','screen') | map(attribute='entity_id') | list | count }}]
  1. This template sensor does not immediately update when any of the states is changed.
    This might be a consequence of the above error.

Any help is much appreciated !

My template sensor:

- sensor:
  - name: "Unavailable Devices"
    unique_id: 7d0c949e-c251-44d4-b535-96cbfefe2f7e
    state: >
      {% set polled_domains = ['light', 'power', 'switch', 'binary_sensor', 'lock', 'sensor'] %}
      {% set polled_states = ['unavailable', 'unknown'] %}
      {% set reject_filter = ['sensor.unavailable_devices', 'binary_sensor.mba_aruba', 'binary_sensor.iphone_koen'] %}
      {{ states 
          | selectattr('domain', 'in', polled_domains) 
          | selectattr('state','in', polled_states)
          | rejectattr('entity_id', 'in', reject_filter)          
          | rejectattr('attributes.friendly_name','search','Browser')
          | rejectattr('entity_id','search','screen')            
          | map(attribute='entity_id')
          | list 
          | count
      }}
    attributes:
      unavail_list_entity_id: >
        {% set polled_domains = ['light', 'power', 'switch', 'binary_sensor', 'lock', 'sensor'] %}
        {% set polled_states = ['unavailable', 'unknown'] %}
        {% set reject_filter = ['sensor.unavailable_devices', 'binary_sensor.mba_aruba', 'binary_sensor.iphone_koen'] %}
        {{ states 
            | selectattr('domain', 'in', polled_domains) 
            | selectattr('state','in', polled_states)
            | rejectattr('entity_id', 'in', reject_filter)          
            | rejectattr('attributes.friendly_name','search','Browser')
            | rejectattr('entity_id','search','screen')            
            | map(attribute='entity_id')
            | list 
            | join(', ') 
        }}

comparing the old_state to the new_state:

state sensors in attributes
old 4 4
new 1 4

something there maybe?


Edit: This may be why jazzyisj’s excellent package derives the sensor’s state from the attribute rather than repeating the same code with |count at the end? Perhaps try that and see if the problem goes away.


Edit 2: Actually, I think your problem is solved here, again, thanks to jazzyisj.

…had a bit of fun playing with yours and jazzyisj’s and came up with this which may be worth trying:

  - sensor:
      - name: "Unavailable Devices"
        unique_id: 7d0c949e-c251-44d4-b535-96cbfefe2f7e
        attributes:
          entity_id: >
            {% set polled_domains   = ['light', 'power', 'switch', 'binary_sensor', 'lock', 'sensor'] %}
            {% set polled_states    = ['unavailable', 'unknown'] %}
            {% set ignored_list = ['sensor.unavailable_entities','group.ignored_unavailable_entities'] %}
            {% set ignored_seconds = 60 %}
            {% set trigger_time =  (now().timestamp() - ignored_seconds)|as_datetime %}
            {{ states
              | selectattr('state','in', polled_domains)
              | selectattr('state','in', polled_states)
              | rejectattr('last_updated','gt',trigger_time)
              | rejectattr('entity_id', 'in', ignored_list)
              | map(attribute='entity_id')
              | list 
              | join(', \n')
            }}
        state: >
          {% set entities = state_attr(this.entity_id,'entity_id') %}
          {{ entities | count if entities != none else none }}

Note that, due to the use of now(), it will onlty update at the start of each minute.

It should work if you remove sensor.unavailable_devices from reject_filter, I would have thought.

Thx for the quick replies and support, John.
I will try this setup later today when back home.

@jchh John,
I tried the suggested template, with partial success.

  • The “attributes” work perfectly: i get a nice list of entity_ids
  • The “state” does not work: the sensor remains in state “unknown”, whereas a count of entities is expected.

If you have any idea how to troubleshoot, that would be greatly appreciated (as I am running out of ideas :wink:

Hi @Troon Troon,

I will give this a go.

However, I believe I added this “sensor.unavailable_devices” to the reject_filter in an attempt to avoid the loop error. I’ll keep you posted.

I did try removing it, but this does not resolve the loop error.