Whatever you wanna achieve I had told you what you had to do to get it done right:
The Riemann integration, then most likely power metering and some other points.
This Riemann integration is well documented here in home assistant too if you do a search for it or follow the link below. You do not even have to do any math yourself cause it runs automatically based on your parameters which you also have to read and understand first cause otherwise you might face strange results caused by your parameters if you had chosen them not right / carefully.
And before you start doing anything further try to get a good old energy meter to get a calibration and to become aware what 1 kWh measured by SonOff is in reality worth cause there are many factors kicking in the whole challenge:
- measurements from SonOff hardware / sensor
- kWh integration by Riemann formula
- time measurements by your HA server and
- any kind of delays between server and sensors
Therefore I first plug in my energy meter that delivers reliable measurement results and then I push in the tuya or SonOff plug. After 1 week I can compare the figures of that particular tuya / sonoff plug versus my real power meter cause the difference can be quite high and might vary between each of those sonoff plugs, I mean even inside a batch of the same Pow R2 I had found deviations of 5 - 8% back then before I left the SonOff ship in 2019.
Maybe now you understand that this is stuff you really have to understand frm the basics and where good youtube explanation videos are quite rare that start from the very beginning to the Wh you are looking for - at least for local tuya which took me 2 weeks to figure out how to get these data, how to deal with iits decimals, delays, integrations and how to sum all those plugs up to get 1 figure over all cause once you have running the first POW2 meter you will add several more and asked for sums, then sums by hours / nights and then days, weeks, months and quarters.
And for that there is a lot to understand first but all of that starts in the basics of Watt sensor and getting the energy consumption as Watt over time by the riemann sum integral.
Do not forget: every result / measurement in HA can be crap as long as you do not have a good powermeter to cross check how well your SonOff unit is working / measuring, how well the HA server is doing the timing of the sample rate and the delay between sensor and server .
Do not trust the figures SonOff is providing cause otherwise you will be astonished how much bigger your electricity bill might be after one year than SonOff might have told you.
If those SonOff figures are off a lot from the good old power meter tool (which is about 30 - 60€ here in europe) than you might have to change Riemann parameters depending on the use case you have at this particular SonOff Powermeter / switch cause for high power devices connected to it and turned on and off quite often you can use a different parameter for conservative values which will mean that the consumption displayed by HA is higher than the real consumption and not vice versa.
The key parameter for that is called “method” and described as "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 more accurate readings.
And beyond that there is also a parameter that touches the topic of sampling size or rate depending on which manufacturer’s plug or sensor you use.
Sampling_size: 20 for example is the maximum for local tuya implementations which describes that HA will ask the plug / sensor 20 times per minute or every 3 seconds for the most current value. That 20 sampling size will create 1200 values per hours from each sensor or plug just to give you the power curve over time for that particular plug and which then will also create the same amount of data for the Wh measurements cause once you get 1200 values per hour from sensor 1 the riemannn integral will also deliver 1200 counter values for the energy consumption cause that can change every 3 seconds too or even not at all if there is no consumption, but then the counter will still reply 1200 times the same amount of energy.
For example a 2.400 figure as reply (which of cause means Watt if you ask a watt sensor) from the son off plug would mean that
for those 3 seconds of an hour or 3 sec / 3600 seconds per hour or
3 / 3600 hours x 2400 Watt power are consumed or
1 / 1200 h x 2400 W = 2 Wh in total - but for the last 3 seconds only.
Can 2 Wh in 3 seconds really be true ?
Damn easy cause from 3 seconds to 3.600 seconds of 1 hour you have to multiply by 1.200 and if you multiply 2 Wh x 1200 you will get 2400 Wh
And a 2400 Watt device will have used 2400 Wh after 1 hour of usage.
Now: Your home assistance server has to do that riemann integration for every sensor and every 3 seconds over and over again.
Imagine you had turned the device of after those 3 seconds for the next 57 seconds then the total consumption would stay at those 2 Wh … but imagine your would have turned it on for 20 minutes at the end of this particular hour then you would have to add 20 / 60 hour x 2400 Won top of the first 2 Wh or 1/3 h x 2400 W + 2 Wh = 800 Wh + 2 Wh.
The total consumption of that 1 hour would end up in 20 minutes and 3 seconds running time and a total consumtion of 802 Wh in total.
And of cause this is an easy and simplified example cause in real life the integration is not counting how long it is running with the same Power (20 minutes x 2400 Watt), no, it is constantly polling the Watts used every 3 seconds and doing the math for that if you run the sensor with a sampling_size of 20 … and if the Power goes a bit up to 2460 W it will do it for 2460 W and the next 3 seconds for maybe 2340 W.
Remember: if the plug had used 2400 W from the last reading and now you would get a 3600 W reading your question might be how the riemann integral is calculating that as a digital jump after 3 sec. from 2400 W to 3600 W or is there a kind of extrapolation and linearisation by assuming that on the last third of the 3 seconds the power already rose half the way and reaches peak not at the beginning of the next measurement cycle but 1 second in then the results might be better or worse, but for sure different.
All this is what you need to be aware to understand such possible results and figures right or to raise questions if these figure can be true or not or if you might have made a mistake. And finally you will also have to deal at the same time with decimals which can also cause a lot of trouble and headache if you cut those too early and too much cause 0,5 Watt measured 1200 times an hour make a difference in the long run if you cut or round to 0 zero decimals cause the difference of 1 W over a year is simply a 9 times bigger figure - but in kWh.
Why ? 365 days 24 hours / day just means 8760 h or roughly 9000 times higher figure or 9 times bigger in kWh if you track in W. 1 Watt = 9 kWh more consumption in a 247 use case.
And these are just 2 steps on the journey to get and understand consumption figures, Riemann integration, sampling size and then decimals / round and so on to get 1 HA consumption figure which you then have to crosscheck with your energy meter results to find a calibration factor that will match or make the HA figure fit to the the real world figures of the power meter cause those are what you will have to pay for and you wanna know in advance.
Shelly ones make it easier cause they do deliver the Wh you wanted to have right now but the basics and misconeptions are the same for all those powersensors, even though the shelly deviation was at least a lot lower than back then when we had gotten a batch from SonOff (all Pow 2 so 2nd generation back then in 2019).
Good luck, take your time and go step by step and you will succeed but it might take quite more time than you had ever considered cause it took me overall to get everything right (building counters, building sums over plugs / powermeters, understanding the basics of calibration, getting behind the sum of riemann integrals and its possible errors, understanding that a to high sampling rate can stop the sensors from working for hours or even days, …)
If someone might give you SonOff formulas and examples then it might deliver you quick results that can be useless if you have not understood any single step and cause for mistakes cause I can tell you that you will never recognice what is happening. Why ?
If the sensor stop working the graph will not fall down but you might find in the past values like a straight line cause the W will stay the same in your graphs: you will see the 20,5 W for 6 hours but in reality the riemann formula will not have counted anything cause the overloaded tuya sensor tend to report the same value as before on the display but not in the math. After 3 days not checking each and every sensor there is close to no chance to get behind such event and mis measruement I can tell you. But I got behiind that due to a simple fact: my power andn energy meter had before shown that the tuya were delivering figures that were 100% right - spot on. But that one morning I had noticed that there was a gap between the power meter and real world compare to the HA values delivered by tuya and the Riemann integration. So I had to check it out and build a graph for each plug about the past 24 hours and there I found this:
This is in Zoom mode 96 hours ago and there is a 3 hours frame when the power consumption had not been working but Watts were shown … as having said 3 hours in big zoom over the past 96 hours (you can see about 24 hours 4 days ago). Now let us know where the error has happened and how to identify such an error without having the knowledge I have told you to gain ?
Here it is
Right in the beginning you see horizontal lines over 3 hours - how can you get such perfect horizontal lines over 3 hours even in total consumption if freezers kick in from time to time and have a specific pattern?
Check the blue graph in the upper graph and look for the spikes - those are from the fridge and the high peak caused by the compressor motor that needs high power to start to turn against high resistance — but over the 3 hours in that night there was not a single peak, not in overall consumption of kitchen nor in the fridge meter … cause all were dead or suffered from overload due to too high sample size of 20 ! Since I turned it down to 15 or every 4 seconds I do not have any issue - but I had to learn it the hard way and copying code to gether would never have taught me what to take care for and how to get behind such things.
What you can see here in these 2 report excerpts are Watt used, but those figures shown there during the period of full flat horizontal line were not true cause during that the system was simply only recording the last stage or last successfull report over and over again while the Wh were not count in the Wh sensor.
I hope you have learned a lesson from that. Getting code and copying that together can be dangerous if you do not understand what is really going on or here going wrong cause the interpretation needs your knowledge, expertise and not guessworks cause otherwise you will never trust a watt meter or such sensor again in my opinion.