Templating - Iterate on group defined through the UI

Hello,

I’m trying to iterate on a group of sensors (mainly doors/windows). It looks like it should be a quite straightforward things to do…but I cannot get it right.

For exemple I found this post.

But when I try something like this:

{% for state in states.group.aperture.attributes.entity_id -%}
    {{ state }}
{%- endfor %}

I get errors:

UndefinedError: 'None' has no attribute 'attributes'

It has to be know that ‘aperture’ is a group helper. While typing ‘aperture’, the tools suggest me another group that I created in the configuration.yml file. With this other group, my code works. So the question is: can iteration on group be done with group helper? And how?

Thanks in advance.

The link you posted is to a topic that’s 5 years old. It predates the introduction of Group Helpers.

A Group Helper entity created via the UI is not part of the group domain. For example, if you used the UI to create a Binary Sensor Group entity, its domain will be binary_sensor (not group). That means its entity_id is probably binary_sensor.aperture (check Developer Tools > States to confirm it).

To iterate through its members, you can do this:

{% for e in expand('binary_sensor.aperture') %}
  {{ e.name }} {{ e.state }}
{% endfor %}

That’s because your ‘other group’ is what is now known as a legacy or old-style Group (which is all that existed at the time of the linked topic you posted). It is a part of the group domain and you can use the same template I posted above to iterate through its members.

{% for e in expand('group.your_other_group') %}
  {{ e.name }} {{ e.state }}
{% endfor %}
1 Like

Thanks a lot for clarifying everything. Now it works for me.

1 Like