Skip to the content
Shawn Smith
Subscribe
The House Project 12 min read

The maths behind the shadow battery

The working behind the shadow battery test, every formula, worked example and result, for anyone who wants to check my sums.

Shawn Smith27 November 2026
Share
The midnight sum equation card in a Safari window on a Mac, showing how the shadow's day is evened up for the energy left in its battery
Fig. 00 · The House Project

Part of the Home Dash series in The House Project. Last updated 27 November 2026.

This is the working behind Putting the battery's AI to the test. If you'd rather have the story, start there. If you want to check my sums, this is the place, and I'd genuinely like to hear about anything that looks wrong. I'll keep it up to date as the live test runs.

The short version

  • The question is whether Sigen AI, the controller that came with our battery, leaves money on the table that an open optimiser could pick up.
  • A shadow replans the next 24 hours every five minutes and carries out only the first five minutes on a virtual battery. It never touches the real one.
  • Each five minutes is costed four ways, with no battery, with Sigen AI from the real meter, with the shadow and, once the day is over, with perfect hindsight.
  • Batteries that end the day at different levels are evened up by planning tomorrow from each level and counting only the difference.
  • The shadow only wins if its whole 95 percent range sits above 70 cents a day, it isn't behind on ordinary days and it isn't leaning on one lucky day.
  • So far Sigen AI has made 57 percent of the modelled best saving, and five replayed days can't tell it and the shadow apart.

The system

PartDetails
Battery48.36 kWh usable, a 10 percent reserve and 95 percent efficiency each way, so about 90 percent round trip
Inverter12 kW AC, a hybrid. The battery is rated higher, so the inverter is the limit
Solar11.875 kW DC across two roof faces, east and west
Export limit5 kW
PricesAmber, a buy price bb and a sell price ss in dollars per kWh, settled every five minutes. The forecast is five-minute for about an hour, then half-hourly
ForecastsSolcast for each roof face, trimmed 5 percent, and house usage as the 14-day average for each hour of the day

Throughout, Δ\Delta is five minutes, or 1/121/12 of an hour.

The planner

The shadow's planner is EMHASS 0.18.11, solved with HiGHS. For each half-hour step tt of the next 24 hours, with Δt=0.5\Delta t = 0.5 h, it chooses grid import ItI_t, grid export EtE_t, battery charge CtC_t, battery discharge DtD_t and solar curtailment XtX_t, all in kW and none below zero, to make this as small as possible.

min⁡∑tΔt[ btIt−stEt+w (Ct+Dt) ]\min \sum_{t} \Delta t \left[\, b_t I_t - s_t E_t + w\,(C_t + D_t) \,\right]

Here ww is a wear charge of 1.5 cents per kWh each way in the live plan, and zero for the hindsight benchmark. The plan has to respect these.

PVt−Xt+It+Dt=Lt+Et+Ctenergy balancePV_t - X_t + I_t + D_t = L_t + E_t + C_t \qquad \text{energy balance}
SoCt+1=SoCt+Δt (ηCt−Dt/η)48.36battery levelSoC_{t+1} = SoC_t + \frac{\Delta t\,(\eta C_t - D_t/\eta)}{48.36} \qquad \text{battery level}
0.10≤SoCt≤1,Et≤5,Ct, Dt≤12 kW0.10 \le SoC_t \le 1, \qquad E_t \le 5, \qquad C_t,\ D_t \le 12 \ \text{kW}
Dt≤12 Zt,Ct≤12 (1−Zt),Zt∈{0,1}D_t \le 12\,Z_t, \qquad C_t \le 12\,(1 - Z_t), \qquad Z_t \in \{0,1\}
SoC0=SoCend=SoCvirtualSoC_0 = SoC_{\text{end}} = SoC_{\text{virtual}}

The last line starts and finishes each plan at the virtual battery's level. The efficiency η\eta is 0.95. The switch ZtZ_t, which allows charging or discharging but never both, is part of EMHASS itself, in optimization.py at lines 3113, 4305 and 4309 of version 0.18.11, which makes this a mixed integer program. Grid import and export have their own switch too, so neither can happen at the same time as the other.

The forecasts roll with the clock. Step ii covers exactly the half hour from now+30i\text{now} + 30i minutes, and each input is the average of six samples across it, each taking the forecast entry whose period covers that moment.

