Incorrect calculation of energy consumption

I’m trying to solve a routine problem of calculating electricity consumption in a house.
To begin with, I took the Xiaomi Aqara Wireless Relay 2 Channels Wifi Relay, which controls the convector with a capacity of about 1.8 kilowatts.
I added a sensor that converts power from watts to kilowatts and rounds to the 4th decimal place:

  - platform: template
    sensors:
      wifi_relay_komnata_current_power_per_kw:
        value_template: "{{ (states('sensor.0x155d200419d9d8_power') | float * 0.001) | round(4)}}"
        icon_template: mdi:flash
        unit_of_measurement: 'kW'

Added utility meter entity:

  hourly_wifi_relay_komnata_load_kw:
    source:   sensor.wifi_relay_komnata_current_power_per_kw
    cycle: hourly
    tariffs:
      - same

If I turn on the convector for a few seconds, then the instantaneous power reaches 1.8 kW, and hourly_wifi_relay_komnata_load_kw_same returns a similar value. Although in fact, the full consumption of 1.8 kW did not occur. If you turn on the convection again for a few seconds, the result is already 3.6 kW. Because of this, the cost of electricity is calculated incorrectly.
Tell me what is my mistake?

You are using a power sensor (kW) for the utility meter but it requires an energy consumed sensor (kWh) that always increases. There are many posts about this on the forum.

Thanks a lot! Before your answer, I could not understand what the problem was, but after your answer, I immediately found the necessary posts on the forum.
To improve the coverage of this issue, I will give my solution.
I created a sensor that shows the total instantaneous power in kilowatts of the devices I need. In this case, these are two wi-fi relays:

  - platform: template
    sensors:
      total_current_power_per_kw:
        friendly_name: 'Общее потребление реле'
        value_template: "{{ (states('sensor.wifi_relay_koridor_and_tualet_current_power_per_kw') | float) + (states('sensor.wifi_relay_komnata_current_power_per_kw') | float | round(4))}}"
        icon_template: mdi:flash
        unit_of_measurement: 'кВт'

Using the sensor with the integration platform (https://www.home-assistant.io/integrations/integration/ I converted the instantaneous power in kilowatts to kilowatt-hours:

  - platform: integration
    source: sensor.wifi_relay_komnata_current_power_per_kw
    name: wifi_relay_komnata_kWh
    unit: 'кВт/ч'
    round: 4

I use the value of this sensor to visualize energy consumption and calculate the cost of consumed energy.
Everything works great! Thanks to the developers and the Home Assistant community!

2 Likes

Great, you got it working.

If you notice odd readings, change the method to left. From the docs:

method string (Optional, default: trapezoidal)

Riemann sum method to be used. Available methods are trapezoidal, left and right.

In case you have an appliance which produces spikey consumption (like an on/off electrical boiler) you should opt for the left method to get accurate readings.