Riemann sum mis-calculation?

Hi All,

I’ve been using Riemann sum successfully for some time, but overnight I noticed a big jump in the output.

Home Assistant shows me the sum entity -

and the source entity -

Since the source was at 2.7kW for 1 hour, I would expect the Riemann sum to increase by 2.7kWh, ie -

  • prior to 4:30am - old value of 17.45
  • 5:30am - new value of 17.45+2.7 = 20.15

Instead, I got -

  • prior to 4:30am - old value of 17.45
  • 4.30am - jump to 26.91 … extra 9.46
  • 5:30am - jump to 28.24 … extra 1.33

Any thoughts ?

I’ve just moved the config from the helpers UI to yaml so its more obvious what I’ve set.

Sounds like you don’t have it set to left sum? This is what you want for a power/energy sensor.

Hmm … given the shape of the power plot, I’m not sure if that would matter … I would expect the choice of algorithm to only make a small difference to the integration estimate if the power is either increasing or decreasing. The docs, Integration - Riemann sum integral - Home Assistant don’t mention this, although I have read about using the left sum in other posts.

Also, previously it was working fine … just once it seemed to go weird (in fact just after the latest home assistant upgrade).

I’ll keep monitoring it and see if this happens again.


It’s due to the way HA handles receiving the same value being reported for a long time; it only records the initial change not the repeated identical values. This means you need to use left to avoid having it calculate incorrectly. Do some searching on the forums if you want a more detailed explanation.

Peter, did you solve your problem? I have the very same issue. I have just setup Left & Trapezoidal integrals to compare to my Right integral, but will have to wait a day or two to see if either are better.

As it happens I was looking at this yesterday for a different case. As before I found “left” works (although a small under estimate), the others don’t.

If you have long periods without updates, then left is the only mode that won’t incur large errors. And you will have long periods without updates on sensors that reach a hard limit, like zero.

Even if you think your sensor is updating every 30 seconds, if it is sitting at zero, there are actually no updates happening in HA. And if it stays at zero for an hour and then goes to 10kW, you’d get the following results for that 1hr period of zero power:

  • right: 10kWhr
  • trapezoidal: 5kWhr
  • left: 0kWhr

Note that the code is not “wrong” for any of these methods; they simply make different assumptions about the source data.