Vertical increases on energy with Riemann sum

Hello everyone,

I have a templated sensor that subtracts my solar production from my consumption to get my net grid consumption.

{% set consumption = states('sensor.total_power_total_power_power') | float %}
{% set production = states('sensor.shelly_em_channel_1_power') | float %}

{{[ (consumption - production) | round(1), 0 ] | max }}

Then I have a simple Riemann sum to get the energy consumption from the grid using the trapezoidal method.

Today it was a sunny day and my grid consumption has been 0 for most afternoon, save for a 2kw peak when I used the water boiler for 1 minute. Apparently that was worth a whole kwh of energy.

I know that trapezoidal sum is not the best at handling peaks, but this is grossly overestimated.

Am I doing something wrong?

Use left not trapezoidal , a boiler is spikey.

Use left sum instead of trapezoidal.

Left sum can help for sure, but it is still weird, the spike happened for about one minute, so about 1/30 of a kWh, how wide are the intervals of the Riemann sum for it to amount to 1 kWh?

Or is it the sensor that did not trigger computation?

Google or search the forum for “Riemann sum”; it just comes down to the mathematics of how the various methodologies work and how HA stores repeat values. With trapezoidal, it curve fits between the FIRST 0 reading and your momentary spike, meaning that it treats it like it was actually slowly increasing. With Left, it stairsteps using each reading as the new column.