"Template loop detected while processing event" when calculating number of unvailable entities

I am trying to calculate number of entities which have “unavailable” state:

template:
  - sensor:
      - name: strange_unavailables
        icon: mdi:null
        state_class: measurement
        state: >-
          {% set COUNT = states | rejectattr('domain','eq','update')
                                | rejectattr('entity_id','search','strange_unavailables')
                                | selectattr('state','eq','unavailable')
                                | list | count -%}
          {{ COUNT }}

Sometimes I am getting this warning:

2023-04-20 04:19:16.405 WARNING (MainThread) [homeassistant.helpers.template_entity] Template loop detected while processing event: <Event state_changed[L]: entity_id=sensor.strange_unavailables, old_state=<state sensor.strange_unavailables=59; state_class=measurement, icon=mdi:null, friendly_name=strange_unavailables @ 2023-04-20T04:17:09.645658+03:00>, new_state=<state sensor.strange_unavailables=57; state_class=measurement, icon=mdi:null, friendly_name=strange_unavailables @ 2023-04-20T04:18:09.662040+03:00>>, skipping template render for Template[{% set COUNT = states | rejectattr(‘domain’,‘eq’,‘update’)
| rejectattr(‘entity_id’,‘search’,‘strange_unavailables’)
| selectattr(‘state’,‘eq’,‘unavailable’)
| list | count -%}
{{ COUNT }}]

Looks like when calculating a value, the “sensor.strange_unavailables” is somehow involved.
Can anyone tell me how can I improve my template to avoid these warnings?

Also, may be this warning comes from enumerating “states” - regardless of presence of “rejectattr('entity_id','search','strange_unavailables')”.

Since this is a “warning” and a corr. value seems to be defined properly - surely I may not care about this warning. But I wonder is it possible to get rid of it… (do not advise me to filter it in logger)

Thats because “This template listens for all state changed events.”
That means every state_changed event produces render this template. Even when happen state changes of your sensor-template, there is always 'event: state_changed entity_id=‘sensor.strange_unavailable’ - that triggering the re-rendering of himself.
You need to make Trigger Based Template Sensor with another trigger, I use time pattern.
For example, this my variant:

- trigger:
    - platform: time_pattern
      seconds: "/1"
  sensor:
    - name: "Unavailable"
      unique_id: "unavailable"
      icon: 'mdi:close-thick'
      unit_of_measurement: "objects"
      state: >
        {{ states | selectattr('state','in',['unavailable'])  # include unavailable states
                  | rejectattr('entity_id','in','sensor.unavailable')  # exclude self from count
                  | rejectattr('entity_id','in', state_attr('group.unavailable_ignore','entity_id'))  #this is group with entities for exclude from count
                  | rejectattr('domain','in',['automation', 'script', 'scene', 'update']) #excluded domains
                  | list | count }}

I had the same error in my template binary_sensor, with listen for binary_sensor domain.

    - name: 'Moistures'
      unique_id: 'moistures'
      device_class: moisture
      state: |
        {{ 'on' if 'on' in states.binary_sensor 
          | selectattr('entity_id','search','_moisture\Z') 
          | map(attribute='state') 
          | list else 'off' }}

Any event state_changed for binary_sensor domain entity produce this binary_sensor-template update.
I changed domain of my template from binary_sensor to sensor:, and my warnings gone. Because event state_changed for sensor domain is not listened by that template.

P.S.: Thanks for understanding my english.