Cumulative Sensor Calculated from 2 others?

My BMS is away being repaired, in the meantime I can only rely on pack voltage to determine SoC for my 48v 16s pack, which for Lifepo4 doesn’t cut it.

Before shelling out for a Smart Shunt or similar I thought I could at least use the pack charge/discharge current values from the inverter divided by the update interval as a crude way of determining how many Ah are going in/out of the pack.

Given I know it’s a 280Ah pack, I can gather a more accurate SoC vs using Voltage alone.

I’ve only got a cursory understanding of creating custom configs etc in HA so am having trouble working out how best to do this.

Using the template editor I have been able to assign values to variables based on those sensor values and do some basic math with them, trim off extra digits etc, but I am missing the step about how to loop this and make it cumulative.

Effectively I need to either start from 0 or 280 and add/subtract from that where the low point cannot go below 0 (and never would) and the high point cannot go above 280.

This could snippet will add the charge current amount, divided by the update interval (40s) to a starting capacity of 140 (as a midpoint), trim it if it’s above 280 (because depending on when this starts the capacity could be anywhere but at some point it will stop charging when it’s full and at that point we will have set our capacity to 280) and display the outcome. It only does it for a single point in time though and I’m not sure on the logic of using a For Loop here to make this work of if there is a better approach.

This snippet is missing the subtraction logic, but that’s not a big deal to add, first step was to get it “counting” properly.

Cheers

{% set batt_start = '140' | int %}
{% set charge = (((states("sensor.voltronic_battery_charge_current") | int) / 90) | round (3)) %}
{% set batt_cap = batt_start + charge %}

{% if batt_cap > 280 %}
{% set batt_cap = '280' | int %}
{{ batt_cap }}
{% else %}
{{ batt_cap }}
{% endif %}