Convert W to kWh?

Which integration method did you use?

Not saying, it can work perfectly well for you.
Just pointing out to others who might read this thread that the average approach has its pitfalls.

I’m using Rieman with left method for my plugs and it seems fine, at least handling zeroes. Never used any kind of meter to confirm accuracy, but I don’t expect those plugs to be spot on, anyways.

I was actually using something similar in grafana, before, but I was doing 1-hour sums of 1-minute averages.

I think I need to explain a little more clearly.

Firstly your statement was incorrect. If we take the example of 1000W for 5mins this would not result in an energy value of 1kWh.
1kWh is 1000W for 1hr and as 5min is 1/12 of an hour then 1000W for 5min is 1000/12 = 83.33Wh

The average sensor calculates the average value of a sensor over a period of time. If, for example the average sensor has a value of 500W at the end of a 24hr period then the energy consumed for that period is 500 * 24 /1000= 12kWh.

There are no pitfalls to this method, it relies totally on the operation of the average sensor. It is simple to implement and highly accurate.

I tried both the Statistic (didn’t handle zero values) and the Rieman (a method of approximations) integrations and both proved to be much less accurate.

If you are in any doubt, just give it a try and compare the the other methods and a measured value.

I don’t think you get my point, either.
It’s not a matter of method, it’s a matter of how the averaging component you are using work, as far as the code told me.

But whatever, it works for you, all fine.

So which method did you use when you tried the Riemann integration?

FWIW, after looking at the code, Rieman is effectively doing sum of averages, as I did in grafana without knowing.

image

The difference being that the integration is doing average between 2 consecutive state changes, at least with trapezoidal method, rather than between fixed durations, and that the sum is ever-increasing, so it has “dynamic” limits, so to speak.

With left, only the old state is considered, so going from 1000 to 0 will give 1000 for that interval
With right, only the new state is considered, so going from 1000 to 0 will give 0 for that interval

The left method is recommended for “on”/“off” type of consumption because it is assumed that when you get the “off” state, i.e. 0, the actual usage during the last interval was actually 1000.
On the contrary, when you go ton “on” / 1000, it is assumed the consumption was actually 0 during the interval.

That’s what I understand, at least :wink:

Good explanation and why I asked the OP, twice, which method was chosen (because it definitely influences the result).

Hello,
Thank you for your method of calculation. However, I have a quick question as I am new to using HA. Where should I place the calculation information you refer to?

  • ‘platform: average
    name: ‘Device Average Power’
    start: ‘{{now (). replace (hour = 0) .replace (minute = 0) .replace (second = 0)}}’
    end: ‘{{now ()}}’
    entities: sensor.device_power’

  • ‘device_energy:
    value_template: "{{’% 0.2f’ | format (states (‘sensor.device_average_power’) | float / 1000 * (now (). hour + now (). minute / 60))}}"
    unit_of_measurement: “kWh”’

  • ‘platform: average
    name: ‘device 5min Average Power’
    duration: minutes: 5
    entities: sensor.device_power’

Thanks for your reply.
Richard

Funny, I created a german video about this topic a week ago: Umwandlung Watt in Verbrauch pro Stunde - YouTube

1 Like

Hi rikray02

Sorry for delay, I’ve been busy elsewhere.

That snippet of code should be placed in the sensors.yaml file.

The devices

device_average_power
device_power
device_5min_average_power

will then be available (after a restart) as sensors on the entities card for example ie.

sensor.device_average_power 

hope this helps

That was a good one, thank you!

I think i understand that this but of code goes in the sensors.yaml

device_energy:
        value_template: "{{ '%0.2f'|format(states('sensor.device_average_power')|float / 1000 * (now().hour + now().minute/60)) }} " 
        unit_of_measurement: "kWh"

Where does this bit of code go? Is it in the configuration.yaml?

- platform: average  
    name: 'Device Average Power '
    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ now() }}'
    entities: sensor.device_power 
1 Like

The code also goes in the sensors.yaml

i simply have this:

---
# Power Consumption in Watt, kWh to W
#
#
sensor:
  name: Power Consumption (W)
  unit_of_measurement: "W"
  icon: "hass:lightning-bolt"
  state: >
    {{ states('sensor.power_consumption')|float * 1000 }}

sensor.power_consumption is in KWH, now sensor.power_consumption_w is in watt

What kind of sensor is it?

The important thing to check is if your sensor provides the power readings with a constant duration between each reading or if the power reading frequency increases with higher power usage?

If you’re getting for example a reading every 2 seconds when your consuming about 400 watts of power and also if you’re getting a reading every half a second when you’re consuming about 4000 watts, you’re in good shape then. It means it’s actually giving you a power reading per every some constant energy usage. This means you can get the exact energy usage your device can mesaure.

Could you explain what kind of sensor it is?

Should i then be able to see the device in the Energy Dashboard? Ultimately thats what i am trying to do.

That’s not how this works.

It utilises the average function operating on the power consumption of a device.

The average power over a period of time is multiplied by the duration of the measurement and this gives the energy consumed.

Sorry I’m confused on how you know this?
If his power readings are averaing over a fixed period or if they are instantaneous power usage after a fixed energy usage?..
I was expecting @invinciberry would know what kind of sensor he has.
How do you know how his sensor works?
He has only one post and no other explanation…
Am I missing something? Did he tell you in a private message? Are you guys living together?
This is really confusing…

Same issue here. Tried everything I could think of, but it’s notwirking

I am new to all this, so apologies if I use some wrong terminology.
I was trying to acheive something similar to create a sensor/meter to sum the Watts used on a Meross (mss310) smart plug I have.

My current method is using a ‘Riemann sum’ (trapezoidal, time unit = hours) based on the watt entity of my smart plug. This just constantly sums the W reading from my smartplug, so after a few days I’m at a few KWh. not ideal.

Then I created a ‘utility meter’ using the above riemann sum, and using a daily reset cycle.
(still working getting an average, or ‘last24hrs’)

This gives me a sensor with Wh I can add to my energy dashboard and see the daily usage of my ‘fridge’