Template Sensor: Total_out in kWh

Hi All,

i’m a bit confused, maybe some of you can help me or point to the right direction.
I have a smart meter which shows me the current consumption in W. (above +0 if I consuming energy and below -0 if I feed the energy back into the grid)
I want to calculate how much energy went back into the grid, means only consider the negativ numbers over time.
I’m a bit lost and have no Idea, I went already through some options below. But still not sure how to start.
I created a template sensor to consider only negative values:

sensor:
  - platform: template
    sensors:
      power_out_cur:
        friendly_name: "Aktuelle Stromeinspeisung"
        device_class: "power"
        value_template: "{{ states('sensor.stromzaehler_mt681_power_cur') | int  if states('sensor.stromzaehler_mt681_power_cur') | int  < 0 else 0 }}"
        unit_of_measurement: "W"

If I understood correctly now I can add the Rieman Intgation? What should be the right integration method for a power kWh calculation?

Your help is much appreciated!

this is how the sensor looks like:

Only one of those actually performs integration with respect to time to convert power to energy. This one:

For your sensor make sure you use the method: left configuration option for best accuracy.

This other integration just follows an increase and resets on a defined cycle.

So you can turn an always increasing value (from your Riemann Sum sensor) to daily/weekly/monthly/etc… sensor(s).

1 Like

Thanks this helps! I was able to solve my issue. This is what I did:

I have a sensor which tels me the current consumption in positive values and negative values (if I feed power back into the grid)
Based on this sensor I created a new template sensor, to consider only the negative values and transfer the negative values into positive:

sensor:
  - platform: template
    sensors:
      power_out_cur:
        friendly_name: "Aktuelle Stromeinspeisung"
        device_class: "power"
        value_template: "{{ states('sensor.stromzaehler_mt681_power_cur') | int *-1 if states('sensor.stromzaehler_mt681_power_cur') | int  < 0 else 0 }}"
        unit_of_measurement: "W"

then I used the “Riemann sum integral” Integration to create a sensor to calculate the kWh, method: left.

Not sure if it was needed but on top I added the following to my customize.yaml:

sensor.energieeinspeisung_gesamt:
  state_class: total_increasing
  device_class: energy
  icon: mdi:home-export-outline
  unit_of_measurement: kWh

Now everything is working fine as far as I can see.

Thanks for your help!

Thanks for this thread.
I will do the same.
One question though:
Do display this in your Energy dashboard as well?

Yes, it does.