σik=now+30i+5k+2.5 minutes,k=0,…,5\sigma_{ik} = \text{now} + 30i + 5k + 2.5 \ \text{minutes}, \qquad k = 0, \dots, 5
bi=16∑k=05b(σik),PVi=0.95⋅16∑k=05PV(σik),Li=16∑k=05profile[hour(σik)]b_i = \tfrac16 \sum_{k=0}^{5} b(\sigma_{ik}), \qquad PV_i = 0.95 \cdot \tfrac16 \sum_{k=0}^{5} PV(\sigma_{ik}), \qquad L_i = \tfrac16 \sum_{k=0}^{5} \text{profile}\big[\text{hour}(\sigma_{ik})\big]

Every five minutes the whole plan is made again from the virtual battery's current level, only its first five minutes are carried out and the rest is thrown away. That's 288 plans a day.

The ledgers

At the end of each five minutes, with the house's usage LL, solar PP, grid import II and export EE for that five minutes in kWh, and that same five minutes' prices bb and ss, each of the three live running totals adds that five minutes' cost.

For Sigen AI, straight from the meter.

ΔcostS=I b−E s\Delta\text{cost}_S = I\,b - E\,s

With no battery, whatever the house needed comes from the grid, and any spare solar is sold up to the 5 kW limit.

n=L−P,ΔcostN=max⁡(n,0) b−min⁡ ⁣(max⁡(−n,0), 5Δ) sn = L - P, \qquad \Delta\text{cost}_N = \max(n,0)\,b - \min\!\big(\max(-n,0),\ 5\Delta\big)\,s

For the shadow, the virtual battery first does what the plan asks, as far as it can.

