Riemann integral calculates wrong values with electrical devices

The riemann integral is used to calculate for example the energy comsumption from the current consumption. Or if a sensor delivers “liters per second”, the riemann
integral calculates “total liters”.
The riemann integral integration uses the trapezoidal method as the default calculation method for the area under a curve.
But in in many cases this default is wrong.

Assume the temperature curve of a heating radiator which is polled every 10 minutes. If it is 20° at time t and 30° at time t+5, you can
assume that the temperature went up slowly from 20 to 30°. So the area under the curve is (20+30)/2 * 5 min. (20+30)/2 is the average temperature.

But now let us look at a cooking plate. It is off for 10 hours and it is switched to 2kW now. HA did not get any sensor readings for 10 hours,
because the value did not change. So the energy is calculated as (0kW + 2kW)/2 * 10 hours = 10 kWh. So for the first second you switch your cooking plate on,
HA calculates 10 kWh energy comsumption. This happens with all devices where the comsumption remains at a constant level for a longer time and then goes up.
For these cases, you can use the method:left which calculates the area under the curve only as old-value*time.

So for values which are continuously differentiable, the trapzoid method is ok, for values which change in steps (i.e. for the most electrical appliances), you should
use method: left.

The problem is that most people do not think about that problem and they will get totally wrong readings for their energy consumption.

Solutions for this problem:

  • make method: left the default
  • enhance the documentation and warn
  • introduce a new “method: optimal” which uses method:left if the elapsed_time is long or if dX/dY is big, i.e. the gradient is large. in all other cases use “method:trapezoidal”


If I get time I’ll submit a PR to change this to a blue “Note” box tomorrow:

Or you could do it. https://github.com/home-assistant/home-assistant.io/blob/cdc0ec82cca77443d7d20e34f36d900416912edb/source/_integrations/integration.markdown

Don’t forget to vote for your own feature request.

1 Like

My oven and hot plate do not produce spikes. They go on and stay on for 10 minutes.
The problem is that they jump from off to on. They produce stairs. Also, all lights do that. For the trapezoidal method to be correct, you would need changing values which are not sent to HA. This is the case for devices which are polled every x minutes. But for all other devices which send their values as soon as they change, you have to use the “left” method.

That’s exactly what the author meant by "spikey consumption ", on/off rather than continuously variable. It is poorly worded. As I said, feel free to edit it.

Maybe we can make a list of devices where the trapezoidal method is fine and those devices where the “left” method should be used.

Also if the device has been off for hours, the integration must not calculate the first value, because this value will be badly wrong.

I agree we should really make method “left” the default.

This issue keeps popping up, unfortunately taking our time, and producing poor integration results.


Apologies for not being a math wizard, but help me to explain; I am seeing a lot of ‘energy leakage’ in my home. What do I call energy leakage is the true power consumption (DSMR reader) and substract all the known energy from it (I have about 12 appliance monitors). During the day the amount of energy leakage always increases to about 3 kWh (which is about 30% of my consumption.

I do measure my fridge, TV, boiler, oven, dishwasher, dryer, washing machine etc and am using the default trapezoid for that.

Are you now suggesting I move all that to method left?

Yes, “left” should be the best option for you.

Updated all of them with the exclusion of 1 of them (which always shows very gradual consumption).

Will report back whether or not my energy leakage problem is solved.


Old topic, but problem still exists. Setup is:


  • platform: integration
    source: sensor.chargert_power_active_import
    name: charged_energy_to_car
    method: left
    round: 3

Resul is high jump in value at beginning of electricity consumption. Any ideas?

Seems proper restart was needed.

And? How is it?