Help with energy consumption measurement

I have a smart plug that does not have an energy sensor, but only a current and power sensor (showing current A+W usage). I want a sensor that I can use for energy/cost tracking but fail to understand what I need to do.

I have created the following sensors:

template:
  - sensor:
    - name: Waschmaschine_kwH
      state: "{{ states('sensor.waschmaschine_power') | float / 1000}}"
      unit_of_measurement: 'kWh'
      state_class: measurement
      device_class: energy 
    
  - sensor:
    - name: Waschmaschine_energy
      state: "{{ states('sensor.waschmaschine_kwH')}}"
      unit_of_measurement: 'kWh'
      state_class: total_increasing
      device_class: energy

sensor:
  - platform: statistics
    entity_id: sensor.waschmaschine_energy

Unfortunately the sensor waschmaschine.energy is not increasing but simply displaying current kwH usage as the other sensor. Any help would be appreciated on how to creating such a sensor.

I think I got it working using the sensor.integration

sensor:
  - platform: integration
    source: sensor.waschmaschine_power
    name: waschmaschine_energy
    unit_prefix: k
    round: 2

(and obviously removing the other sensors mentioned in my old post)
Honestly, I find this exremely confusing and I think one should write a proper guide on how to use these. Btw, what is the difference between

sensor:
  - platform: template

and

template:
  - sensor:
sensor:
  - platform: template

is legacy template sensor

template:
  - sensor:

is new style template sensor. New style lets you add device_class, legacy does not.

1 Like

I don’t think you understand how this works …

Power in Watts in an instantaneous meaurement (Volts x Amps) and you cannot convert this to Energy in kWh via a simple equation. Energy is the integration (or summing up) of all the instantaeous Power measurements over a period of time e.g. if you had a 100W lamp running continuously for 1 hour then that would consume 0.1kWh of Energy. This is a very simple example but just imgaine a home with many different electrical devices going on and off at various times - this is exactly what your home electricity meter measures for billing purposes.

Anyway coming back to your question about converting Power to Energy, this has been covered several times before in the forums such as the following:

My problem is not the lack of understanding on how energy works, but rather on how to setup the sensors. I was aware that when my sensor displays 5W for an hour that my consumption was 0,05kWh - but what I wanted was a total consumption, so how much energy was used since I plugged in the device. And since my plugs update with the correct value every second of power usage I was hoping I could easily setup an sensor that simply adds those numbers, which sensor.integration does.

Edit: I just realized that my conversion in kwh was obviously wrong anyway :wink:

Please accept my apologies, I looked at your original post and it seemed like you were having trouble with the basics.

I think you need to use the “Integration” integration as you have discovered but note that this is only an approximation because the instantaneous power measurement is sampled and then reported to HA where the integration is performed. For a more accurate measurement, the integration function would normally done in the measuring device and the Energy value then reported to HA.

If you can’t get the integration function working as you want then it may just be easier to prurchase a plug that does this for you.

1 Like