OK - the sensor -
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='entity_id')|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='entity_id')|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 -}}
It currently updates every minute (with sensor.time) but I’m thinking of refreshing it with an automation that runs every second instead just to be sure it captures straight away when things go offline.
Obviously you’ll want
group:
entity_whitelist:
# Entities you want to check are there that might not be in the states list at startrup
entity_blacklist:
- sensor.unavailable_entities
# any other entities that you don't mind being unavailable/unknown/none
As I say, aside from the duplication of whitelisted items that get in to the states list and then go offline, it’s pretty ticketyboo.