Skip to content

Historical savings: vs flat-rate, computed per day from prices + history #278

Closed

Description

@frahlg

Background

The plan view already shows a "vs flat avg price" badge (web/plan.js:587-589) using mpc.ComputeBaselines for the MPC horizon. That number is forward-looking only — it disappears as soon as time passes.

A field tester ([context: chat]) asked whether we can show the same thing historically — i.e. "how much have I actually saved vs flat-rate this week/month?"

What we have

  • prices table archives every spot slot indefinitely (state/store.go:668, no rolloff).
  • history_hot/warm/cold already integrates per-day energy via state.DailyEnergy (store.go:529).
  • Resolution falls off with age: hot (~5 s, ~24 h retention) → warm (15 min, ~14 d) → cold (daily, forever).

Scope — MVP

GET /api/savings/daily?days=N&zone=SE3

Per-day rows containing:

  • import_wh, export_wh (already in DailyEnergy)
  • actual_cost_ore — slot-weighted: Σ imp_wh × total_ore − Σ exp_wh × export_ore using mpc.SlotExportPriceOre so the export model matches the planner exactly
  • flat_cost_ore — (import_wh − export_wh) × day_avg_import_ore
  • saved_ore — flat_cost − actual_cost (>0 means the system saved money vs flat rate)
  • avg_price_ore, resolution ("slot" when ≥ warm-tier data exists for the day, "daily" fallback for older days)

Plus a totals block for the whole range.

Not in scope (yet)

  • vs no battery historical — feasible later, same SQL pass: import_cost(load_wh − pv_wh, slot). Tracked separately.
  • vs self-consumption historical — would require dispatch replay; not pursuing.

Implementation outline

  1. state.DailyCostBreakdown(sinceMs, untilMs, zone) — SQL that joins history_* to prices by slot window, returns the breakdown. Mirrors DailyEnergy shape.
  2. api_savings.go — handler + per-day immutable cache (mirror handleEnergyDaily pattern at api.go:1394-1411).
  3. Dashboard card later (separate PR).

Files touched

  • go/internal/state/store.go (or new state/cost.go) + test
  • go/internal/api/api_savings.go + test
  • go/internal/api/api.go — one-line route registration

Activity

  1. added
    enhancement
    on May 18, 2026
  2. added a commit that references this issue on May 18, 2026
    7b3e20a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions