How to sum energy and power sensors?

Hello,

I managed to do it, but unfortunately I can’t select it in the HA energy desk yet.
Do you have to convert it to kWh?

- sensor:
    - name: "PV Gesamt"
      device_class: power
      state_class: total
      unit_of_measurement: "W"
      state: >
       {{ states('sensor.kaco_pv_watt')| float + states('sensor.gartenhaus_pv_power' )| float  }}

That makes sense because it’s no HA power desk yet :stuck_out_tongue:

Power over time is energy :point_left: that is what you are aiming for :zap:

HA got you covert with a Riemann sum integral function that’s very useful for your task :wink:

1 Like

Hi,
It is great for me as well, but I have one question:
How can I divisible by 1000?
For me, kWh is the measured unit, so it was multiplied based on this, I would redistribute it to get kWh. Thus, kWh would be displayed everywhere, I should not display this as Wh.
Thank you very much!

It’s possible calculate the difference and not the sum? What is the easiest way?

Hm. if + in the example above is the way to do the sum, what could be the calculation magic to get the difference.

a. + as well
b. *
c. /
d. -

1 Like

As a totally newb on Home Assistant I want to thank You for providing this example. I didn’t get it to work at first, trying to google if object_id is the same as Entity ID (which it apparently isn’t). So I modified Your code a little, and adjusted the name of my shelly sensors which is sensor.shellyNAME_power and this seems to work for me. Thanks!

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

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

Is it possible to use the Entity ID sensor.shelly_current_consumption in (edit: statistical) graphs after adding it to configuration.yaml? I can’t use it in (edit: statistical) graphs right now. edit: I could use the sensor in a History Graph though.

When trying to add this synthetic sensor to a statistical graph I get this; Expected data source not listed - Home Assistant

You’re configuring a statistic but you couldn’t find your source in the dropdown? That’s caused by a bug in the integration providing the entity. Integrations need to configure their entities correctly so Home Assistant knows that we need to track statistics for it and how.

Open an issue with the author of the integration and link them to Sensor Entity | Home Assistant Developer Docs.

Any hints on how I could improve this to get that to work? I tried adding the following line to the sensor-description above, but that alone didn’t do it.

state_class: measurement

editedit:

Apparently there’s an old way and a new way to create templates. This method above is the old way, and cannot accept a state_class. Thus, the template has to be rewritten to the new format, which does accept state_class. I did it through the UI helper, so I don’t know how to show it in YAML, but essentially You create a new Helper in settings, choose Template, Template a sensor, enter a name, enter the code-block below (edited to Your needs) as State template. W as unit of measurement, Device class: Power and State class: Measurement. Now the sensor can be used in historical graphs too.

{% set ns = namespace(states=[]) %}
          {% for s in states.sensor %}
            {% if s.entity_id.startswith('sensor.shelly') and s.entity_id.endswith('_power') %}
              {% set ns.states = ns.states + [ s.state | float ] %}
            {% endif %}
          {% endfor %}
          {{ ns.states | sum | round(2) }}

Phew, this first post of my became unncessarily long, sorry for that. Hope this helps someone.

Hi, just getting the hang of all of this, where in HA do I add this “platform: template…” script/texxt for adding sensor data?

That is a very old and out of date post. The new config is defined here:

This is an old thread I know.
Why ist this list of entities necessary for this template? Would it not work without?

It was how the template sensor knew which entities to monitor for updates. This no longer required it all happens automatically and is actually no longer a valid option. Also you should be using the new template sensor format not this legacy version.

Yes I know but I am still on 0.103.6.
I have noticed when not using this list it seems like the sum of the sensors is not updated immediately on a value change.

:scream:

That’s from 5 years ago!

Very little you ask on these forums is going to be relevant.

Yes I know. Only people like you know about these versions :wink:
One day I will upgrade but the amount of work now will be enormous!
Actually I like the old states UI and yaml programming.

Yes. Yes it will.

And it will only get worse every month you delay.

YAML programming is still a thing. I use it extensively on the current version.

believe we need proof of that… how is it even possible

you get a notification upon each and every update…

You will have to upgrade. Sooner or later. Wish you luck :slight_smile:

What kind of install do you have?

I bet supervised… On a pi3…

On a Pi4 :wink:
And even using States UI

image

Some day I will start the project :smiley:
Anyway.
I have noticed when not using this list it seems like the sum of the sensors is not updated immediately on a value change. So the sum will update by each change of one of the entities when using the list?

Suffice it to say, you are the only person I’ve seen using a pre-covid version in multiple years.
That makes you the expert on this version. I’ll keep your name in mind if someone else show up with a question about vintage HA.

But getting help from someone that’s kept up with the versions is VERY hard, and you can’t expect very good answers from us. You know how much HA has changed in 5 years?? The python version listed in that image is probably 3.6, HA is using 3.12. Polymer is gone. Lovelace UI is not that any more. The only thing current is the people listed there are likely still listed in the current version. There are almost 2000 PR’s a month that are merged, and several times that added and worked on.

I respect what you are doing but it’s hard to help you…