How to wildcard template entities?

I’m trying to modify the developer tools demo template. Original is:

{% for state in states.weather -%}
  {%- if loop.first %}The {% elif loop.last %} and the {% else %}, the {% endif -%}
  {{ state.name | lower }} is {{state.state_with_unit}}
{%- endfor %}.

I want to change “states.weather” to “states.sensor.pentair*” (pentair has several sub entities); is this possible? I tried using * but it doesn’t work.

All of the attributes of an entity are under the ‘attributes’ property. So to loop through them you could use:

{% for attr in states.sensor.pentair.attributes %}

This will just get the attribute names. If you want to get the values of the attributes at the same time, you could use:

{% for attr, value in states.sensor.pentair.attributes.items() %}

My problem is it’s not the word pentair. It’s pentair_4563_randomword. That’s why I need a wild card character to fetch all of the pentair names. Around thirty. Is this not possible?

For example:
[binary_sensor.pentair_f6_70_b3_chemistry_alarm]
[binary_sensor.pentair_f6_70_b3_scg_status]
etc. etc.

I need to retrieve multiple entities using a wildcard ‘*’ or something like that.

Maybe something like this:

{% for state in states.binary_sensor if 'pentair_' in state.name -%}
  {%- if loop.first %}The {% elif loop.last %} and the {% else %}, the {% endif -%}
  {{ state.name | lower }} is {{state.state_with_unit}}
{%- endfor %}.

It should pick up all the binary_sensor.pentair_* entities.

Awesome JT! Thank you I’ll try that.

{{ states.binary_sensor | selectattr('object_id', 'match', 'pentair_')
   | map(attribute='entity_id') | list }}
1 Like

Elegant Taras, thank you!

1 Like

Elegant, but apparently not enough to be your chosen solution. :wink:

I want to try both but felt I needed to close the discussion- let me work on this and thanks

No worries; you’re free to choose whatever you feel works best for you.