Creating a new sensor to track the rate of discharge in a solar battery

Good morning all,

I’m looking to calculate the rate of change in one sensor dependent on another sensor.

Specifically, I have a FoxESS inverter with HV2600 batteries and I want to measure the rate of discharge of the batteries (battery discharge sensor - daily utility meter in kWh) between state changes (specifically decreasing state) on a battery SoC sensor (%).
I’d also like to do the opposite between the battery charge sensor and between increasing state changes in the SoC sensor to better understand the rate of charge being added over time.

For an example, when the SoC of the battery drops from say 89% to 88%, I want to capture the total change in the battery discharge sensor (doing this manually it’s currently 140Wh per % drop).

I’ve dealt with reasonably complex templates and automations but this seems a real challenge to me!

Any help/ideas would be massively appreciated!
Thanks!