How to calculate value of energy in my battery

I’ve got a house battery, and I get wholesale electricity rates that change every 30 mins and my battery can charge from solar or the grid.
Because of this arrangement, I’d like to be able to calculate the current value of the energy in dollars, which I can then also convert to c/kWh for the energy in the battery.
This is because the battery may charge from free solar, when it’s 5c/kWh from the grid, or when it’s 30c/kWh from the grid so when the battery says it’s at 100% the actual value is different day to day.

I’ve got entities which represents:

  • Battery charge from grid
  • Battery charge from solar
  • Current grid price
  • Battery discharge

I sort of want to integrate the values which is straight forward, but I can’t figure out how to “consume” the value as the battery discharges. The discharge might need to be done by battery % and battery total capacity to automatically handle the efficency loss.

The tricky bit I think is if the battery discharges 1%, then the value of the energy needs to decrease by 1%, not by a particular dollar amount.

Has anyone tried to do this before? Any suggestions on how this might be done?