State attribute is not retrieved when using a for loop

Hi,

I’m stuck on creating a for loop that retrieves multiple names from the attributes of a sensor.

If I do the following:

{% for faces in state_attr('image_processing.microsoftface_ip_webcam','faces') %}
{%- if loop.first %} {% elif loop.last %} and {% else %}, {% endif -%}
{{ states.image_processing.microsoftface_ip_webcam.attributes.faces }}
{% endfor %}

I get:

[{'name': 'Carmen', 'confidence': 58.236}, {'name': 'Michiel', 'confidence': 54.443}]
 and [{'name': 'Carmen', 'confidence': 58.236}, {'name': 'Michiel', 'confidence': 54.443}]

Which makes sense. But since I want the name I add the .name to the end:

{% for faces in state_attr('image_processing.microsoftface_ip_webcam','faces') %}
{%- if loop.first %} {% elif loop.last %} and {% else %}, {% endif -%}
{{ states.image_processing.microsoftface_ip_webcam.attributes.faces.name }}
{% endfor %}

But then no names are returned:


and 

My goal is to have both the names displayed. e.g. Carmen and Michiel

Could someone help me figure out what I’m am doing wrong?

ps I can access the seperate name values {{states.image_processing.microsoftface_ip_webcam.attributes.faces[0].name }}, but since the list doesn’t always have the same amount of values, I would like to create it dynamically.

I’ve gotten some progress by mapping an attribute to a list :

{{ state_attr('image_processing.microsoftface_ip_webcam', 'faces') | map(attribute='name') | list | join(' and ') }}

But, this only works when there are one or two persons detected, otherwise you will get e.g. John and Maria and Margret instead of John, Maria and Marget

Anyone got any ideas to point me in the right direction?

To display a list of names in one line, you need to do this

image

{{ state_attr('image_processing.detect_face_eufy_camera','faces') | selectattr('faces','!=','name')| map(attribute='name') | join(', ') }}

For more information, you can read here

Текст на русском (Text in Russian)

Чтобы отображался список имен в одну строку, нужно сделать вот так
{{ state_attr('image_processing.detect_face_eufy_camera','faces') | selectattr('faces','!=','name')| map(attribute='name') | join(', ') }}

Более подробно можно почитать здесь

1 Like