{% set exclude = namespace(entity_id=[]) -%}
{%- set exclude_regexp = "DontMatchMuch" -%}
{%- set result = namespace(sensors=[]) -%}
{%- for s in states.sensor -%}
{%- if s.state is search("unavail|unknown") and (not s.entity_id in exclude.entity_id and not s.entity_id is search(exclude_regexp)) -%}
{%- set result.sensors = result.sensors + [s.entity_id] -%}
{% endif -%}
{%- endfor -%}
{%- for s in states.binary_sensor -%}
{%- if s.state is search("unavail|unknown") and (not s.entity_id in exclude.entity_id and not s.entity_id is search(exclude_regexp)) -%}
{%- set result.sensors = result.sensors + [s.entity_id] -%}
{%- endif -%}
{%- endfor -%}
{{result.sensors}}
But when I run it in an automation, as a blueprint, I get the WARNINGS:
2022-06-25 13:30:00 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: ‘str object’ has no attribute ‘entity_id’ when rendering ‘{%- set result = namespace(sensors=) -%} {%- for s in states.sensor -%}
{%- if s.state is search(“unavail|unknown”) and (not s.entity_id in exclude.entity_id and not s.entity_id is search(exclude_regexp)) -%}
{%- set result.sensors = result.sensors + [s.entity_id] -%}
{% endif -%}
{%- endfor -%} {%- for s in states.binary_sensor -%}
{%- if s.state is search(“unavail|unknown”) and (not s.entity_id in exclude.entity_id and not s.entity_id is search(exclude_regexp)) -%}
{%- set result.sensors = result.sensors + [s.entity_id] -%}
{%- endif -%}
{%- endfor -%} {{result.sensors}}’
And I cant figure out why… How can s.entity_id be an string all of a sudden? Isnt templates parsed the same in the different contexts?
That IS the full error. I am sure it can be superbly re-written for those who are experts in jinja, but I am not.
And you’ve got access to the full blueprint.
The only added info when I created an automation of that BP, is that exclude_regexp is set to a long “xxx|yyy|zzz” string, and that exclude is set to a single entity-value (since the multiple parameter doesnt seem to work for my selector, no matter what I do).
Aaaaa… I’ve been so stuck at focusing on the state object, I didnt realize that the exclude also used an entity_id attribute!
And when I run in manually in devtools, the exlude.entity_id is more…correct.
I havent solved it yet, but thanks for pointing me in the right direction.
Yes, I am only selecting a single entry because I havent managed to get multiple to work with selectors…although, I think I found a bugg in the bp there as well, since the exclude part, tried to use a “target” selector initially…
So, fixed the blueprints badly imported exclude parameter. It used to be based on a target selector, and the default value filled in a an object containing an entity_id LIST.
The current entity: selector, just creates a plain list… and then of course the loop was checking for exclude.entity_id, when it should just be checking in exclude.
Thanks to @pedro for helping my sloppy, midsummer-party-hungover mind…