Jinja2 - How to get |list "value" for function?

Hi,

With unavailable-entities-sensor I can get all unavailable entities.
Now I would like to filter them “up” to devices.
So with

{{ state_attr('sensor.unavailable_entities','entity_id')|map('device_id')|unique|list }}

I can get the list of device IDs.
But how can I translate the device IDs to device names? My approach is

{{ state_attr('sensor.unavailable_entities','entity_id')|map('device_id')|unique|list|map(device_attr(??????,'name'))|list }}

But what is the “keyword” for the piped list value in Jinja?

Thanks in advance!!!

device_attr() accepts entity ID’s, so you can leave out that conversion:

{{ state_attr('sensor.unavailable_entities','entity_id') 
| map('device_attr', 'name') | unique | list }}
1 Like

map("device_attr","name") should do the job, so

{ state_attr('sensor.unavailable_entities','entity_id')|map('device_id')|unique|list|map("device_attr","name")|list }}

For other available device attributes, see What attributes do devices have?

list | map(f,arg]) generates a list by applying the function with the name f to every element of its input. It takes an element from the list as the first argument for f and the second from arg, so it generates [f(list[0],arg),f(list[1],arg),...]

Edit: Skipping the device_id altogether as suggested by @Didgeridrew is even better of course, but that’s how it would work to map to name form device id

Thank you for your answer!
That is the solution for my specific case. But just for interest and learning: Is there a kind of “value” keyword for piped lists/maps in Jinja?
I’m coming from Java/C#/Javascript, so honestly Jinja sometimes is kinda weird or at least unnecessary complex for me. But I guess it’s a philosophy thing.

Edit: I got the point that map('device_id') is calling the function device_id and passing the value entity_id(list results) as argument. Calling functions via String argument is definitely not part of my C-like philosophy. :sweat_smile:

No, the elements of the list in a | map(...) filter are the (first) argument to the function. map() applies a function to every element of a list:

Assume a function

add(a,b):
  return a+b

then you can do

[1,2,3] | map ('add',5) | list`

which turns into

[add(1,5),add(2,5),add(3,5)]

so, ultimately

[6,7,8]

(Functional programming is a weird thing to get the head wrapped around. :exploding_head:)

1 Like