Repository navigation
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 1b77fd9909
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| share, wh = returnMixStep(slots[i], a.BatteryW, a.GridW, | ||
| soc*p.CapacityWh, share, slots[i].DurationHours(), etaC) |
There was a problem hiding this comment.
Apply executed PV caps when replaying the battery mix
For Energyplan actions with PVCurtailActive, this passes the original solar forecast to returnMixStep, even though a.GridW already reflects curtailed generation. For example, with 5 kW forecast solar capped to 500 W, a 3 kW battery charge and no household load imports 2.5 kW, but this replay counts none of that charge as grid energy because it subtracts the full 5 kW forecast. Subsequent exports therefore underreport battery_return_wh and battery_return_ore, and distort the refund-adjusted shadow comparison. Apply the effective PV cap using the same replay logic as ValidatePlan before calculating the mix.
Useful? React with 👍 / 👎.
1b77fd9 to
46cb988
Compare
A Swedish household gets back the energy tax on grid electricity it stored in a battery and later sent back to the grid. The Core DP now keeps the grid share of what the battery holds as a second state (0, 1/2, 1) and, in active arbitrage, credits each kWh of stored grid energy it sends to the grid, counted as History counts it (#1950): solar first, a proportional mix, no refund for losses. Without a refund it runs the old arithmetic. The credit is the least tax any Swedish household pays that year (a dated table less the 9.6 öre northern deduction), never more than the grid tariff, for SE1-SE4 priced in SEK with one sized battery. The start share comes from History's last counted hour, in the background; charge since then counts as other energy. The DP leaves the refund out while a car is plugged in and across New Year. Every published plan reports the refund it earns, and the Core DP shadow nets it out of its corrected comparison. Energyplan does not plan for it yet. Co-Authored-By: Claude Opus 5.5 <[email protected]>
History (#1950, 3950f98) now counts what the house takes from the battery as grid energy first and what goes to the grid as other energy first; grid energy enters after the loss, other energy whole, and a full battery sheds grid energy first. The proportional share let the plan count more than the owner can claim. The Core DP keeps its grid-share state but moves it by that order on every action, from flows built once per slot and action. The planner starts from History's grid and other energy rather than a share: what the battery gave out since the count took grid energy, what it took in is other energy. With no refund the DP runs the old arithmetic; with it a 48 h plan takes about five times as long on an M5. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… energy History (#1950, a17faf1) now plays each hour as: other energy in, the export out (other energy first), grid energy in after the least efficiency with its loss kept as other energy, the house (grid energy first), then a full battery sheds grid energy first. A planner slot moves the battery one way, so only the charge loss changes the plan's count: it now enters as other energy, and the cap at what the battery holds sheds the same amount of grid energy. Buying at night and selling at the peak from an empty battery returns (2η − 1)/η of the export, as History does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
46cb988 to
3eb16e7
Compare
With History's newest order the refund is convex in the grid share, and linear interpolation between 0, 1/2 and 1 overstated it: on October 2026 prices a 10 kWh villa lost 0.3-0.5 kr a month to exports for a refund it did not get. Five and nine levels lose nothing. A 48 h plan at 201x401 now takes 498 ms with the refund on an M5, against 77 ms without. Co-Authored-By: Claude Opus 5.5 <[email protected]>
miravoss26
left a comment
There was a problem hiding this comment.
Adds the Swedish energy-tax refund on battery-stored grid energy to the planner: a second DP state (grid share of what the battery holds, 5 levels), only in arbitrage mode, fed by an hourly background count from the ledger. The plan reports battery_return_ore and per-slot battery_return_wh, kept out of TotalCostOre.
- Safe by default: zero value for anything but one battery of known size, SE zones priced in SEK, a count under 2 h old. A failed count or flow read falls back to no refund. Credit is capped at the grid tariff. Good.
- Low:
newReturnFlowsin the DP getsp.ChargeEfficiency/p.DischargeEfficiencyas-is, whilewalkBatteryReturndefaults them to 0.95 when <=0. If Params can reach the DP with 0 efficiency, the DP and the annotation disagree. Probably normalised upstream, worth confirming. - Low:
count()runs in a goroutine with a 1 min timeout, andtriedis set before it starts, so a failed count waits for the next hour as the comment says. Fine, just noting it is intentional. - Perf: work and memory budget are scaled by the share levels, and the refund is dropped when a car is plugged in, so the 64 MiB guard still holds. I did not run the solver.
- Security screen: no secrets, no new deps, no network calls, no authz changes. The API handler refactor only moves
SiteOfinto thebatteryreturnpackage. - Process: stacked on #1950, so merge that first. Core + native-solver checks are green. Converter/DuckDB checks fail on this PR and on #1955, so they look unrelated to this diff.
Tests cover credit rules, the planning hook and the DP math. Safe to merge from my read once #1950 is in. The tax logic itself (11 kap. 13 § LSE ordering rules) is worth a domain check by Fredrik, which I can't do.
Why
A Swedish household gets back the energy tax on grid electricity it stored in a battery and later sent back to the same grid (11 kap. 13 § LSE; Skatteverket). #1950 adds the yearly figure to History. The planner did not know about the refund, so in active arbitrage it undervalued buying, storing and selling back.
Stacked on #1950 (
feat/battery-return-tax, a17faf1): the plan counts the refund in History's order and starts from its count. Merge #1950 first; this PR's base then moves tomaster.What
Core DP: a second battery state, the grid part of what the battery holds (share in steps of ¼, interpolated). Only in mode
arbitrage, the only mode that sends battery energy to the grid. The plan counts the refund in the order History counts it since a17faf1:A slot moves the battery one way only, so the in-slot order matters only for the charge loss. Buying at night and selling at the peak from an empty battery returns (2η − 1)/η of the export, as History does.
With no refund the DP runs the old arithmetic: plans are byte-identical to the base on six scenarios (three modes, with and without a car) and the time is unchanged.
Every plan carries
battery_return_oreand each slot'sbattery_return_wh, whichever engine made it, so an Energyplan plan shows what it earns too.total_cost_orestays what crosses the meter.DP shadow: the terminal-corrected figures net out each plan's refund, so
energyplan_minus_core_ore_terminal_correctedshows what Energyplan leaves when the refund matters.Rate: a dated table in
batteryreturn(2024 42.8, 2025 43.9, 2026 36.0 öre/kWh excl. VAT, Skatteverket's "Skatt på el"), less the 9.6 öre northern deduction: the least any Swedish household pays (26.4 öre in 2026). Never more thangrid_tariff_ore_kwh, which holds the tax today. A year past the table keeps 2026's rate.Who gets it: zone SE1–SE4 priced in SEK, one battery with its size set (what History needs for a figure), mode arbitrage. No new setting.
Left out: while a car is plugged in (the DP with a car is already near Core's 2 s budget), and in a plan that crosses New Year (History starts each year with the battery full of energy from elsewhere).
Start: History's grid and other energy after its last complete hour, counted in the background once an hour. The plan starts low: what the battery gave out since then took grid energy, what it took in since then is other energy, and all of History's other energy is taken to be there. No refund until a count has run, or when it is more than two hours old.
batteryreturn.SiteOfreplaces the API's copy, so History and the planner read the same site. The two params persist in diagnostics and pass validation.Evidence
Local, on this branch:
make verify: clean (vet, tests, build) on 46cb988's tree.go/internal/mpc/battery_return_test.go(the refund makes a too-thin spread pay; stored solar never counts; other energy goes to the grid first; the house uses grid energy first, including feat(history): the figure for Skatteverket's energy tax refund after battery storage #1950's night-grid / morning-house / midday-sun / evening-export day, which returns nothing; night buying and peak selling return (2η − 1)/η; the start counts low; passive, cheap-charge and self-consumption ignore it; a plugged-in car leaves the plan as it was; Core's count of a plan matches the DP's; the service asks over the plan window and starts from the hook's state),go/internal/batteryreturn/planning_test.go(rates, New Year on Swedish time, site, held energy),go/cmd/ftw/battery_return_planning_test.go(zone, currency, tariff cap, one battery, no credit before a count, flows since the count, stale count; passes under-race).Replay. Closed loop with Core DP at 201×401 on real quarter prices (elprisetjustnu), replanned each hour, tomorrow's prices from 13:00, perfect forecasts. Import (spot + 25 öre network + 36 öre tax) × 1.25, export at spot. Battery 0.10–0.95, 0.95/0.95, power C/2. Heated villa load, mean 1.6 kW (×0.4 for a small load), the same in both months. Solar: 0 or a 6 kWp south roof at half of clear sky near Stockholm. Both runs count the refund they earn by a17faf1's order; gain is grid cost less refund at 26.4 öre (36.0 öre in brackets, a household without the northern deduction), with the end energy valued at the last terminal price.
Without the refund the plan still sends some stored grid energy back, and the owner gets that refund anyway; both runs count it. Under History's order much of what goes to the grid earns nothing: the charge loss and any solar leave first. A 10 kWh battery in a heated villa sends almost nothing to the grid, since the house takes the evening's energy, worth far more than the refund: the gain is nil (−0.09 to 0.20 kr a month). A large battery next to a small load gains 2.4–3.3 kr a month in January and 5.3–9.9 kr in October, with up to four more full cycles. October uses January's load, so it overstates what an October house leaves for export.
Known gaps
For the owner
battery_return_ore, from Core's count) costs no planning time and could ship alone.🤖 Generated with Claude Code