OK - I’ve messed about with it and simplified it all down and I’ve come up with the following:
sensor:
- platform: template
sensors:
unavailable_entities:
entity_id: sensor.time
friendly_name: Unavailable Entities
value_template: >
{% set count = states|selectattr('state', 'in', ['unavailable', 'unknown', 'none'])
|rejectattr('entity_id', 'in', state_attr('group.entity_blacklist', 'entity_id'))
|rejectattr('entity_id', 'eq' , 'group.entity_blacklist')
|rejectattr('entity_id', 'eq' , 'group.entity_whitelist')
|map(attribute='name')|list|length %}
{%- set devices = state_attr('group.entity_whitelist', 'entity_id') -%}
{%- set dev = namespace(value=0) -%}
{%- for entity_id in devices -%}
{%- if states(entity_id) in ['unavailable', 'unknown', 'none'] -%}
{%- set dev.value = dev.value + 1 -%}
{%- endif -%}
{%- endfor -%}
{{ count + dev.value }}
attribute_templates:
entities: >
{% set entities = states|selectattr('state', 'in', ['unavailable', 'unknown', 'none'])
|rejectattr('entity_id', 'in', state_attr('group.entity_blacklist', 'entity_id'))
|rejectattr('entity_id', 'eq' , 'group.entity_blacklist')
|rejectattr('entity_id', 'eq' , 'group.entity_whitelist')
|map(attribute='name')|list|join(', ') %}
{%- set whitelisted = state_attr('group.entity_whitelist', 'entity_id') -%}
{%- set dev = namespace(value=0) -%}
{%- for entity_id in whitelisted -%}
{% if states(entity_id) in ['unavailable', 'unknown', 'none'] %}
{%- if not dev.value == 0 %}{{- ', ' -}}
{% endif %}
{%- set dev.value = dev.value + 1 -%}
{{- entity_id -}}
{% endif -%}
{% endfor %}
{%- if not dev.value == 0 %}{{- ', ' -}}{% endif %}
{{- entities -}}
group:
# Whitelist contains entities that you expect to be there, that may not appear if the integration isn't ready on startup
entity_whitelist:
- light.attic
- light.bath
- light.bedroom
- light.boys
- light.hall
- light.landing
- light.kitchen
- light.led1
- light.led2
- light.stairs
- binary_sensor.attic_motion
- binary_sensor.bathroom_motion
- binary_sensor.hall_motion
- binary_sensor.kitchen_motion
- binary_sensor.landing_motion
- switch.heater
- switch.xmas_lights
- switch.thermostat
# Blacklist contains sensors that you don't care if they're unavailable - should contain the sensor itself!!
entity_blacklist:
- sensor.unavailable_entities
- sensor.hacs
- sensor.bedroom_next_alarm
- sensor.bedroom_next_reminder
- sensor.bedroom_next_timer
- sensor.dark_sky_precip
- sensor.livingroom_next_alarm
- sensor.livingroom_next_reminder
- sensor.livingroom_next_timer
- sensor.this_device_next_reminder
- switch.bedroom_repeat_switch
- switch.bedroom_shuffle_switch
- switch.everywhere_repeat_switch
- switch.everywhere_shuffle_switch
- switch.livingroom_repeat_switch
- switch.livingroom_shuffle_switch
- switch.this_device_repeat_switch
- switch.this_device_shuffle_switch
This state of sensor.unavailable_entities is a count of all the unavailable entities that ARE in the whitelist, that AREN’T in the blacklist, and that are not on either list but are showing as unavailable (or unknown/none) on your system. The attribute ‘entities’ lists them in a comma separated string - whitelisted entities first, followed by any others.
edit - maybe not quite working 100%, as I just lost both my tp-link switches - it registered the 2 devices going offline but it just put the same one in the attributes twice, so a bit of working out still to do - but it’s pretty close!!