TemplateError('UndefinedError: No aggregated item, sequence was empty.') while processing template

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 %}

Thank you!

Should I set the “expand” result to a variable and then check if is defined?

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') }}"
1 Like

Thank you!!

1 Like

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.

group:
  all_leak_sensors:
    entities:
      - binary_sensor.kitchen_sink_water_sensor_water_leak_detected
      - binary_sensor.matts_bathroom_leak_sensor_water_leak_detected

  all_battery_devices:
    entities:
      # Motion Sensors
      - sensor.garage_motion_battery
      - sensor.mailbox_motion_battery
      - sensor.ethans_room_motion_battery
      - sensor.kitchen_motion_battery
      - sensor.guest_room_motion_battery
      - sensor.living_room_motion_battery
      - sensor.matts_office_motion_battery
      - sensor.serenas_office_motion_battery
      - sensor.matts_closet_motion_battery
      - sensor.serenas_closet_motion_battery
      # Contact Sensors
      - sensor.front_door_battery
      # Locks
      - sensor.front_door_lock_battery_level
      - sensor.garage_indoor_lock_battery_level
      # Leak Sensors
      - sensor.kitchen_sink_water_sensor_battery_level
      - sensor.matts_bathroom_leak_sensor_battery_level
      # Ecobee
      - sensor.ethan_s_room_battery
      - sensor.serenas_office_ecobee_battery
      - sensor.master_bedroom_ecobee_battery
      # Vibration
      - sensor.dining_room_bench_center_battery
      - sensor.dining_room_bench_end_battery

Copy-paste the following template into the Template Editor. What is the result?

{{ expand('group.all_battery_devices') | map(attribute='state') | list }}

It should be a list containing the state value of each sensor.

Yes it is !

Good. So what does this report?

{{ expand('group.all_battery_devices')
            | map(attribute='state')
            | reject('in', ['unknown', 'unavailable'])
            | map('int', -1)
            | list | min }}

It should report 75.

Yes it does ! Thank you for your continued help by the way.

The template sensor works as expected. I have tested it.

It’s just the availability component that doesn’t work to be clear. Because the “states” always returns “unknown”.