Repository navigation
feat(pvmodel): anchor near-future predictions to live telemetry - #85
Conversation
The twin's Predict already blends a learned RLS model with a naive physics prior. Both sides of that blend depend on the forecast's cloud_pct — so when met.no says 67% cloud but the sky is actually clear (and PV is producing 8 kW on a 1 kW prediction), the twin has no way to self-correct for ~50 samples. Meanwhile the MPC plans the next slot around the stale prediction and undercommits the battery. Fix: fold in a multiplicative (actual_now / prior_now) correction, linearly decayed over NowAnchorHorizon (2 h). At t=now the correction is applied in full; at t=now+2h it fades to zero so the base model still owns the long-horizon view (where weather fronts in reality won't match "sky is clear right now"). Correction is clamped to [1/5, 5] so a transient inverter restart (actual~0) or sensor spike can't slash or explode predictions. Final output is re-capped at rated. Pure-function helper applyNowAnchor lives in service.go with the branch logic; Service.Predict now wires in live PV via liveActualPV. Falls back to the raw model whenever telemetry is missing, stale, or below a 50 W floor (night) — so the path adds no surprise failure modes. Tests - Seven applyNowAnchor cases: upward/downward clamp at t=0, decay at half-horizon and at horizon, beyond-horizon skip, negative-dt clamp, night/actual-low/prior-low short-circuit, negative-base floor. - Two Service.Predict integration tests: xorath's 8 kW actual vs forecast-80%-cloud (anchored prediction ≈ 3.5 kW instead of 0.7 kW) and the nil-telemetry fallback (raw prior unchanged). No config or main.go change — the anchor is always on. Production behavior when live telemetry is absent or <50 W falls through to the existing model/prior path exactly as today. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 58b99e7eb0
ℹ️ 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".
| for _, r := range s.Tele.ReadingsByType(telemetry.DerPV) { | ||
| if r.SmoothedW < 0 { | ||
| pvW += -r.SmoothedW | ||
| count++ |
There was a problem hiding this comment.
Exclude stale PV readings from live prediction anchor
liveActualPV treats every cached DerPV reading as current and only checks sign/magnitude, but telemetry.Store.ReadingsByType returns last-known values without freshness filtering. If a PV driver stops reporting, its final negative wattage remains in the store and Predict keeps applying the now-anchor against that stale value, which can bias near-term MPC PV forecasts for hours during daylight instead of falling back to the base model. Gate anchor inputs on freshness (UpdatedAt/watchdog timeout or driver online state) before summing.
Useful? React with 👍 / 👎.
* ci: stop new direct reads of cached telemetry outside the telemetry package A reading stays in telemetry.Store after its driver goes stale, offline or faulted. Every direct reader of RawW, SmoothedW, ReadingsByType or ReadingsByDriver must check driver health itself, and review caught the missing check in #36, #85, #310, #647, #664 and #740. The check counts those reads per file outside go/internal/telemetry and fails when a pull request adds one. Existing reads (147 today) may only go down. The error names the health-gated accessors to use instead. On the merged commits of #36, #85, #310 and #647 it fails. * ci: count telemetry reads across platforms and validate exceptions Signed-off-by: Fredrik Ahlgren <[email protected]> --------- Signed-off-by: Fredrik Ahlgren <[email protected]>
Problem
On homelab-rpi today: PV forecast says 67% cloud → twin predicts 1 kW. Reality: sky is clear, PV is producing 8 kW. The twin's RLS would need ~50 samples to re-learn this cloud-vs-reality disconnect; meanwhile the MPC undercommits the battery every slot because it plans around the stale prediction.
Root cause: both halves of the twin's blend (
learned+naive prior) depend on the forecast'scloud_pct. Nothing short-circuits when reality disagrees with the forecast right now.Fix
Fold a multiplicative
actual_now / prior_nowcorrection intoService.Predict, decayed linearly over a 2-hour horizon:t = now→ full correction (xorath's case: 1 kW → ~3.5 kW after 5× clamp)t = now + 1h→ half correction (blend of reality and base)t = now + 2h→ no correction (base model trusted for the long view)Correction clamped to
[1/5, 5]so a transient inverter restart (actual → 0) or sensor spike can't slash or explode predictions.Pure-function helper
applyNowAnchordoes the math (trivially testable);Service.Predictwires in live PV via a new internalliveActualPVmethod. Falls back to the raw model when telemetry is missing or <50 W (night) — no new failure modes.Why this matters now
With energy-allocation dispatch on (
planner.use_energy_dispatch: true, landed in #79), the plan converts Wh per slot → W in real time. If the plan's Wh allocation is still based on a forecast-induced prediction of 1 kW when reality is 8 kW, batteries under-charge. The anchor restores realism to the plan's inputs.Test plan
applyNowAnchorcovering the xorath scenario, downward correction, decay boundaries, past-dt clamp, night / outage short-circuit, and negative-base floor.Service.Predictintegration tests — with and without telemetry.go test ./...green including e2e.pv_w_predictedtrackspv_win near-term slots when the forecast is systematically off.🤖 Generated with Claude Code