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?

Ah! I have also wanted to do this for a while. I have had a few attempts at creating helpers but haven’t been successful. I have tried making entities that track the calculated rate as a custom tariff and then used the utility meter helper with this tariff but my numbers always appear to be nonsense. Have you had any luck?

No not yet unfortunately. I think it might actually take some code to read total_increasing sensor values for total charge and total discharge and correlate that with the prices and store the result in an accumulator. I don’t think that accumulation can be handled well inside of HA’s templates.