I found out about this last night and tried to implement this as part of a clean-up of an existing sensor.
This is my existing implementation:
template:
- trigger:
- platform: time_pattern
minutes: "*"
sensor:
- name: Unavailable Entities
icon: mdi:help-circle-outline
state: "{{ states | selectattr('state', 'in', ['unavailable', 'unknown', 'none']) | rejectattr('domain','eq','button') | map(attribute='entity_id') | reject('==', 'sensor.unavailable_entities') | list | length }}"
unit_of_measurement: entities
state_class: measurement
attributes:
# buttons don't have state – we don't care about buttons here
unavailable: "{{ states | selectattr('state', 'in', ['unavailable']) | rejectattr('domain','eq','button') | map(attribute='entity_id') | reject('==', 'sensor.unavailable_entities') | list }}"
unknown: "{{ states | selectattr('state', 'in', ['unknown']) | rejectattr('domain','eq','button') | map(attribute='entity_id') | reject('==', 'sensor.unavailable_entities') | list }}"
none: "{{ states | selectattr('state', 'in', ['none']) | rejectattr('domain','eq','button') | map(attribute='entity_id') | reject('==', 'sensor.unavailable_entities') | list }}"
This isn’t the most efficient, for various reasons: There’s a lot of duplication and the order of filters can be improved.
Using this action
section, I set a variable with a reduced list that I can re-use:
template:
- trigger:
- platform: time_pattern
minutes: "*"
action:
- variables:
reduced_list: >-
{# the midea fan and outdoor temp sensors are unavailable when a unit isn't running #}
{% set exclusions = [
'sensor.unavailable_entities',
'number.living_room_ac_fan_speed',
'number.main_bedroom_ac_fan_speed',
'sensor.living_room_ac_outdoor_temperature',
'sensor.main_bedroom_ac_outdoor_temperature',
'sensor.solar_reserve_percentage_5min_average',
'sensor.pv_power_5min_average'] %}
{# buttons and event entities don't have state – we don't care about those here #}
{% set reduced_list = states
| rejectattr('domain','search','button|event')
| rejectattr('entity_id', 'in', exclusions)
| selectattr('state', 'in', ['unavailable', 'unknown', 'none'])
| list %}
{{ reduced_list }}
sensor:
- name: Unavailable Entities
icon: mdi:help-circle-outline
state: "{{ reduced_list | length }}"
unit_of_measurement: entities
state_class: measurement
attributes:
unavailable: "{{ reduced_list | selectattr('state', 'eq', 'unavailable') | map(attribute='entity_id') | list }}"
unknown: "{{ reduced_list | selectattr('state', 'eq', 'unknown') | map(attribute='entity_id') | list }}"
none: "{{ reduced_list | selectattr('state', 'eq', 'none') | map(attribute='entity_id') | list }}"
This, however, seem to go haywire. It’s not reporting the correct entities and count – not even remotely. I have about 950 entities in my system. This version reports 637 (unavailable) entities.
Evaluating the templates in the dev tools give the correct result: A state of 3, with all 3 being unavailable entities. Cross-checking this by filtering on entities from the Entities view under the Devices & Services menu confirms this (taking my exclusions into account).
Do I have an error that I’m not spotting? Otherwise, I can only think there’s some multiple evaluations of the action block going on. Honestly, I actually have no idea.