Skip to content

fix(savings): compare the bill only where Home use is known - #2144

Merged
frahlg merged 1 commit into
masterfrom
2143-savings-known-home-use
Nov 30, 2026
Merged

frahlg merged 1 commit into
masterfrom
2143-savings-known-home-use

Conversation

@frahlg

@frahlg frahlg commented Nov 30, 2026

Copy link
Copy Markdown
Member

Fixes #2143.

Problem

/api/savings/daily priced the baseline only where Home use was known, but subtracted the whole bill. Time with unknown Home use, such as a charger whose power went unknown, counted as pure cost. A day with 0 % coverage read as minus its whole bill: 29 Nov on the home box said −149.1 kr.

Change

The saving now compares like with like. Each row keeps the bill and adds the part of it that has a baseline:

  • import_cost_ore, export_revenue_ore and actual_cost_ore are the bill, as before: all measured import and export.
  • compared_actual_cost_ore is new: the bill for the intervals that also have a baseline.
  • saved_ore = baseline_cost_ore − compared_actual_cost_ore. self_consumption_saved_ore uses the same compared cost.
  • history_covered_ms and priced_covered_ms count the same intervals, so a day without Home use reads 0 % and partial.

On a fully measured day compared_actual_cost_ore equals actual_cost_ore and nothing else moves. Totals sum the days, and today's forecast builds on the row's saved_ore, so both follow the rule.

Per path:

  • Source intervals (what the home box uses): a price slot adds its bill to the compared cost only when it adds its baseline, that is, when Home use is known for the whole slot.
  • Partial point log, before source intervals: these rows used to drop import with unknown Home use from the bill as well. The bill now keeps it; the saving still leaves it out.
  • Energy ledger (writeLedgerSavings): a bucket without an observed-consumer record had no baseline but kept its import in the compared cost. LedgerFlowBuckets now marks it LoadUnknown; it adds to the bill only, and the blind self-consumption battery holds its charge across it. A known 0 W still leaves a record, so it stays known.
  • Legacy point log: unchanged. Core never stored a partial point there, so the bill and the baseline already covered the same intervals.

The handler comment documents the fields. No registry name changes, and docs/protocol.md does not describe this endpoint.

The two days on the home box

Under the new rule, estimated from the box's own 15-minute energy history and prices. A quarter counts as known when it has a Home use record. This matches the box's baseline to the öre; the compared cost is scaled by the box's bill over the ledger's bill, so treat it as ±2 kr.

Day coverage actual_cost_ore compared_actual_cost_ore baseline_cost_ore saved_ore before saved_ore now
25 Nov 62 % 212.1 kr about 148 kr 143.1 kr −68.9 kr about −5 kr
29 Nov 0 % 149.1 kr 0 0 −149.1 kr 0

The week 24–30 Nov goes from −143.8 kr to about +193 kr. That is below the +270 kr from rebuilding Home use for the whole week: the rule leaves the unknown hours out rather than guess, and 25, 27, 28, 29 and 30 Nov read as partial.

Clients

None computes the saving from actual − baseline. The demo boxes in the web app and both phone apps build their own saved_ore = baseline − actual at full coverage, which stays true for them.

The web app (savings-glance.ts), iPhone (Savings.swift) and Android (Savings.kt) show the box's saved_ore with the line "Cost actual_cost_ore · with no solar or battery baseline_cost_ore", and mark a day partial below 95 % priced_coverage_pct with "The box had prices for N % of the time". On a partial day those two money figures no longer explain the saving, and the note blames prices when Home use was the gap. A follow-up in each client could show compared_actual_cost_ore beside the baseline on a partial day and word the note as measured and priced. The on-box card in web/components/ftw-savings-card.js has the same line and marks a day partial only when it lacks prices; that follow-up belongs in this repo.

Tests

  • internal/state: source intervals with an unknown stretch while import continues, a day with no Home use, and a fully measured day; the partial point log; ledger buckets with known, unknown and known-zero Home use; the legacy path's compared cost equals its bill.
  • internal/savings: the same three cases for the ledger evaluation, including the self-consumption saving and coverage.
  • internal/api: both endpoint paths, with the two problem days and the totals.
go test ./internal/savings/ ./internal/api/ ./internal/state/   # ok
make verify                                                      # ok

Limit

The ledger knows Home use per five-minute bucket. After 30 days it keeps hourly totals, and an hour with any Home use record counts as known. On a box that writes source intervals, only windows that end before they started read the ledger.

🤖 Generated with Claude Code

/api/savings/daily priced the baseline only where Home use was known,
but subtracted the whole bill. Time with unknown Home use, such as a
charger whose power went unknown, counted as pure cost, and a day with
0 % coverage read as minus its whole bill.

The bill (import_cost_ore, export_revenue_ore, actual_cost_ore) still
counts all measured import and export. The new compared_actual_cost_ore
is the bill for the intervals that also have a baseline, and saved_ore
is baseline_cost_ore minus it. Coverage counts the same intervals.

- Source intervals: a price slot adds its bill to the compared cost only
  when it adds its baseline.
- Partial point log: the bill now keeps import while Home use was
  unknown; the saving still leaves it out.
- Energy ledger: a bucket without an observed-consumer record is
  LoadUnknown and adds to the bill only. The blind self-consumption
  battery holds its charge across it.
- Legacy point log: unchanged; its rows always carry Home use.

Totals sum the days and today's forecast builds on the row's saved_ore,
so both follow the rule.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

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

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-30T12:12:06.719267Z 5c63d28 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 merged commit 7336e8c into master Nov 30, 2026
20 checks passed

@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: 5c63d280c6

ℹ️ 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 on lines 328 to +330
"actual_cost_ore": ds.ActualCostOre,
"baseline_house_ore": ds.BaselineHouseOre,
"baseline_ev_ore": ds.BaselineEvOre,
"baseline_cost_ore": ds.BaselineCostOre,
// The bill over the intervals baseline_cost_ore prices; see above.
"compared_actual_cost_ore": ds.ComparedCostOre,

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 Update the on-box card to use the compared cost

When Home use is missing for part of a day, saved_ore now compares the baseline with compared_actual_cost_ore, but the existing on-box consumer still presents the whole bill as the comparison cost. In web/components/ftw-savings-card.js, _paint() and _showSparkTip() read actual_cost_ore beside the baseline, so the displayed figures no longer explain the displayed saving. Moreover, web/components/savings-periods.js determines completeness from price availability, so a day with prices but no known Home use can now appear as a complete zero-saving result. Update these consumers alongside the API change to distinguish the bill from the compared cost and mark missing measurement coverage.

Useful? React with 👍 / 👎.

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.

Savings charge the whole bill against a partial baseline when Home use is unknown

1 participant