A sensor to list unavailable devices

Add a rejectattr line, for example:

{{ states
| selectattr('domain','in',['binary_sensor', 'climate', 'light', 'sensor', 'switch'])
| selectattr('state', 'in', ['unavailable'])
| rejectattr('entity_id', 'in', 'your_entity_id_here') 
| map(attribute='entity_id')
| map('device_attr', 'name_by_user') 
| reject('match', 'None')
| unique
| list 
| sort 
| join('\n')
}}
1 Like

Thank you for the code!

How can I select specific entities to monitor?

I get that to exclude entities we can use “rejectattr(‘entity_id’, ‘in’, ‘your_entity_id_here’)”.

I only want to monitor a list of ~10 entities.

I guess there a few ways, but I would define the list of entities (here I called the list “unavail”) and then use this list the way we have in this post. I have five listed entities here, just change them to what you want:

{% set unavail = [
states.light.ollys_beam,
states.media_player.denon_amp,
states.sensor.thermpro_office_battery,
states.light.office_1,
states.light.office_lamp,
] %}
{{ unavail
| selectattr('state','eq','unavailable') 
| map(attribute='entity_id')
| map('device_attr', 'name_by_user') 
| reject('match', 'None')
| unique
| list 
| sort 
| join('\n')
}}

This works perfectly and so neat compared to some other solutions, thx!
Is there a way to display a ‘count’ of the number of devices found?

I worked it out 2mins after posting :man_facepalming:

{{ states
| selectattr('domain','in',['binary_sensor', 'climate', 'light', 'sensor', 'switch'])
| selectattr('state', 'in', ['unavailable'])
| rejectattr('entity_id', 'in', 'your_entity_id_here') 
| map(attribute='entity_id')
| map('device_attr', 'name_by_user') 
| reject('match', 'None')
| unique
| list 
| sort 

| count
}}

You don’t need the sort if you’re doing a count.

1 Like

Greetings.

I modified it to fit in an auto-entities card, by selecting one entity per unavailable device.

type: custom:auto-entities
filter:
  template: >
    {# Create list of unavailable entities #}
    {% set ent_list = [None] + (states
    | selectattr('state', 'in', ['unavailable'])
    | selectattr('domain','in', ['binary_sensor', 'climate', 'light', 'sensor', 'switch'])
    | map(attribute='entity_id')
    | list)
    %}
    {# For each unavailable entity, put it in dict with device name as key
       which results in one entity per device #}
    {% set tuples = dict( zip( ent_list | map( "device_name" ), ent_list )) | items | list
      | select(attribute=0)
      | list
    %}
    {# Print it, removing the first None line #}
    {{ dict(tuples[1:]).values()
      | sort 
      | join('\n')
    }}
1 Like