I have an energy monitor sensor reporting in W. I would like to convert it to kWh to track how much power the device uses daily with the new energy panel.
There have been many posts and requests regarding the conversion from W to kWh. On the surface it would seem simple enough however it does seem to cause endless problems with many solution and work arounds.
I am going to describe the method I use which is over 90% accurate and really simple to implement.
First of all we have to understand exactly what it is we are trying to do. A frequently asked question is how do I convert 100W to kWh, the simple answer is you canât.
Power is in Watts and energy is in kWh, to convert W to kWh you have to introduce a period of time, ie how long was I consuming 100W, if it is for 1 hour then you have consumed 100Wh or 0.1kWh.
Unfortunately life isnât that straight forward and the power will rarely if ever be a constant value, it will rise and fall and may even drop to zero.
A solution I have commonly seen is the feed the statistics integration with the power levels, this fails because that integration does not seem to handle values of zero.
My solution utilises the truly excellent average integration.
This is how it works;
- platform: average
name: 'Device Average Power '
start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
end: '{{ now() }}'
entities: sensor.device_power
In the above snippet I fed the average integration with sensor.device_power.
entities: sensor.device_power
The integration allows for various methods of defing the span of time we are interested in. In this case I was looking at the period of time since midnight and now (which would change and increase throughout the day)
The start statement
start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
replaces the time now() with 00:00:00, ie midnight.
And the end statement
end: '{{ now() }}'
is simply now()
The average integration then works out the average power over this period.
It is then a simple matter to multiply this average power by the time since midnight.
device_energy:
value_template: "{{ '%0.2f'|format(states('sensor.device_average_power')|float / 1000 * (now().hour + now().minute/60)) }} "
unit_of_measurement: "kWh"
The length of time since midnight is simply the current time
In the code above I have converted the current time from hours and minutes into a decimal number. The result is divided by 1000 to convert to kWh (from Wh) and I have formatted it to 2 decimal places.
I have also used the average integration to calculate a rolling 5 min average power that I send to PVOutput
- platform: average
name: 'device 5min Average Power'
duration:
minutes: 5
entities: sensor.device_power
The value of this sensor is simply the rolling average power over the previous 5 minutes.
I have run this method for over a month and compared it with actual values taken from my smart meter.
Consumption Meter: 4.7kWh calculated 4.77kWh
PV Meter 4.39kWh calculated 4.41kWh
Export Meter 2.29kWh calculated 2.31kWh
I hope this is helpful.
Be careful that âplainâ averaging is only good enough for âdevicesâ (generally speaking) that are always on/consuming.
If you do that with, e.g., a smart switch, youâll get strange results because the averaging will only be done on actual readings / state changes, i.e. the zeroes wonât be taken into account.
For example, if you switch on a device consuming 1000W for 5 minutes, I assume youâll basically see 1kWh for the whole day.
Assuming you use https://github.com/Limych/ha-average , it seems indeed to do averaging on the HA history, so I assume youâll get the side-effect described above.
The ha-average integration handles zero values perfectly. This can be seen with my consumption figures, because I have solar panels my consumption from the grid falls and remains at zero for prolonged periods of time. Yet the difference between my reading from my consumption meter and the calculated values is trivial.
Your comment about 1000W for 5 minutes is true, but that is not what Iâm doing.
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.
FWIW, after looking at the code, Rieman is effectively doing sum of averages, as I did in grafana without knowing.
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
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
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
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?