Calculating the sum of a group?

Hello all!

I have a boiler, which I am using to heat the house. Every heating has its own thermostat and is running on its own schedule and is opening the valve when it needs hot water.
For this I am using the following code (full example here):

- alias: 'Heizungssteuerung'
  trigger:
    - platform: numeric_state
      entity_id: climate.badheizung
      value_template: '{{ state.attributes.Valve }}'
      above: 15
    - platform: numeric_state
      entity_id: climate.barbaraheizung
      value_template: '{{ state.attributes.Valve }}'
      above: 15
  action:
    service_template: >
      {% if states.climate|sum(attribute="attributes.Valve")|float >= (20*3) %}
        switch.turn_on
      {% else %}
        switch.turn_off
      {% endif %}
    entity_id: switch.wirtschaftsraumtherme

Unfortunately Homematic does not report a state for one or two devices and they get unavailable as value and do not have the attribute Valve anymore, which leads to a broken automation and a veery cold house :frowning:

Therefore I switched over to a template sensor, with which I can use default values:

- platform: template
  sensors:
    bad_heizung_valve:
      entity_id: climate.badheizung
      value_template: '{{ states.climate.badheizung.attributes.Valve | default(0) }}'
      unit_of_measurement: '%' 

and put those into a group.
Question is: Can anyone give me a good example how to calculate the sum of the groups?

Thanks in advance!

Cheers Hannes

Does this help? I am using this to calculate the average home temp by adding some sensors then dividing by the number of sensors and rounding:

#
#
#   Average Home Temp
#
#
     average_main_temp:
       friendly_name: 'Average Main Floor Temp'
       unit_of_measurement: "°F"
       value_template: >-
         {{ ((float(states.sensor.aeotec_multisensor_6_temperature_7_1.state) + float(states.sensor.aeotec_multisensor_6_temperature_8_1.state) + float(states.sensor.main_floor_temperature.state) + float(states.sensor.office_temperature.state)) / 4) | round(1) }}

     average_upstairs_temp:
       friendly_name: 'Average Second Floor Temp'
       unit_of_measurement: "°F"
       value_template: >-
         {{ ((float(states.sensor.upstairs_temperature.state) + float(states.sensor.upstairs_temperature_2.state) + float(states.sensor.second_floor_temperature.state)) / 3) | round(1) }}

I just did something similar, summing all values for a given unit of measurement:

{{ states.sensor|selectattr('attributes.unit_of_measurement', 'equalto', 'W')|rejectattr('entity_id', 'equalto', 'sensor.power')|map(attribute='state')|map('float')|sum }}

I use jinja filters to list all wattage sensors, and then, most importantly, I kick the current sensor out from the result (if not you get endless recursion), and then i pick the attributes and finally sum them.
I would assume you could do something similar for a group … :

{{ group.foo.entities|map(attribute='state')|map('float')|sum }}

That should be a starting point, I don’t know if it works, but tweak accordingly.

3 Likes

Here’s a good reference for some of the techniques @nervetattoo was mentioning:

http://jinja.pocoo.org/docs/2.9/templates/

Hello!

Thank you all so much for your help!
Too bad groups do not support the “entities” tag, it would definitely have solved this issue a lot easier, but actually the “rejectattr” was what I was missing out on.
My solution now is just a modified version:

     {% if states.climate|rejectattr('state', 'equalto', 'unknown')|map(attribute='attributes.Valve')|map('int')|sum >= 20*3 %}
          switch.turn_on
        {% else %}
          switch.turn_off
        {% endif %}

so it should filter out the unknowns properly.

Thanks again!

Cheers Hannes