Get list of unavailable devices in a template

I want an automation that when triggered sends me a notification with a list of lights that are in the unavailable state. I use this when I leave the house to get an idea of which smart lightbulbs might be physically powered off at the switch.

Currently using this and it gives me a list of entity_ids, but is there a way of getting the friendly names ?

{{ 
  expand(states.light) 
  |selectattr('state', 'eq', 'unavailable') 
  |map(attribute='entity_id')
  |join(',')  
}}

Use name in the map instead of entity_id.

1 Like

Or you might take a look at the HACS integration Watchman.

That worked. thanks. I tried with using ‘friendly_name’ and that didn’t work.

Thanks. This looks interesting for a number of other reasons… Might be a bit too much for what I am doing with this specifically. I want a very simple notification that I can read on my watch.

You could also have used map(attribute='attributes.friendly_name'). See the State object documentation:

It’s a bit confusing, but the State object has a name property which is referred to as an attribute in the map filter; and it also has an optional attributes dictionary which you can drill into as shown above.

name is safer to use as it’s guaranteed to be present.

1 Like