Battery SOC Forecaster

Hello - I am wondering what people are using, or approaches people may have tried/used around forecasting battery level. I am referring to a configuration where:

  • A smart meter/inverter is reporting total (load) usage to home-assistant. In my instance I utilise the Sungrow-SHx-Inverter-Modbus-Home-Assistant with my SH10RT - 3 Phase system
  • A battery reporting current state of charge to home-assistant. In my instance again using the Sungrow integration with a 25.6kWh battery.
  • A solar system and a solar forecasting system (I use the HA Solcast Solar integration).

My system of monitoring is working well, my solar forecast is sensible, my battery and inverter are integrating into home-assistant, but to scratch that itch further, I’m interesting in forecasting not just solar - but simulating how my battery might discharge overnight and recharging during the following day(s). The use case for this is to potentially delay or make decisions around whether to say use my washing machine today, or air-conditioner early, knowing that my battery might not recharge fully tomorrow, it would help me better understand the system and down the line would potentially be used to set/limit forced discharge when the feed-in-tariff is high.

To date, I have been playing with a custom Python script that:

  • Reads in the previous 7-days of energy usage, groups the usage into 30-minute intervals (as this is the interval Solcast produces), and then calculates the mean and standard-deviation for each time-interval.
  • Similarly, while making some (not statistically valid) assumptions using Solcasts 10th, 50th (assumed) and 90th percentile forecasts, estimating a statistical distribution for power at each time-interval.
  • Because generation and consumption are independent, I thought sampling each distribution 10,000 times and then determining 10,000 potential net energy values (10,000 produced solar estimates less 10,000 consumed energy estimates). These 10,000 net energy values could then be analysed to extract say the 10th / 50th / 90th percentiles for each time-interval and therefore be used to simulate a battery…(?)
  • Currently this pushes a forecast (similar to solcast using a date-stamped attribute with values) to home-assistant through a push/http request, but I am looking to transition this to MQQT(?) or another format so that the output is cached/saved in case HASS Restarts (push requests don’t seem to have saved history/states).
  • The python script simply executes on my raspberry pi every hour using cron.

Using this forecast, I then have a sensor inside home-assistant via a template that:

  • Reads the current battery state of charge in kWh
  • Calculates for the next 30-minute, the potential battery level based on the 10th/50th/90th net energy values.
  • Loops forward over the next 30-minute interval using the previously predicted 10th/50th/90th battery level.
  • Builds a time-series/forecast of state of charge

The python script and sensor seems to be functioning, e.g,:
Daily cumulative solar forecast actuals vs forecast (10th/90th bands) as an output from the python code (not Solcast)

  • Additional graphs in comment, new user limitation… *

Some things i’m thinking about:

  • Statistics isn’t my strong suit, but by say, using the 10th percentile net energy to calculate the 10th percentile battery level, then iterating that always using the 10th (or the 90th…), is a compounding assumption. There must be a better way to handle this in selecting an appropriate distribution to sample.
  • Is this better done not in python externally, so that the 10,000 sample approach could be done for the battery?
  • What is the most logical way to process the daily data and resample the generation, at the moment I am calculating the energy step every 30minutes and doing the statistics on that - ensemble statistics. Is there a more robust way to do the statistics on the daily cumulative and then process it back to intervals?
  • It would be cool to be able to use this to forecast export (or import) as well, so that a power chart (like below - could be adapted to include the power distribution forecast. Once all the kinks and statistics are ironed out.

Anyway - hoping to hear what others have tried or some thoughts and suggestions on this. I’m new to python but happy to share code or sensors etc., if that’s of interest to anyone! Cheers!

1 Like

Daily cumulative energy forecast actuals vs forecast (10th/90th bands) with historical 7d values in grey. I have a daylight saving time error on my daily data so ignore the 1-hour offset. Also a very low energy day!

Here’s the output, application for the battery simulator, showing the historical/current state of charge and then the forecasted levels for today through and tomorrow. The red-dash is the best estimate (50th) and the bars indicate the range using the other percentiles. Grey lines indicate the previous 7-days. (We had a longer solar day than the previous few days coupled with low usage, so the battery went into the evening higher than normal)

… and here’s the power card to add the forecast to: