Sum of entities?

Is my only option for adding up a list of entities to use the template sensor, with a list of sensors in the value template?

  - platform: template
      sensors:
        total_sum:
          value_template: "{{ (states('sensor.sensor1') | float + states('sensor.sensor2') | float) }}"

I was hoping something like the min_max integration had a sum feature, where you just provide it with a list of entities.

Is there a way to make the platform template a bit easier to read, i want to sum the value of about 20-30 entities, and having them all on a single line makes it more prone to mistakes. Are there any ways of formatting it for easier reading?

You can try this code:


        {% for d in states | groupby('domain') %} {{ d[0].replace('_', ' ') |
        title }} | {{ states[d[0]] | count  }}

        {% endfor %}

        **TOTAL**:  {{states | count}}

That looks to print out a total list of components?

         Alarm Control Panel | 1

         Automation | 44

         Binary Sensor | 51

         Calendar | 1

I actually want to add up the value of individual sensors.

i.e.
Sensor 1 + Sensor 2 + Sensor 3
(10) + (20) + (30)

So sticking that in the the template sensor would return 60. Which is fine for a small number, but if i want to add up 20-30 sensors, then it could lead to typos/errors.

Ok, should have read whole sentences and not only the title…

You can count sensors by using {{states.sensor | count}}. This way gives you the sum of all sensors. The output can be filtered. You mentioned „individual sensors“. What sensors do you want to be counted?

I basically have a number of smart plugs that i’ve pulled out the power consumption. So i have sensor.smartplug1_watts, sensor.smartplug2_watts and so on.

I currently have a template sensor like in the OP where i just add the sensors together, but it’s a long list and if i need to edit/add to it then i’m more prone to making a typo.

I guess by the time i’ve filtered out {{states.sensor | count}} it’ll be of similar length to just defining them as per above.

Yes, it’s a pity that it seems to be no calculation possibility out.

This might be interesting for you, though:

https://community.home-assistant.io/t/how-to-automatically-include-and-sum-the-values-of-multiple-utility-meter-sensors/165814/4