Helper Function "accumulator" (sum up)

I want an integration in order to sum up values. Very similar to the Rieman-Integral, but without any time relevance.
Any change in an input sensor must be summed up in the integration. Like the “M+” button in a traditional pocket calculator.

Isn’t that as simple as maintaining a variable, and adding to it on state change.

Actually thinking about it, what is wrong with riemann integral?

Maybe I’m thinking too complicated - please post a code example for “maintaining a variable, and adding to it on state change”

Why isn’t riemann working?

Time is often relevant whether you think so or not.
HA might update the sensor at specific times, but it might also only update the sensor when the value actually changes. You would have to know this when you try to sum up and that is the part that the Riemann integral solves.
Take a sensor that report this:


Just add the values together for each reported value will give you 1+1+4+4+6 = 16
Adding the values together for only changes will give you 1+4+6 = 11
using the Riemann integral will add a weight on the values based on time, so it would be 1+1+1+1+4+4+5+6 = 23 (since the common timeslice would be 15 minutes)

Isn’t that what the “Combine the state of several sensors” helper does when setting it to “sum” ?