Skip to content

Settlement and savings stats: hourly-netted import/export, deposit credit, payback - #55

Merged
piomar123 merged 4 commits into
mainfrom
settlement-stats
Nov 29, 2026
Merged

piomar123 merged 4 commits into
mainfrom
settlement-stats

Conversation

@piomar123

@piomar123 piomar123 commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Problem

We had no long-running record of what the grid operator settles or what PV and the battery save. _calculate_income.py prints one day to the terminal and stores nothing, and the HA recorder keeps only 35 days. Comparing with the bill and tracking the investment return meant redoing the sums by hand.

Change

New settlement.py with a daily_settlement table in data.db. Each row is one local day, computed from hourly_summary per hour:

  • Net import / net export. The meter's import and export are offset within each hour, like the operator's 1 h meter windows.
    • Net import is priced at the import tariff.
    • Net export is the deposit credit at the hourly average of the 15-min RCE. It gets ×1.23 from 2025-02-01, and a negative hour is worth 0.
  • Without PV. The house load is priced at each hour's import tariff, minus PV_EXTRA_LOAD_FACTOR (default 0.10) of the PV used in the house. That part is load PV made cheap, e.g. DHW boost or AC.
  • Savings = without PV − net import cost + deposit credit. Grid energy stored at a cheap hour and used at an expensive one counts as savings.
  • Gross import/export, load, PV, PV used in the house and battery charge/discharge are stored too. A row also records how many hours had data and how many hours were missing an RCE price.

Import prices that changed over time come from a tariff history (TARIFF_HISTORY_CONFIG). Each period is either a flat price or a tariff_engine YAML; see tariff_examples/tariff_history.yaml. If it's not set, TARIFF_IMPORT_CONFIG prices every day.

Load-shift columns (grid_charge_kwh, shifted_kwh, shift_profit_pln). They show grid energy charged into the battery, how much the house used later, and the profit from it.

  • Grid charge in an hour = net battery charge minus the PV surplus over the house load, at most the import.
  • The battery's grid part loses 10 % round trip. The house uses it before stored PV, Predbat's pv_origin rule.
  • The grid part carries over between days (grid_pool_kwh/_pln hold the end-of-day state). It's capped at the energy in the battery: BATTERY_CAPACITY_KWH (default 7.1) × the hour's max SoC.
  • Profit = value at the import price of the hour of use − cost when charged. It's already part of Savings.
  • Last 12 months: 630 kWh moved, 279 zł.

Keeping it current. After each new hourly_summary hour, today's and yesterday's rows are recomputed, plus any older day still missing RCE prices. Errors are only logged and never break the hourly backfill.

Back-fill. _backfill_daily_settlement.py fills past days. It fetches missing RCE days from PSE into rce_prices.db.

Page. /stats is linked as 💰 Savings on the dashboard. It has:

  • summary cards: savings so far, net investment (INVESTMENT_NET_PLN), % paid back, last 12 months, and a payback date projected from that rate;
  • a cumulative savings chart against the investment;
  • a monthly table with year totals;
  • a daily table for the selected month;
  • /stats/daily.csv to download every day.

The data cell turns orange for months with missing hours or days.

Not included. Fixed monthly fees, and tracking the deposit balance or its expiry. The deposit can pay only the sales part of a bill, and the tariff files hold total gross prices.

Tests

  • tests/test_settlement.py covers:
    • RCE hourly averaging, the ×1.23 bonus date, negative hours and DST repeated-hour keys;
    • netting and pricing, and grid arbitrage counted as savings;
    • incomplete hours, hours before the first tariff period, and a failed RCE fetch flagged and retried;
    • tariff history loading and validation;
    • storage and monthly sums, the 25-hour fall-back day, the payback projection and calendar-day coverage.
  • tests/test_main_stats_routes.py covers the /stats page with and without data, the CSV, and checks that the update hook never raises.
  • Full suite: 633 passed.

Back-filled on the real hourly_summary data from 2024-08-12 to 2026-11-25: 817 days, every RCE day found. It's deployed on the Pi.

🤖 Generated with Claude Code

piomar123 and others added 4 commits November 23, 2026 17:42
…edit, savings vs no PV/battery

New daily_settlement table in data.db, recomputed after each new
hourly_summary hour, a back-fill script, and a /stats page with monthly and
yearly totals, a daily table, a CSV download and a payback chart.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… profit

The battery's grid part grows with charge the PV surplus didn't cover (90 %
round trip), is used by the house before stored PV, carries over between
days and is capped at the energy in the battery (hourly max SoC).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… page)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@piomar123
piomar123 merged commit edbd419 into main Nov 29, 2026
1 check passed
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