How to sum energy and power sensors?

Hi,

I have many devices, each able to measure energy ( kWh ) and power ( kW ).
I would like to have a card that will show graph about power consumed by the whole house.
As well has a energy history.

Can anyone help building the logic ?

Thank you in advance,

Simone

Use a template sensor to add all your individual energy sensor values. e.g.

- platform: template
  sensors:
    energy_total:
      friendly_name: 'Total Energy'
      entity_id:
        - sensor.energy_1
        - sensor.energy_2
        - sensor.energy_3
      value_template: "{{ (states('sensor.energy_1')|float + states('sensor.energy_2')|float + states('sensor.energy_3')|float)|round(3) }}"
      unit_of_measurement: "kWh"

In that case I have rounded the result to three decimal places.

Create another template sensor for all your power sensors. Don’t mix the two types.

You may get some more inspiration here:

https://community.home-assistant.io/t/daily-energy-monitoring/143436?u=tom_l

8 Likes

Thx for the quick reply.

Is there a way to get new entities automatically added ?
No way to get a wildcard in entity_id or similar ? Or may be a way to group them first and then use the template you suggested ?

Simone

I suppose it is possible by adapting something like this template that gets all lights that are off and creates a list of entity ids.

{% set domain = 'light' %}
{% set state = 'off' %}
{{ states[domain] | selectattr('state','eq', state) | map(attribute='entity_id') | list | join(', ') }}

Set the domain to ‘sensor’, and match on the unit of measurement (kWh) . Though this may be an issue as your template sensor will have the same unit and it will add itself. The easiest solution to that would be to subtly change the unit of the template sensor (e.g. KWh is different to kWh).

Then join the matches into a string of additions.

I suspect you would have to monitor a time sensor to force the template to update every minute, otherwise you may end up having to list all the sensors the template has to monitor for changes anyway - negating the usefulness of the template.

Either way it’s beyond my templating skill. @123, @pnbruckner or @petro may have some input.

You can’t just combine kW and kWh. They 2 totally different meanings. Gotta take all your kW sensors and turn them into kWh sensors by using the integration sensor.

1 Like

You can’t just combine kW and kWh. They 2 totally different meanings. Gotta take all your kW sensors and turn them into kWh sensors by using the integration sensor.

I don’t want to sum those 2 entities; the result should be 2 different values one for KWh and one for KW.
My issue is to make the addition of consistent sensors in a wisely manner.

Simone

fixed for power:

- platform: template
  sensors:
    shelly_current_consumption:
        friendly_name: Current Power
        entity_id: sensor.time
        unit_of_measurement: W
        value_template: >
          {% set ns = namespace(states=[]) %}
          {% for s in states.sensor %}
            {% if s.object_id.startswith('shelly_sh') and s.object_id.endswith('_current_consumption') %}
              {% set ns.states = ns.states + [ s.state | float ] %}
            {% endif %}
          {% endfor %}
          {{ ns.states | sum | round(2) }}

Now time to understand how to reset counter at midnight for energy.

Simone

1 Like

Use the utility meter with a daily cycle.

1 Like

Done:

- platform: template
  sensors:
    shelly_total_consumption:
        friendly_name: Total Energy
        entity_id: sensor.time
        unit_of_measurement: Wh
        value_template: >
          {% set ns = namespace(states=[]) %}
          {% for s in states.sensor %}
            {% if s.object_id.startswith('shelly_sh') and s.object_id.endswith('_total_consumption_attr') %}
              {% set ns.states = ns.states + [ s.state | float ] %}
            {% endif %}
          {% endfor %}
          {{ ns.states | sum | round(2) }}

and

utility_meter:
  energy:
    source: sensor.shelly_total_consumption
    cycle: daily

Let’s see tomorrow if values are correct.

Simone

5 Likes

@chemelli: I started to use your code (thanks for posting) and it seems that the template sensors get reset to 0 and then back to their group value. This in turn leads to the dailiy utility_meter to increase by that amount.

Have you encountered this issue as well?

PS: Sorry for reviving this old topic :slight_smile:

1 Like

@hillbicks, sorry for the late anwer.

Nope I’m not experiencing the issue you describe.
My latest code is here: Shelly utils repository

Simone

1 Like

Hi Simone,

i didn´t get it work. i want to sum the total energy for all my shelly devices. i tried with your codes but i don´t know how you managed it. can you help?

thanks arthur

@flatty2804, which integration as you using ? ShellyForHass or native ?

Simone

Shelly for Hass

Strange, should work fine. At least it used to here.
Now I switched to the Shelly native integration so I adapted it a bit.

Simone

what is the entity_id: sensor.time in your script?
when i insert your code in configuration.yaml i got the error for line: entity_id: sensor.time
String does not match the pattern of “DEPRECATED^”.yaml-schema: http://schemas.home-assistant.io/

I’m struggling with random resets of the total energy consumption. I’m also using the native integration of Shelly. Would you mind sharing the adaptions you made to your initial GitHub script to make it work with the native shelly integration?

nevermind i found way to solve these random resets of shellys way to provide total energy usage to home assistant.
For future reference: You can use the riemann sum integral to calculate the total energy usage based on the current energy usage. This is a little bit less accurate than the values directly reported by shelly (around 0.5% due to the reporting rate of current power usage) but still better than getting wrong information. You can then wrap the total energy usage provided by the riemann sum integral with the utility meter integration. This delivers top results for me so far.
In contrast to directly reading the total energy usage from shelly devices, this is also more error proof. When rebooting home assistant, home assistant tends to set the last reading of a shelly device to 0. When home assistant then gets the new total energy usage value from shelly, home assistant calculates the difference and that’s why these huge total energy usage numbers exist. Using the riemann sum integral solves these because having 0 current power usage doesn’t affect the calculation. The only problem is, if you have devices that do not report energy usage in real time or at least very frequently. The riemann integrations also fails on downtimes. But usually home assistant and shelly devices that offer power readings are online around the clock

Hello,

if I want to combine two sensors of my solar system, how do I combine them?
The sensors in watts:

  • sensor.gartenhaus_pv_power
  • sensor.kaco_pv_watt

Where does that have to go? In the configuration.yaml it is not accepted.

groups:
all_power_sensors:
name: current sensors
all: true
icon: mdi:lightning-bolt
entities:
- sensor.gartenhaus_pv_power
- sensor.kaco_pv_watt

This in the configuration.yaml

- platform: template
    sensors:
      total_power:
        friendly_name: Stromverbrauch gesamt
        value_template: >- 
          {{ expand('group.all_power_sensors') | map(attribute='state') | map('float')  | list | sum }}
        unit_of_measurement: 'Watt'

But it doesn’t work :frowning:

Hi,
i am extremly new fan of HomeAssistant…
According to this: Template - Home Assistant

I managed to sum up 3 phases of discovergy meter (discovergy hacs) by putting this into configuration.yaml:

template:
  - sensor:
    - name: "myPower_total"
      unit_of_measurement: W
      state: >-
        {{ (states('sensor.electricity_XX_phase_1_power')|float +
        states('sensor.electricity_XX_phase_2_power')|float +
        states('sensor.electricity_XX_phase_3_power')|float)|round(0) }}

it will show up as sensor entities:
image

So in @zumheulen case i would simply do:

template:
  - sensor:
    - name: "PV_Power_total"
      unit_of_measurement: W
      state: >-
        {{ (states('sensor.gartenhaus_pv_power')|float +
        states('sensor.kaco_pv_watt')|float )|round(0) }}
3 Likes