This template…
… seems to work as expected.
But this template…
…umm… doesn’t…?
How is that supposed to happen?
This template…
… seems to work as expected.
But this template…
…umm… doesn’t…?
How is that supposed to happen?
I’ve had similar things happen once or twice… refreshing the browser has fixed it every time.
Otherwise, I would probably skip the expand:
{% set dsn = state_attr('group.esphome_deep_sleep_nodes', 'entity_id') %}
{% set dsn_offline = dsn | reject('is_state', 'on') | list %}
{{ dsn_offline }}
No, refreshing the browser isn’t changing anything.
I only showed this in the template editor, because it’s convenient to capture the template together with the result in a screenshot.
The template is behaving the same way in a template sensor rendered in the backend.
I can reproduce this after a browser refresh, after deleting all cached data, in a private tab and even on devices, where that template was never used before. This is a screenshot from my phone:
How does it behave if you explicitly cast the variable as a list?
{% set dsn_offline = dsn | rejectattr('state', 'eq', 'on') | list %}
This seems to be the solution, although I still don’t know, why the output of a reject filter can’t be directly saved in a template variable, but has to be cast explicitly to a list.
In the template editor, the output of a select/reject is declared as <generator object select_or_reject at 0xabcd...>. Maybe a “generator object” is a pointer only meant to be read once by the next operation (i.e. the next filter) and discarded afterwards?
It’s a generator. It’s an object that only allows you to iterate once and it’s built for speed and optimization. The code only “handles” the objects you access, unlike a list where everything is loaded/handled.