Hey team! What’s the right check in this template (trying to find lowest battery level) to prevent the following error which happens sometimes (I think during startup, or when reloading template entities).
TemplateError('UndefinedError: No aggregated item, sequence was empty.') while processing template 'Template("{% set min_battery_level = expand('group.all_battery_devices') | map(attribute='state') | reject('in', ['unknown', 'unavailable']) | map('int', -1) | list | min %} {% if min_battery_level < 50 %} red {% elif min_battery_level < 70 %} amber {% else %} green {% endif %}")' for attribute '_attr_native_value' in entity 'sensor.dashboard_sub_batteries_color'
- platform: template
sensors:
dashboard_sub_batteries_color:
friendly_name: "Dashboard Sub Batteries Color"
value_template: >
{% set min_battery_level = expand('group.all_battery_devices')
| map(attribute='state')
| reject('in', ['unknown', 'unavailable'])
| map('int', -1)
| list | min
%}
{% if min_battery_level < 50 %}
red
{% elif min_battery_level < 70 %}
amber
{% else %}
green
{% endif %}
Add an availability template that confirms the group entity’s state value is not unknown (i.e. doesn’t exist yet or doesn’t exist at all).
If it’s unknown then it won’t evaluate the value_template (thereby avoiding the error message caused by expand producing an empty list) and will report the Template Sensor’s value as unavailable.
- platform: template
sensors:
dashboard_sub_batteries_color:
friendly_name: "Dashboard Sub Batteries Color"
value_template: >
{% set min_battery_level = expand('group.all_battery_devices')
| map(attribute='state')
| reject('in', ['unknown', 'unavailable'])
| map('int', -1)
| list | min
%}
{% if min_battery_level < 50 %}
red
{% elif min_battery_level < 70 %}
amber
{% else %}
green
{% endif %}
availability_template: "{{ states('group.all_battery_devices') != 'unknown') }}"
Hey @123 = this isn’t working for me unfortunately.
The group is always returning a state of “unknown”. Is that because I have it defined as a sensor group? Is this the right way to define such a group? The documentation doesn’t talk about sensor groups too much - only binary_sensor.