How to use expand function in sensor template to sum all state of a group?

Hi, I want to sum all sensor state of a group.

If put this code in the development template page (/developer-tools/template), it works fine :smiley:
But when I put the code in a .yalm, the configuration validation says:

Invalid config for [sensor.template]: invalid template (TemplateAssertionError: no filter named ‘expand’) for dictionary value @ data[‘sensors’][‘total_power’][‘value_template’].

Mi code is:

- platform: template
  sensors:
    total_power:
      friendly_name: Total Power
      value_template: >- 
        {{ states.group.power_sensors.attributes.entity_id | expand | map(attribute='state')|map('float')|sum }}
      unit_of_measurement: 'Watts'

I’m not sure why it complains about the expand filter (I believe it was introduced in version 0.96). Perhaps it prefers to see it used as a function (it produces the same output):

{{ expand('group.power_sensors') }}

Regardless, there’s a problem with using expand in a Template Sensor. When Home Assistant starts, it will examine this Template Sensor and attempt to identify all the entities it must monitor for state-changes. What it will find in your template is the group entity, not the members of the group. So the only state-change it will monitor is the group turning on/off.

So, get ha to update the sensor periodically, however often /10 minutes or whatever

My personal preference is, wherever possible, to use an event-based approach as opposed to polling. In other words, have the device report its state-change (event-based) as opposed to querying it periodically to check for state-changes (polling).

For this particular Template Sensor, you can get event-based updates by simply listing the group’s members in the entity_id option. This allows Home Assistant to easily identify all the entities it must monitor. When any one of them changes state, then value_template will be evaluated.

- platform: template
  sensors:
    total_power:
      friendly_name: Total Power
      entity_id:
        - sensor.first
        - sensor.second
        - sensor.etc
      value_template: >- 
        {{ expand('group.power_sensors') | map(attribute='state') | map('float') | sum }}
      unit_of_measurement: 'Watts'

The sole disadvantage is that despite having created a group, you still have to list the group’s members in entity_id. So if you add a new entity to the group, you have to remember to also add it to the Template Sensor. However, this is the way it’s always been done in Home Assistant prior to the recent introduction of expand.

Ideally, on startup Home Assistant would handle expand('group.whatever) a special way. The presence of the expand function would cause it to monitor the group’s members and not the group itself. Then it wouldn’t be necessary to explicitly list the group’s members in entity_id. Maybe this is worthy of a Feature Request.

1 Like

This work fine!!! Thanks soo much! The only problem, how you said… is that you need to repeat the members in the group, but it works very good :smiley:

Glad to hear it works for you.

If you are interested in improving the situation, I would suggest you vote for the Feature Request I just posted. If it is implemented, you will not need to list the group’s members in entity_id when using the expand function.