How I solved a reset of an integral

Spent a few hours reading without finding a good solution of how to reset an integral.
the suggestion of how to integrate a value is this, (without the possibility to reset or??):

  - platform: integration
    source: sensor.resetable_battery
    name: Energy_in_battery
    unit_prefix: k
    round: 2

However, this just continuous to integrate without the possibility to reset (I might be wrong…)

So I created a template sensor instead of the actual power that I wanted to integrate… I added a push button so when the button is pushed the sensor gets the value of x(-100000) times the integrated value. The resulting integration goes to zero. Had some trouble so I added a small random value to my “fake” sensor value during reset. Don’t know if it’s needed. The idea was that integration updates when the sensor value changes so…

      - name: "Resetable_battery"
        unit_of_measurement: W
        state: >
          {%set CC=states('sensor.battery_power')|float%}
          {%set DD=states('sensor.Energy_in_battery')|float%}
          {% if states('input_boolean.reset_battery')=='on'%}

Isnt there any nicer solution??

Feed your Riemann Sum sensor to a Utility Meter helper (UI or YAML) and specify the reset cycle for periodic rests, or don’t specify a cycle and use the reset service when you want to.

Add the unit_time key with a value:

That’s not going to enable them to reset the total.

Maybe I misunderstand what @Arne999 wants to achieve.
At present the integrating-function is reset every unit_time= 1hr. This means it starts to count up again from 0 after every hour of time.

If he wants to continuously integrate and perform a manual reset now and then, he should not use the HA Integrate integration but another one.

The Riemann Sum integration is required to convert power to energy (though I suspect they should be using method: left to minimise errors). The utility meter can provide a periodic or automated reset.

Im integrating the power in/out from the battery continously to have a measure of how much energy I have stored.
Im aware that the voltage can be used but Im doing this because it is interesting to test and compare it to the BMS (that actually is not very good at predicting…).

It ia also theoretically more accurate and “easy” to implement a smart correction when the battery degrades over time.

Back to your original question, did you try the Utility Meter?

I have the same problem, and tried the Utility meter. The trouble is, like the OP, I really do want an integration, e.g. to integrate Watts (power) to get Watt-hours (energy). This should, for example, scale contributions by the time between samples, use the trapezoid rule to get better accuracy, etc. The utility meter just adds “deltas”, which is only correct if the samples are equally spaced and you scale them by how often they arrive (which may be hard to find out).

I looked into the “unit_time” thing which is close to what I want but doesn’t say when the reset happens. What I really want is a service call that I can call to reset the value to zero. In fact what I want to do is monitor the state of the battery charge (SoC), so what I want to do is set an initial value in the morning, just before the sun comes up, using e.g. the “settled” voltage of the battery to estimate the SoC, then integrate power (input and output; BTW, I have multiple different sensors for that, so I actually have a set of integrals I need to sum and difference to get the net energy) over the day.

Anyway, another option here is to just let the integrations increase forever, but set up an automation to take “snapshots” of them daily (e.g. in the morning) and subtract that from the integral to get the daily cumulation. It works, but…

That’s exactly what this does:

Use method: left.

You then feed that to a utility meter and define your wanted reset cycle.

Oh, I see. So it’s a combination of the Reimann integration and the utility meter integration that should be used. I guess that works, will try.

  • hah, confusing use of “integration” term; here I mean “the code thingy you plug into HA”

But: why “left”? Is not “trapezoidal” more accurate?

Usually, Home assistant only triggers on a change of value, and immediately when the value changes. One therefore must assume that the value was constant before the change came. The Riemann sum only calculates on each change. That is why you need the left method, which assumes a constant value up to the change.

Take for instance a light that is off for a day, and then turns on at 50W (for a split second). Trapezoidal would assume it took a day to rise from 0 to 50W, e.g. the trapezoidal rieman integration would add quite a high value. But in fact the value should not (or barely) change, because the light was only on for a split second. All the time before the light was completely off.

Trapeziodal would be better when a continuously changing value is polled on fixed intervals. But that is usually not how sensors in Home assistant behave. So it baffles me that trapeziodal is the default.

Bear in mind that this also means that if the light stays at a constant 50W, the integral will not update until a change (e.g. when the light is turned off or dimmed). When there are many fluctuations, the integral is way more up to date. This behavior is similar in sensors like derivative, which are not very accurate on sensors with only few updates.

I’m trying to do something similar but am a little confused. Should I be using sensor.whatever_utility_meter_entity? to reset and display the zero’d out value? In other words it’s now responsible for its own copy of the aggregation of my integral value (Wh and Ah)

I am not sure if I understand this correctly :
The Riemann integral should reset according
to the time base settings, i.e. I choose hour and the sum resets each hour ?
That does not happen, my sum increases forever.

I could feed this into an utility meter and get my hourly reset, but the integral will still increase forever.

If you read my post again. that is exactly what I said to do.

Maybe I am wrong, but that does not solve the problem of resetting the integral sum.
The Riemann integral will increase forever, and sooner or later there will be problems working with these numbers.

You don’t reset the Rieman Sum. Ever. There is no problem with letting this count up forever.

If you want a periodically resetting sensor feed the Riemann Sum to a utility meter and choose the reset cycle you want. Then use this sensor and ignore the Riemann Sum sensor.

I had all 3 of my Riemann sum values reset about 10 days.
DIdn’t affect my energy charts or anything as I am using Daily meters on them but I’m unsure why they reset. Anyone know what causes it? I thought they would just increase forever.

Do you mean they rest to zero?

Or that you can’t see history further back than 10 days?