State attributes from entity_id stored in variable

Trying to search for all sensors with the word ‘Alarm’ in their name, where their state is not ‘Unknown’ or ‘Unavailable’. I then want to use a FOR loop to pull some attributes from the entity_ids that my search returns.

My FOR loop is working, and it successfully outputs the entity IDs and stores them as (‘e’), but I’m struggling then with passing the entity_ids therein into my the ‘state_attr’ query (or whatever the correct terminology is).

Here’s what I have:

{% for e in
states|selectattr(‘domain’,‘in’,[‘sensor’]) | rejectattr(‘state’,‘in’,[‘unknown’,
‘unavailable’])
| select(‘search’, ‘alarm’) | map(attribute=‘entity_id’)|list -%}

{{state_attr(“{{e}}”, “recurrence”) }}
{{state_attr(“{{e}}”, “prior_value”)| as_timestamp | timestamp_custom(‘%H:%M’) }}

{% endfor %}

If I just put {{ e }} in the loop, it returns the entity ID. If I hard-code the entity ID into my query, it returns the attributes I’m looking for. What gives?

The e is due to the for loop available as jinja variable, so you could use it within the for loop scope as any other variable

{% for e in
states|selectattr(‘domain’,‘in’,[‘sensor’]) | rejectattr(‘state’,‘in’,[‘unknown’,
‘unavailable’])
| select(‘search’, ‘alarm’) | map(attribute=‘entity_id’)|list -%}

{{state_attr(e, “recurrence”) }}
{{state_attr(e, “prior_value”)| as_timestamp | timestamp_custom(‘%H:%M’) }}

{% endfor %}

Hero. Thanks so much! Works like a charm.