Calculating of electrical energy kWh from power sensor kW

How to calculate the electrical energy kWh from the value of the active power sensor kW? The active power sensor “P2” kW already works via modbus and can have a positive value (power from the network - consumption) or a negative value (power to the network - delivery) and I need to calculate separately the “consumed energy +kWh” and the “supplied energy -kWh”.
The calculated sensors “consumed energy +kWh” and “supplied energy -kWh” should have an increasing value without reset, similar to the values on the electricity meter.
Thanks for the answers.
sensor P2

    `sensors:
     - name: P2
     unit_of_measurement: kW
     slave: 245
     address: 50546
     input_type: holding
     data_type: int32
     precision: 2
     scale: 0.01
     scan_interval: 5`

Just an idea … could you create a second template sensor that is the inverse of P2 (i.e. P2 x -1.0), you could then create two utility meters to totalise the energy for each of these. The net_consumption parameter defaults to off meaning that only positive values for each will be used.

Hi,
It’s not very complicated.

  - platform: integration
    name: "Template PM Water Heater Energy"
    unique_id: template_pm_water_heater_energy
    source: sensor.template_pm_water_heater_power
    method: left
    round: 0 

Both of the above suggestions are incorrect. The utility meter needs to be fed with energy sensors, not power sensors. The Riemann Sum helper needs to have the power split into consumed and supplied first.

To do this you need to split your power into consumed and supplied energy sensors using template sensors:

configuration.yaml:

template:
  - sensor:
      - name: P2 Consumed
        unit_of_measurement: kW
        device_class: power
        state_class: measurement
        state: >
          {% if states('sensor.p2')|float(0) >= 0 %}
            {{ states('sensor.p2')|float(0) }}
          {% else %}
            0
          {% endif %}

      - name: P2 Supplied
        unit_of_measurement: kW
        device_class: power
        state_class: measurement
        state: >
          {% if states('sensor.p2')|float(0) < 0 %}
            {{ states('sensor.p2')|float(0)|abs }}
          {% else %}
            0
          {% endif %}

Then feed these two sensors to the Riemann Sum Helper to convert your two power sensors to energy sensors. Be sure to use method: left

I’m not sure if the integration would not work with the negative numbers.
If you separate supplied and consumed power like @tom_l showed,
you can do:

- platform: integration
    name: "Supplied Energy"
    unique_id: supplied_energy
    source: sensor.p2_supplied
    method: left
    round: 0
- platform: integration
    name: "Consumed Energy"
    unique_id: consumed_energy
    source: sensor.p2_consumed
    method: left
    round: 0

now you can add sensor.consumed_energy and sensor.supplied_energy to your dashboard.

1 Like

Yeah exactly that. They did say:

1 Like

hi, yes. realized that later. My ADHD strikes again :slight_smile:

Thank you tom_I and Mr.ToR, good job, works perfectly!

1 Like