Skip to content

feat(history): one account per battery for the battery-storage tax figure - #1953

Merged
frahlg merged 1 commit into
masterfrom
claude/battery-return-per-battery-032049
Nov 2, 2026
Merged

frahlg merged 1 commit into
masterfrom
claude/battery-return-per-battery-032049

Conversation

@frahlg

@frahlg frahlg commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

Stacked on #1950 (feat/battery-return-tax, up to a17faf1). Merge that first. GitHub then moves this PR's base to master.

Why

#1950 refuses any site with more than one battery: one shared account would let battery A's exports carry battery B's grid energy, which overstates the figure. Fredrik asked for this follow-up on 2 November 2026. With an account for each battery, those sites get a figure.

What

  • Account per battery meter. b077033 joins assets of one kind that never read at the same time into one meter. Each battery meter now has its own account, size floor and run efficiencies. 3950f98 and a17faf1 made that method; here it runs once per battery.

  • Bounds per battery and hour:

    • grid → b ≥ clamp(charge_b − solar − car discharge − the other batteries' discharge, import)
    • b → grid ≥ clamp(export − solar − car discharge − the other batteries' discharge, discharge_b)
  • The account per battery. Each hour plays in the order that credits least:

    1. Other energy arrives.
    2. What b sends to the grid leaves, taking other energy first.
    3. Grid energy arrives after the least efficiency of b's own run of readings. The loss stays in the account as other energy.
    4. What b keeps at home (its discharge minus its own b → grid) leaves, taking grid energy first.
    5. A full battery sheds grid energy first.
  • Missing readings and new assets. When battery b's readings are missing, or when another asset takes over in b's meter, only b's account resets and only b's run ends. The hour counts for no battery, because every bound needs every battery's discharge.

  • Size per battery.

    • A driver's battery_capacity_wh goes to the battery meter that has an asset with the driver's name.
    • The ledger asset's label is the driver name, written on every reading (go/cmd/ftw/energy_history.go). So a new identity behind the same driver keeps the size, even when the new asset has no label yet.
    • I used the label instead of the devices table. Two drivers on one inverter share a devices row whose driver_name flips between them, but each driver keeps its own asset.
    • A meter named by two drivers takes the larger size, which counts lower.
    • A loadpoint's driver is skipped: its size is a car's.
  • No figure, with a reason, in two cases:

    • battery_size: a battery meter read in the year has no size.
    • battery_unread: a sized driver did not read a battery in the year.

    A battery first read after the year counts as absent, as feat(history): the figure for Skatteverket's energy tax refund after battery storage #1950 already treats solar. The batteries problem is gone.

  • API. batteries is now a list. Each entry has the label, the latest asset id, whether it is sized, the size, the charge-weighted efficiency and the returned energy. A new unread field lists drivers that read no battery. The top-level efficiency and capacity_wh moved into the list.

  • CSV.

    • One row per battery and hour, with a battery column (the meter's latest asset id), other_batteries_discharge_wh and that battery's run efficiency.
    • Each row's grid_to_battery_wh and battery_to_grid_wh can be rebuilt from that row alone. returned_wh also needs the same battery's previous row, for what it held.
    • The figure is the sum of returned_wh. With one battery there is one row per hour, as before.
  • View. Several batteries get one line each. Each reason has its own message, which names the drivers. No month rows appear when there is no figure. The note says each battery keeps its own account.

Evidence

  • One-battery results are unchanged against a17faf1.
    • The existing tests keep every expected value. Only Site literals and field paths such as r.Batteries[0].Efficiency changed, and len(r.Batteries) in TestANewIdentityContinuesTheMeter.
    • I ran the old and new Build on 1000 random one-battery sites, 500 of them with an identity change. The sites had gaps, spread counters, missing points, V2X, dark hours, and hours that both charge and discharge. Every hour (including its run efficiency), month, size, efficiency and figure matched bit for bit. The probe is not committed.
  • Home box. The read-only HTTP ledger (GET only) gives 0.0 kWh for 2026, as on the base. It has one battery meter over a MAC-based and a serial-based asset, both labelled with the driver's name. /api/config answered 401, so the 9.6 kWh size was assumed.
  • New tests, worked out by hand:
    • The second review's case: A charges only from solar and exports 9 kWh each evening. B charges 10 kWh from the grid each night and never discharges. The figure is 0.
    • Two batteries charged from the grid that both export: 3000 Wh. A gives the one-battery 2000 Wh; B, at half the power and size, gives 1000.
    • An export that either battery could have sent: 0 for both. The bounds give A 1.5 kWh an hour and B nothing; A's export takes other energy, and what stays at home takes the grid energy.
    • A gap in B resets only B's account. It splits B's readings into two runs, the second with efficiency (9 − 5)/5 = 0.8. A gives 2000 Wh, B 3000.
    • Sizes and unread batteries.
    • The API with two batteries and a car charger that has a size.
  • Checks run: go test ./internal/batteryreturn ./internal/api and npm test pass. make verify passes on the rebased tree.
  • Browser. Reviewed through the real ftw binary against 30 days of seeded ledger data with two batteries:
    • The figure with one line per battery, and a battery without a size.
    • Desktop and 375 px wide (no sideways scroll), dark and light.
    • After rebasing onto 3950f98 I checked again: the figure, the per-battery lines and the CSV (2 × 7 332 + 1 rows, summing to the figure, efficiency per hour) all render. The seeded battery A gives 0 because the seed has it gain energy every day, so its account stays full and sheds grid energy first. a17faf1 changed no view code.

Independent review

A Sonnet subagent tried to make the code overstate in three rounds. It compared against the lowest figure the flows allow, played at minute level.

  • Round 1 ran on the proportional mix. The per-battery code was sound, and three overstatements in the method went to feat(history): the figure for Skatteverket's energy tax refund after battery storage #1950. 3950f98 fixed them.

  • Round 2 ran on 3950f98 per battery. The per-battery bounds held, with 0 violations on 12 000 sites with 1 to 3 batteries. Three overstatements also hit one battery:

    • discharge played before charge within an hour (4000 Wh against 0);
    • the charge loss dropped from the account (1230.8 Wh against 0);
    • the account carried over at a handover within an hour.

    I reproduced the first two. The reviewer's repair went into a17faf1, and this PR carries it per battery.

  • Round 3, on b4ace70, found no overstatement:

    • Random fuzz: about 90 000 sites with 1 to 3 batteries and with solar, no solar, a car, gaps and a larger configured size. None went above the lowest truth or the bus-proportional truth. The worst ratio was 0.895.
    • Hill climbs: 1, 2 and 3 batteries, with a car and with gaps; at most 0.00 Wh over. The one- and two-battery climbs reached the truth exactly.
    • Bounds: 0 violations in about 1.01 million complete battery hours.
    • Earlier cases: every case from rounds 1 and 2, and a handover within one hour, now stay at or below the lowest truth.
    • One case remains by design. If a battery is swapped without its asset id changing and with no unread hour, the old content carries over. Nothing in the readings can show such a swap.

For the owner

🤖 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:20:03.003654Z 9bcfe12 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.

@frahlg
frahlg force-pushed the claude/battery-return-per-battery-032049 branch from 9bcfe12 to 37be3d7 Compare November 2, 2026 11:35
@frahlg frahlg changed the title feat(history): one mix per battery for the battery-storage tax figure feat(history): one account per battery for the battery-storage tax figure Nov 2, 2026
@frahlg
frahlg force-pushed the claude/battery-return-per-battery-032049 branch from 37be3d7 to b4ace70 Compare November 2, 2026 11:57
…gure

Sites with more than one battery had no figure, because one shared account
let one battery's exports carry another's grid energy. Each battery meter
now keeps its own account, size and run efficiencies, and for each battery
solar, a car and the other batteries count first in every hour. A driver's
battery size goes to the meter with an asset of its name, so a new identity
keeps it. The hourly CSV has one row per battery and hour.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@frahlg
frahlg changed the base branch from feat/battery-return-tax to master November 2, 2026 15:14
@frahlg
frahlg force-pushed the claude/battery-return-per-battery-032049 branch from b4ace70 to d833173 Compare November 2, 2026 15:14
@frahlg
frahlg merged commit 9724190 into master Nov 2, 2026
22 checks passed
@frahlg
frahlg deleted the claude/battery-return-per-battery-032049 branch November 11, 2026 09:12
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.

1 participant