Template variable warning: 'str object' has no attribute 'entity_id' when rendering

Hi,

I am using the blueprint at: Report/Detect unavailable sensors and binary sensors (non)matching a regular expression · GitHub which has the following loop, that works fine, without any log-warnings, when run via Devtools==>templates: (Below example has added variables used in blueprint)

{% 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?

Post the full error and how you’re using it in the blueprint.

FYI, the small amount of the error that you’ve posted is saying that s is a string, not s.entity_id.

Secondly, you can reduce this by a lot.

{%- set exclude = [] -%}
{%- set exclude_regexp = "DontMatchMuch" -%}
{{ expand(states.sensor, states.binary_sensor) | selectattr('state', 'search', "unavail|unknown") | rejectattr('entity_id', 'in',  exclude) | rejectattr('entity_id', 'search', exclude_regexp) | map(attribute='entity_id') | list }}

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).

Ok, the error then is pointing to exclude.entity_id, which means exclude is a string. Are you only selecting 1 entity?

1 Like

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…

Now it works fine!