Hi
I’m currently trying to build a template sensor (adapted from @jazzyisj’s unavailable-entities-sensor).
However, the template seems not to behave as I’d expect and I don’t understand why. Maybe someone here can point me in the right direction
I’m debugging the template in the Developer Tools and the code looks like this:
{% set entities = states
| rejectattr('attributes.device_class', 'undefined')
| selectattr('attributes.device_class', 'eq', 'battery') %}
{# print1: {{ entities | list }} #}
{% set ignored = state_attr('group.ignored_battery_levels', 'entity_id') %}
{% if ignored != none %}
{% set entities = (entities | rejectattr('entity_id', 'in', ignored)) %}
{% endif %}
{# print2: {{ entities | list }} #}
{% set low = entities
| map(attribute='state')
| map('int')
| select('<', 90)
| map('string')
| list %}
{# print3: {{ entities | list }} #}
{# print4: {{ low | list }} #}
{{ entities | selectattr('state', 'in', low) | map(attribute='entity_id') | list }}
Issue: the last line always prints an empty list
Debugging steps:
- If I uncomment comment 1 and list the entities, the correct list is printed out.
- If I then go ahead and uncomment comment 2, the printed list is empty.
- If then I comment out the print1 again, then suddenly the print2 part lists the correctly filtered entities.
- If I “disable” the print1 and print2 again and uncomment print3 the printed list is still empty. However, uncommenting print4 outputs 1 (correct) value from the low variable.
This leaves me wondering if the entities variable is kind of “consumed” as soon as list has been called once on it.