Skip to content

feat(planner): count the energy tax refund on battery energy sent back - #1956

Open
frahlg wants to merge 4 commits into
feat/battery-return-taxfrom
claude/planner-battery-tax-refund-57aae9
Open

frahlg wants to merge 4 commits into
feat/battery-return-taxfrom
claude/planner-battery-tax-refund-57aae9

Conversation

@frahlg

@frahlg frahlg commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

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 to master.

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:

    • per slot the battery took from the grid only what solar could not have supplied, and gave to the grid only what solar could not have exported;
    • other energy comes in whole; the export leaves and takes other energy first; grid energy comes in after the round-trip loss, with the loss kept as other energy; the house takes its share, grid energy first;
    • the account holds what the battery holds, so what does not fit sheds grid energy first. History caps at the battery's size, so the plan sheds sooner and never counts more for the same flows, beyond the per-slot netting and efficiency gaps below.

    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_ore and each slot's battery_return_wh, whichever engine made it, so an Energyplan plan shows what it earns too. total_cost_ore stays what crosses the meter.

  • DP shadow: the terminal-corrected figures net out each plan's refund, so energyplan_minus_core_ore_terminal_corrected shows 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 than grid_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.SiteOf replaces 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.
  • Tests: 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).
  • DP time, 193 quarters, 201×401, M5, measured side by side: 77 ms without the refund (same as the base), 498 ms with it (×6.5). The account moves the share on every action, so each action is valued at each of the five levels. Not timed on a Raspberry Pi.
  • Share levels: the refund is convex in the share, so interpolating between too few levels overstates it. On October's prices three levels cost a 10 kWh villa 0.3–0.5 kr a month (the plan sent energy to the grid for a refund it then did not get); five and nine levels cost nothing and gave a 20 kWh battery the same gain within 0.3 kr. So five.
  • Lower resolution instead (measured on 3950f98's order): at 101×201 (93 ms, about today's cost) or 141×281 the refund plan loses 0.6–0.7 kr a month against today's 201×401 plan for a 10 kWh villa with solar, and a 20 kWh battery gains 9–11 kr instead of 12. So this PR keeps 201×401.

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.

Prices Zone Battery, load Sun Exported kWh, without → with Returned kWh Full cycles Gain, kr
5 Jan – 7 Feb 2026 (34 d) SE3 10 kWh, villa – 1.7 → 2.6 0.0 → 0.2 43.6 → 43.6 −0.05 (−0.04)
SE4 10 kWh, villa – 5.3 → 6.3 1.5 → 1.8 43.8 → 43.8 −0.07 (−0.03)
SE3 20 kWh, ×0.4 – 44.6 → 70.0 20.8 → 44.4 21.6 → 22.7 3.03 (5.29)
SE4 20 kWh, ×0.4 – 58.3 → 80.6 34.5 → 55.4 21.9 → 22.7 2.44 (4.44)
SE3 10 kWh, villa 6 kWp 2.4 → 2.7 0.0 → 0.3 42.2 → 42.2 0.04 (0.06)
SE4 10 kWh, villa 6 kWp 6.6 → 8.0 1.6 → 2.8 42.4 → 42.5 0.20 (0.32)
SE3 20 kWh, ×0.4 6 kWp 51.3 → 82.5 26.0 → 54.5 19.0 → 20.4 3.28 (6.01)
SE4 20 kWh, ×0.4 6 kWp 62.4 → 87.6 36.1 → 59.3 19.2 → 20.3 3.20 (5.42)
October 2026 (31 d) SE3 10 kWh, villa – 0.7 → 0.7 0.0 → 0.0 53.1 → 53.1 0.01 (0.01)
SE4 10 kWh, villa – 3.8 → 5.4 1.3 → 1.8 58.4 → 58.6 −0.09 (−0.03)
SE3 20 kWh, ×0.4 – 82.1 → 166.6 42.8 → 118.6 23.6 → 27.8 9.19 (16.46)
SE4 20 kWh, ×0.4 – 158.5 → 233.1 110.8 → 179.0 28.3 → 31.8 8.32 (14.87)
SE3 10 kWh, villa 6 kWp 2.3 → 2.4 0.0 → 0.0 48.2 → 48.3 −0.01 (−0.01)
SE4 10 kWh, villa 6 kWp 7.9 → 10.5 1.5 → 2.4 52.8 → 53.2 0.07 (0.16)
SE3 20 kWh, ×0.4 6 kWp 148.7 → 211.3 30.7 → 87.2 22.9 → 26.5 9.92 (15.34)
SE4 20 kWh, ×0.4 6 kWp 213.9 → 270.0 64.5 → 113.7 27.8 → 30.6 5.28 (10.00)

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

  • Energyplan does not plan for the refund. The ordered account is not convex. Energyplan's single-battery kernel is a convex curve DP and its fleet path a MILP, so it needs its own method (a share-level DP, or binaries). On default (Energyplan) sites this PR changes the reported refund and the shadow comparison, not the plan. Core DP sites and every fallback plan count it.
  • Time: ×6.5 on the M5. A Raspberry Pi 4 may pass Core's 2 s DP budget for a 48 h plan, and then Core publishes its reserve plan. Time it on the home box before merging.
  • Slot by slot, not hour by hour: in an hour that mixes solar and grid charging the plan can count a little more than History's hourly netting.
  • History's grid energy enters at the least efficiency its run of readings allows, a little under the battery's (about 0.899 against 0.9025 after a year of daily cycles on 10 kWh), so the plan can count up to about 0.5 % more.

For the owner

  1. Merge at all? My recommendation is no, not now. The refund is worth nothing to a 10 kWh villa battery and 2–10 kr a month to a large battery beside a small load (table), and only on Core DP sites, since Energyplan does not plan for it. In return the DP takes about six and a half times as long when the refund is on, with a Raspberry Pi 4 likely past Core's 2 s budget. If you want part of it, the reported refund on every plan (battery_return_ore, from Core's count) costs no planning time and could ship alone.
  2. Rate source. The least tax any Swedish household pays (26.4 öre in 2026), from a table each new year's release must update. Alternatives: the standard rate (36.0 öre; overstates by 9.6 öre in the northern municipalities, which include Torsby and northern Dalarna in SE3), or a setting. The 2027 rate is not law yet (a March 2026 memo proposes 36.0).
  3. On by default for Swedish SEK sites with one sized battery and battery export allowed. It assumes the owner applies to Skatteverket. Choosing active arbitrage is the owner's consent to selling battery energy; History shows what to claim.
  4. Energyplan. Build the refund into the worker (paired PR, a new handshake feature), or leave it while the value is this small. The shadow's corrected figure will show the gap on real sites.

🤖 Generated with Claude Code

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-02T10:45:47.622539Z 1b77fd9 PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread go/internal/mpc/battery_return.go Outdated
Comment on lines +106 to +107
share, wh = returnMixStep(slots[i], a.BatteryW, a.GridW,
soc*p.CapacityWh, share, slots[i].DurationHours(), etaC)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

@frahlg
frahlg force-pushed the claude/planner-battery-tax-refund-57aae9 branch from 1b77fd9 to 46cb988 Compare November 2, 2026 11:39
frahlg and others added 3 commits November 2, 2026 12:53
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]>
@frahlg
frahlg force-pushed the claude/planner-battery-tax-refund-57aae9 branch from 46cb988 to 3eb16e7 Compare November 2, 2026 12:07
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 miravoss26 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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: newReturnFlows in the DP gets p.ChargeEfficiency/p.DischargeEfficiency as-is, while walkBatteryReturn defaults 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, and tried is 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 SiteOf into the batteryreturn package.
  • 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants