Jinja2 Templating Groups

Hi All,
Trying to set up an Alexa Flash Briefing to say which devices have been left on.
Rather than list out each entity in a group is there a way to say
Is Light Group ON?
If so list ON entities from it.
If not “No Lights on”
Is Switches Group ON?
If so list ON entities from it.
If not “No Switches on”

So far only way i have managed it to then list each entity within a group.

            {% if (states.group.all_lights.state) == "on"  %}
            The following lights are currently on:
                {% if (states.light.dining_main.state) == "on" %}
                        Dining Main,
                {% endif %}
                {% if (states.light.sofa_main.state) == "on" %}
                        Sofa Main,
                {% endif %}
            {% else %}
            All lights Turned off.
            {% endif %}
            {% if is_state('group.all_lights', 'on')  %}
              The following lights are currently on:
              {{ expand('group.all_lights')
                  | selectattr('state', 'eq', 'on')
                  | map(attribute='name') | list | join(', ') }}
            {% else %}
              All lights Turned off.
            {% endif %}

EDIT

Another way to do the same thing:

            {% set lights_on = expand('group.all_lights')
                  | selectattr('state', 'eq', 'on')
                  | map(attribute='name') | list %}
            {{ 'All lights are turned off' if lights_on | count == 0
               else 'The following lights are currently on: ' ~ lights_on | join(', ') }}
3 Likes

Thank you every so much for swift response.
Both worked perfectly in the Developer Tools Template for tests, however oddly enough only the second way worked when actually included in an Alexa flash briefing.

This is great as has massively reduced the list needed for each group i have, really appreciated.

1 Like