Beginner template problem

Hey guys,
so I’m new to work with templates, so have a question. I try something on the developer page and there is something i don’t undestand. I want to filter my entities and so I try some jinja filters, but the results are not as expected. So maybe it is obvious for you, but not for me.

My filter:
{{states.light | selectattr(‘state’, ‘eq’, ‘off’) | map(attribute=‘entity_id’) |list()}}
{{states.light | selectattr(‘state’, ‘eq’, ‘off’) | map(attribute=‘friendly_name’) | list()}}

The results:
[‘light.kuche’, ‘light.esszimmer’, ‘light.terrasse’, ‘light.eingangstur’, ‘light.schlafzimmer’, ‘light.sessel’, ‘light.couch’, ‘light.kuche_3’, ‘light.kuche_2’, ‘light.kuche_1’, ‘light.kuche_4’, ‘light.esstisch_1’, ‘light.esstisch_2’, ‘light.esstisch_3’, ‘light.terrasse_1’, ‘light.terrasse_2’, ‘light.eingang_rechts’, ‘light.eingang_links’]
[Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined]

The first result is as expected, but not the second. I gave all the entities a friendly name, but in the list the friendly names are undefined.

You can give me a little hint, please.

thank you in advance.

Firstly, please format code correctly for the forum. Either use the </> button or use three backticks before and after:

```
CODE GOES HERE
```

If you don’t do that, the forum software messes up quotes and indentation, and we can’t copy / paste your code for testing.

Your problem is caused by name being a direct property of the State object, but the friendly_name being in the attributes dictionary.

This is confusing when Jinja’s map filter uses attribute= — it’s a different use of the word “attribute”. You want this (line breaks optional just for ease of reading):

{{ states.light
   |selectattr('state','eq','off')
   |map(attribute='attributes.friendly_name')
   |list }}

Note I’ve asked for attributes.friendly_name in the map filter.

Thank you!
Next time, I will folllowyour instructions on the format.

1 Like

This will do also:

map(attribute="name")
2 Likes