Repository navigation
feat(history): the figure for Skatteverket's energy tax refund after battery storage - #1950
Conversation
…battery storage A Swedish household gets back the energy tax on grid electricity it stored in a battery and later fed back to the same grid (11 kap. 13 § LSE). History now works out that figure for one calendar year, month by month, with the hourly basis as CSV. The count stays low wherever the readings cannot tell where energy went. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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: c66a28e008
ℹ️ 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".
| // out = efficiency × in − what the battery gained, and it gained at | ||
| // most a full battery. | ||
| r.Efficiency = clamp((out-r.CapacityWh)/in, 1) |
There was a problem hiding this comment.
Account for unobserved charging in the efficiency estimate
When the battery charges during telemetry outages and discharges after readings resume, out includes those discharges but in excludes their charging. Subtracting just one battery capacity across all readable segments can therefore inflate Efficiency to 100%, leaving fictitious grid energy in the mix after the actual grid charge has been consumed. For example, a 10 kWh battery with two unobserved solar refills followed by measured household discharges, then separate 10 kWh grid and solar charges each yielding 9 kWh, produces 20 kWh measured input and 38 kWh output. If the grid charge serves the house and only the final solar charge is exported, this calculation nevertheless reports about 818 Wh returned from the grid. Account for unknown initial energy in each uninterrupted segment when deriving efficiency.
AGENTS.md reference: AGENTS.md:L26-L26
Useful? React with 👍 / 👎.
The home box's Sungrow moved from a MAC-based to a serial-based device id in September, so the ledger holds two grid meters, batteries and solar assets. The report took that for two batteries and for meters that fell silent, and gave no figure for the year. Assets of one kind that never read at the same time now form one meter; the hours between them are still owed and stay incomplete. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…than the readings allow The proportional mix overstated. Nothing says how much the battery holds, and with the content taken as a full battery, grid energy the house had already used stayed in the mix and was credited to the evening's export: 5 kWh from the grid at night, 5 kWh to the house in the morning, 5 kWh of solar at midday and 5 kWh to the grid in the evening returned about 0.6 kWh a day where the truth is 0. What the house takes from the battery now counts as grid energy first, and what goes to the grid as other energy first. Grid energy enters after the least efficiency its own run of battery readings allows, so a short run or one cut by a silent battery cannot borrow an efficiency; other energy enters whole. A full battery sheds grid energy first. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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]>
…p the charge loss A third review found the strict account could still overstate: - It played discharge before charge, so the house never took grid energy that came in earlier in the same hour (4 kWh where the least figure the flows allow is 0). - It dropped the charge loss, so the account fell below what the battery held; the house then took other energy the real battery still had, and the export reached grid energy early (1230.8 Wh against 0). - A new battery asset that took over within an hour kept the old account. Each hour now plays: 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's share (grid energy first), then a full battery sheds grid energy first. A battery asset that takes over starts a new account. The review's cases are tests and fail on the previous code. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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]>
Why
A Swedish household can get back the energy tax on grid electricity it stored in a battery and later fed back to the same grid (Skatteverket; 11 kap. 13 § LSE, in force from 2018, prop. 2017/18:294). The owner applies once per calendar year, within three years after it ends. Losses are not refunded. Skatteverket prescribes no way to measure the energy, the applicant must be able to show the figure, and a wrong figure can bring a tax surcharge. FTW's energy ledger holds what is needed.
What
GET /api/energy/battery-return?year=and/api/energy/battery-return.csv, Read tier, listed in the agent catalogue.go/internal/batteryreturn; queries stay instate(LoadEnergyHistoryContext, month by month).How the figure is counted
Where the readings cannot tell where energy went, it counts low:
charge − solar − car discharge(at most the import), and sent to the grid at leastexport − solar − car discharge(at most the discharge).(discharge − battery size) / chargeper run between gaps, and its loss stays in the battery as other energy, so losses never count and the account never falls below what the battery holds. Each hour plays in the order that credits least: other energy in, the export out (other first), grid energy in, the house's share (grid first), then a full battery sheds grid energy first. A battery asset that takes over from another starts a new account.measuredandintegratedledger rows count. Gap markers and counter deltas spread over an outage do not. Every grid, battery and solar meter seen in the year must read every hour, and the next hour too, since the ledger marks an outage only when the reading returns. A charger that ever gave energy back must too.Evidence
go test ./internal/batteryreturn ./internal/apiandnpm testpass;make verifywas clean on the final tree before the rebase ontof3b48e48(narrow tests re-run after it).For the owner
minor: a visible new capability.Not in this PR
🤖 Generated with Claude Code