Accruing values after change of state

I’m trying to estimate battery state of charge given a switch from charging to discharging and the accumulated energy from that point. The resource was helpful in getting some ideas and learning about the monitor function, but it gets kind of complicated in practical terms.

Is there a better way of doing something like this?