B={min⁡ ⁣(Breq, (soc−0.10 cap) η, 12Δ−P)if Breq>0−min⁡ ⁣(−Breq, (cap−soc)/η)otherwiseB = \begin{cases} \min\!\big(B_{\text{req}},\ (soc - 0.10\,cap)\,\eta,\ 12\Delta - P\big) & \text{if } B_{\text{req}} > 0\\ -\min\!\big(-B_{\text{req}},\ (cap - soc)/\eta\big) & \text{otherwise} \end{cases}
soc←soc−B/η  if B>0,soc←soc−B η  otherwisesoc \leftarrow soc - B/\eta \ \text{ if } B>0, \qquad soc \leftarrow soc - B\,\eta \ \text{ otherwise}

Then the grid makes up the difference, just as it would for a real battery.

n=L−P−B,ΔcostSh=max⁡(n,0) b−min⁡ ⁣(max⁡(−n,0), 5Δ) sn = L - P - B, \qquad \Delta\text{cost}_{Sh} = \max(n,0)\,b - \min\!\big(\max(-n,0),\ 5\Delta\big)\,s

BreqB_{\text{req}} is the plan's battery power for this step times Δ\Delta, positive for discharging. The discharge is capped so the battery plus the solar stays within the 12 kW inverter. Over 16 days the Sigen AI total matched Amber's bills to 0.6 percent, −$37.92 against −$37.70.

A worked example, 3:10 am on 21 November. That five minutes was a price spike, with bb at $9.61 and ss at $8.65 per kWh. The house used 0.022 kWh with no sun.

  • No battery bought 0.022 kWh at $9.61, so $0.22.
  • Sigen AI's battery covered the house and sold almost nothing, so about $0.00.
  • Had the shadow sold at the 5 kW limit, 5Δ=0.425\Delta = 0.42 kWh at $8.65 would have earned $3.60 in those five minutes.

Midnight

A controller can make today look cheap by emptying the battery, so the energy left at midnight has to be valued. Take ESE_S and EShE_{Sh} as the real and virtual battery's energy at two seconds to midnight, and FF as the forecast just after midnight. Then C(E)C(E) is the cost of tomorrow's best plan over FF, starting from level EE, finishing at ESE_S and with no wear charge. Sigen AI is the reference and stays as it is. The shadow pays or is paid only the difference its level makes to tomorrow, never a second day's bill. Hindsight is one plan over today's actuals followed by FF.

Sdadj=SdS^{\text{adj}}_d = S_d
Shdadj=Shd+[ C(ESh)−C(ES) ]Sh^{\text{adj}}_d = Sh_d + \big[\,C(E_{Sh}) - C(E_S)\,\big]
Hdadj=Htoday+[ Htomorrow−C(ES) ]H^{\text{adj}}_d = H_{\text{today}} + \big[\,H_{\text{tomorrow}} - C(E_S)\,\big]

Every strategy gets the same horizon, the same forecast and the same finishing level. Hindsight plans today and tomorrow in one go, so it can choose its own level at midnight, and that means the adjusted shadow can never beat it.

A worked example, 21 November. The shadow's day cost −$10.23 and it finished with 14.4 kWh against Sigen AI's 23.4 kWh. Tomorrow planned from 14.4 kWh came to −$2.13, and from 23.4 kWh to −$2.62.

Shadj=−10.23+(−2.13−(−2.62))=−10.23+0.49=−$9.74Sh^{\text{adj}} = -10.23 + \big(-2.13 - (-2.62)\big) = -10.23 + 0.49 = -\$9.74

So the shadow is judged on −$9.74, against Sigen AI's −$5.01.

Why not just use a price? The first version did, valuing the difference at the average forecast buy price over the next day divided by the efficiency, λ=bˉ/η\lambda = \bar b/\eta, and multiplying it by the difference in stored energy. On 25 November that credited the shadow $3.36 for 12 kWh, about 28 cents a kWh, and made it beat hindsight, which can't be right. Planning tomorrow twice valued 9.3 kWh at $1.02 that day, about 11 cents a kWh, somewhere between the sell and buy prices, which is where it should be. A suggested middle ground, λ=max⁡(bˉ/η, ηsˉ)\lambda = \max(\bar b/\eta,\ \eta \bar s), wouldn't have changed anything, because at every midnight bˉ/η\bar b/\eta (0.175 to 0.729) was well above ηsˉ\eta \bar s (0.032 to 0.487).

Hindsight

Hindsight is the same planner given the day's actual prices, solar and usage, starting from the real battery's first five-minute reading of the day. It runs without the wear charge for the benchmark, because Sigen AI's total is electricity only, and with it for reference.

VersionStepsWhen
Half-hourly, joint96, today then tomorrow's forecastEvery night
Five-minute, joint576, today in five minutes then tomorrow's half hours repeated six timesIn the replays, 8 to 18 seconds a solve

It's the best within this model rather than an absolute ceiling, so a real controller can occasionally beat it. In the replay, every day came out in this order.

Shadj  ≥  H30adj  ≥  H5adjSh^{\text{adj}} \;\ge\; H^{\text{adj}}_{30} \;\ge\; H^{\text{adj}}_{5}

Grading and statistics

With NN, SS and HH the no battery, Sigen AI and hindsight costs over a set of days, and each day's gap di=Si−Shiadjd_i = S_i - Sh^{\text{adj}}_i, positive when the shadow did better, these are the measures.

capture=∑(N−S)∑(N−H),regret=∑(S−H)days\text{capture} = \frac{\sum (N - S)}{\sum (N - H)}, \qquad \text{regret} = \frac{\sum (S - H)}{\text{days}}
dˉ=1n∑di,s2=1n−1∑(di−dˉ)2\bar d = \frac1n \sum d_i, \qquad s^2 = \frac{1}{n-1}\sum (d_i - \bar d)^2
r=∑i≥2(di−dˉ)(di−1−dˉ)(n−1) s2r = \frac{\sum_{i\ge2} (d_i - \bar d)(d_{i-1} - \bar d)}{(n-1)\,s^2}
neff={n 1−r1+rif r>0notherwisen_{\text{eff}} = \begin{cases} n\,\dfrac{1-r}{1+r} & \text{if } r > 0\\ n & \text{otherwise}\end{cases}
95 percent range=dˉ±t0.975, neff−1 sneff\text{95 percent range} = \bar d \pm t_{0.975,\ n_{\text{eff}}-1}\,\frac{s}{\sqrt{n_{\text{eff}}}}

Days that resemble each other count for less than independent ones, which is what neffn_{\text{eff}} allows for, and the range uses Student's t rather than the usual two standard errors because the samples are small. It's still approximate. At 30 days I'll add a Newey–West standard error with a bandwidth of three days, and at 45 to 60 days a block bootstrap with three to five day blocks. A decision needs them all to agree.

A worked example, 21 to 25 November. The five gaps were d=(4.73, 0.15, −1.42, 1.68, 0.54)d = (4.73,\ 0.15,\ -1.42,\ 1.68,\ 0.54), so dˉ=1.14\bar d = 1.14 and s=2.30s = 2.30. The autocorrelation rr was negative, so neff=5n_{\text{eff}} = 5, and with t0.975,4=2.776t_{0.975,4} = 2.776 the range is 1.14±2.776×2.30/5=1.14±2.851.14 \pm 2.776 \times 2.30/\sqrt{5} = 1.14 \pm 2.85, from −$1.71 to +$3.99.

How many days does it need? With daily noise σ\sigma of about $1 and a bar δ\delta of 70 cents, the textbook answer is 16.

n=((z0.975+z0.80) σδ)2=((1.96+0.84)×10.70)2≈16n = \left(\frac{(z_{0.975} + z_{0.80})\,\sigma}{\delta}\right)^2 = \left(\frac{(1.96 + 0.84)\times 1}{0.70}\right)^2 \approx 16

That's the minimum for an 80 percent chance of spotting a real 70 cent lead. Because the noise is only an estimate and days resemble each other, the target is 30, and 45 to 60 is better.

The shadow only takes over if it passes all three of these.

  1. The lower end of the 95 percent range is above 70 cents a day.
  2. The average gap on days without a spike, any price of $1 a kWh or more, is above zero.
  3. The average gap with its best day left out is above zero.

The replay of 21 to 25 November passes the second and third at 24 cents each, and fails the first at −$1.71.

Results so far

Sigen AI against hindsight, 9 to 25 November, 17 days, hourly, using Amber's bills. Below zero means the house earned money, and it's energy only with no supply charge.

MeasureTotal
No battery−$2.40
Sigen AI−$42.96
Modelled best−$72.97
Capture57 percent, or 61 percent without the spike day
Regret$1.77 a day, approximately $0.94 to $2.59

The shadow against Sigen AI, replaying 21 to 25 November five minutes at a time, exactly as the live shadow works.

DayNo batterySigen AIShadowMidnightShadow adjustedHindsight 30 minHindsight 5 minStored at midnight, Sigen AI and shadow
21 Nov, spike−$0.35−$5.01−$10.23+$0.49−$9.74−$11.24−$11.5323.4 and 14.4 kWh
22 Nov$0.71$0.27−$1.13+$1.25$0.12−$0.23−$0.3022.1 and 7.7 kWh
23 Nov$1.44−$0.01$1.45−$0.04$1.41−$0.41−$0.4645.8 and 46.5 kWh
24 Nov−$0.99−$4.65−$5.97−$0.36−$6.33−$7.03−$7.1426.4 and 30.5 kWh
25 Nov−$1.76−$5.24−$4.76−$1.02−$5.78−$6.92−$6.9622.6 and 31.9 kWh
Total−$0.95−$14.64−$20.64+$0.32−$20.32−$25.83−$26.39

That's a gap of $1.14 a day with an approximate range of −$1.71 to +$3.99, or 24 cents without the spike day. Against half-hourly hindsight Sigen AI captured 55 percent and the shadow 78 percent. These days are background only, because Solcast's forecast for them was rebuilt from its own records, which knew a little more than the forecast did at the time.

How the replay's answer moved each time I fixed something.

VersionWhat changedShadow ahead per day
1First try, no midnight adjustment$0.37
2Review fixes, but the solar forecast was missing$0.05
3Solar forecast back, replanning every 30 minutes$1.17
4The live loop exactly, every five minutes, tomorrow planned twice at midnight$1.14

What went wrong along the way

ProblemWhat it didFound by
Energy left at midnight was ignoredA controller could look better by emptying the batteryThe first review
Each five minutes was priced at the next five minutes' priceA kWh sold just before the 3:10 am spike would have counted as $8.65 instead of 71 centsThe first review
Two standard errors on five daysFar too confident. Student's t for five days is 2.776The first review
Home Assistant never kept Solcast's half-hourly forecastReplays planned for no sun. On 21 November they were told 0 kWh, Solcast had forecast 67 kWh and the panels made 60.5 kWhMe, fixing the above
The live planner started from the real battery's level, then acted on the virtual oneWrong decisions whenever the two drifted apart, by 15 kWh on the evening of 21 NovemberMe, checking the second review
The second review worried the planner might charge and discharge at onceIt can't, because EMHASS has a switch for thatThe EMHASS source code

What it can't tell me

  • Sigen AI may see things the shadow doesn't, like its own forecasts or the battery's state. If it wins, that's a fair answer.
  • The usage forecast is a plain 14-day hourly average, which handicaps the shadow.
  • Half-hourly hindsight smooths out spikes shorter than half an hour, which is why there's a five-minute version.
  • The no battery case uses a flat 5 kW export limit and works out its flows rather than metering them.
  • The wear charge of 1.5 cents a kWh each way and the efficiency are assumptions.
  • The midnight valuation uses the forecast as it stands a few minutes after midnight and assumes tomorrow's plan goes perfectly.
  • Working out neffn_{\text{eff}} from only a few days is itself a bit rough.

Nothing in any of this can control the real battery. There's also a plain-text copy of this page as a GitHub gist, for anyone who'd rather read the formulas as source.

Keep reading the series

Keep reading

Everything →