using a few template sensors like this:
critical_devices_offline:
friendly_name: Critical devices offline
entity_id:
- sensor.cv_garage_state
- sensor.cv_stookhok_state
- sensor.cv_zolder_state
- sensor.zonneboiler_zolder_state
- sensor.freezer_bijkeuken_state
value_template: >
{% set offline = expand('group.critical_devices_state')
|selectattr('state','eq','off')|list|count %}{{offline}}
attribute_templates:
message: >-
{% set count = states('sensor.critical_devices_offline') %}
{% set offline = expand('group.critical_devices_state') |
selectattr('state','eq','off')|map(attribute='name')|list -%}
{%- if offline|length == 0 -%} Ok, all critical devices are online
{%- elif offline|length == 1 -%}{{count}} Critical device offline: {{offline[0]}}
{%- elif offline|length == 2 -%}
{{count}} Critical devices offline: {{offline[0]}} and {{offline[1]}}
{%- else -%}
{{count}} Critical devices are offline:{{offline[:-1]|join(', ')}}, and {{offline[-1]}}
{%- endif -%}
icon_template: >
mdi:numeric-{{states('sensor.critical_devices_offline')}}-box
All is fine, except for the icon which won’t update, and, after one of the triggering entity_id’s changes state, is one state behind…
moving the icon template to the customize section makes the magic happen, and from startup all sensors are showing perfectly.
Ive also tried the template without the expand, and naming all entity_ids verbosely, but that doesn’t make a difference.
this is the custom-ui bit for the sensor:
homeassistant:
customize:
sensor.critical_devices_offline:
templates:
icon_color: >
if (state !== '0') return 'red';
return 'green';
icon: >
return 'mdi:numeric-' + state + '-box';
I realize the customize section is evaluated in browser, so maybe after the backend has changed state, and thus is just that bit later, and with the correct new state to calculate the customization.
Still, adding all entity_ids in the sensor itself should take care of the updating shouldn’t it?
maybe you can see/suggest what could be I improved?
thanks