Hi, I’m trying to determine which persons are in a zone (when a automation is running) by checking the zone of their device_trackers. Now I was already able to print something when the device tracker is in the zone. Now I only want to determine whether at least one device is in a zone and then print the persons name. Unfortunately you cannot break out of the loop in jinja as far as I can tell.
{% for state in states.person -%}
{{ state.name }}
{% for attr in state.attributes['device_trackers'] -%}
{% if states(attr) == 'home' %} Home {% endif %}
{%- endfor %}
{%- endfor %}
Is there another way to do the check? There is a length filter, but that doesn’t work with the for-each loop and I haven’t found a way to count the length of a “sublist”. If it were in python it would look something like this:
for state in states.person:
if any(for attr in state.attributes['device_trackers'] if states(attr) == 'home'):
print(state.name)
state: >
{%- set people = expand('group.awesome_people') %}
{{ people | selectattr('state', 'in', ['home', 'on'] ) | list | count }}
Who’s in the house
state: >
{%- set people = expand('group.awesome_people') | selectattr('state', 'eq', 'home') | map(attribute='name') | list %}
{%- set company = expand('group.awesome_people') | selectattr('state', 'eq', 'on') | map(attribute='name') | list %}
{%- set people = people + company %}
{{ people }}