There really is no easy solution ro that problem.
There are two options.
- Let the device calculate the values
- Let HA calculate the values
If you go with option one then the device will measure the consumption at regular intervals and calculate a value for consumption per time slice.
The issue here is to get HA ti read these values correct.
It needs to read each value before it is changed again by the device.
If it too early it will read the same value twice, which can be countered with a timestamp, butbifbit is too late then a value will be missed and a timestamp will only tell HA that it missed a value and not what it was. HA can then choose. Value of 0, a guesstimate value, a mean value or repeat the last value. None of these are probably correct.
HA, in other words, needs to be in sync with the device to read it correctly, but the lower the interval of the calculations get on the device the more precise this sync needs to be.
If HA is restarted or just hangs, freeze or otherwise gets delayed then you will have your dreaded spikes.
If you choose option 2 then the calculations occur in HA.
The device just have a counter for consumption.
When HA reads the counter it might be 128, and when it reads it 7 seconds later it might be 136.
HA knows both timestamps for the reads and can then calculate the difference which is the consumption.
If HA get a freeze or delay, then the read will just occur at maybe 13 seconds later, but the calculations are the same, so no sync is needed.
If HA is restarted the. It might write the value down to it’s disc and read it again after boot up and everything just works.
But there are issues with this solution too.
One issue is if overflow is not handled correctly.
Let’s say your counter is a 8-bit counter, which means it can count to a max of 255. Once it hits 255 and adds a number more it will restart to count from 0, so HA will see a change from like 253:to 7 in a few seconds, which will give a huge negative consumption if not handled correctly.
Likewise if you unplud the device and the counter was at 136, then when it comes back up it might start at 0 and it will again calculate a huge negative consumption.
If HA is restarted it might save the value, which can counter this to some extend, unless the time period is so long that the counter could overflow twice. If that is the case, then value is completely unreliable.
Option 2 is the most used one, but for integration/add-on developers there is the issue of not always knowing what kind of counter you are dealing with.
Is it 8, 16 or 32-bit and is it signed or unsigned.
For example a signed 8-bit counter will go from -128 to +127, while an unsigned will go from 0 to 255.
And these numbers are just the hardware limitations.
The device manufacturers can choose a smaller range if the want, so instead of 0 to 255 it will maybe be 0 to 100.
If the developer choose a too small number type in HA then higher values will cause an error.
And if it is bigger then the developer needs to find the right limit for overflow and handle it.
An 8-bit counter for like watthours are somewhat easy to test for, since after 255 or less watthours it will reset, but a 16-bit value might first reset after 65536 watthours and the number for 32-bit is insane.
I hope this give some insight in the issues.