Repository navigation
fix(history): keep the chart running when optional PV never reports #1480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "ftw": patch | ||
| --- | ||
|
|
||
| Keep the history chart and site energy running when a device can report PV but does not, such as a Zap P1 meter with PV reading off, or while a device identity is still unconfirmed. Before, adding such a device stopped history until it was removed, even across restarts. Forecast learning keeps its stricter checks. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "path/filepath" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/srcfl/ftw/go/internal/config" | ||
| "github.com/srcfl/ftw/go/internal/drivers" | ||
| "github.com/srcfl/ftw/go/internal/state" | ||
| "github.com/srcfl/ftw/go/internal/telemetry" | ||
| ) | ||
|
|
||
| // Issue #1441: adding a Zap P1 meter (catalog meter+pv+battery, PV reading | ||
| // off by default) froze the chart for good. The Zap never reports PV, so the | ||
| // forecast topology kept the household balance invalid on every tick and | ||
| // after every restart. The chart needs a measured balance, not proof that an | ||
| // optional source is absent or that device identities are confirmed. | ||
| func TestHistoryKeepsRunningWithOptionalPVAndUnconfirmedIdentity(t *testing.T) { | ||
| st, err := state.Open(filepath.Join(t.TempDir(), "state.db")) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer st.Close() | ||
| catalog := []drivers.CatalogEntry{ | ||
| {Path: "drivers/zap.lua", Filename: "zap.lua", Capabilities: []string{"meter", "pv", "battery"}}, | ||
| {Path: "drivers/sma.lua", Filename: "sma.lua", Capabilities: []string{"pv"}}, | ||
| {Path: "drivers/pixii.lua", Filename: "pixii.lua", Capabilities: []string{"battery"}}, | ||
| } | ||
| cfg := &config.Config{Drivers: []config.Driver{ | ||
| {Name: "sourceful-zap", Lua: "drivers/zap.lua", IsSiteMeter: true, Config: map[string]any{"host": "zap.local"}}, | ||
| {Name: "sma", Lua: "drivers/sma.lua"}, | ||
| {Name: "pixii", Lua: "drivers/pixii.lua", BatteryCapacityWh: 10000}, | ||
| }} | ||
| ids := map[string]string{"sourceful-zap": "zap:sn", "sma": "sma:sn"} // pixii identity not yet known | ||
| site := newForecastSiteConfig(st) | ||
| site.identity = func(name string) (string, bool) { id := ids[name]; return id, id != "" } | ||
| site.Configure(cfg, catalog) | ||
|
|
||
| tel := telemetry.NewStore() | ||
| for name, kind := range map[string]telemetry.DerType{"sourceful-zap": telemetry.DerMeter, "sma": telemetry.DerPV, "pixii": telemetry.DerBattery} { | ||
| tel.EnsureDriverHealth(name) | ||
| w := map[telemetry.DerType]float64{telemetry.DerMeter: 1500, telemetry.DerPV: -2000, telemetry.DerBattery: 500}[kind] | ||
| tel.Update(name, kind, w, nil, nil) | ||
| tel.RecordDriverSuccess(name) | ||
| } | ||
| snap := site.Snapshot() | ||
| if r := tel.ForecastMeasurementNow("sourceful-zap", snap.Options); r.Valid { | ||
| t.Fatalf("forecast learning must stay strict: %+v", r) | ||
| } | ||
| point, ok := buildHistoryPoint(tel, tickPersistControl{SiteMeterDriver: "sourceful-zap"}, | ||
| time.Now().UnixMilli(), time.Minute, snap.HistoryOptions) | ||
| if !ok || point.LoadW != 3000 || point.PVW != -2000 || point.BatW != 500 { | ||
| t.Fatalf("history stopped: point=%+v available=%v", point, ok) | ||
| } | ||
|
|
||
| // A known source that goes silent still stops history; only optional, | ||
| // never-reported PV is treated as absent. | ||
| tel.DriverHealthMut("pixii").SetOffline() | ||
| if _, ok := buildHistoryPoint(tel, tickPersistControl{SiteMeterDriver: "sourceful-zap"}, | ||
| time.Now().UnixMilli(), time.Minute, snap.HistoryOptions); ok { | ||
| t.Fatal("offline battery treated as zero in history") | ||
| } | ||
| tel.Update("pixii", telemetry.DerBattery, 500, nil, nil) | ||
| tel.RecordDriverSuccess("pixii") | ||
| // Once the Zap does report PV, a stale PV reading stops history again. | ||
| tel.Update("sourceful-zap", telemetry.DerPV, -100, nil, nil) | ||
| tel.Get("sourceful-zap", telemetry.DerPV).UpdatedAt = time.Now().Add(-2 * time.Minute) | ||
| if _, ok := buildHistoryPoint(tel, tickPersistControl{SiteMeterDriver: "sourceful-zap"}, | ||
| time.Now().UnixMilli(), time.Minute, snap.HistoryOptions); ok { | ||
| t.Fatal("stale reported PV treated as zero in history") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a catalog entry combines PV with another capability and the configuration does not explicitly declare PV, this removes the PV flow even if the driver is offline or has not completed its first poll. If that device really supplies PV, the telemetry scan has no reading with which to add it back, so the site-meter-only balance is accepted and history permanently records PV as zero and grid power as household load until the first PV sample arrives—potentially throughout an outage. Treat the missing optional flow as absent only after the live driver has established that it will not publish PV, so freshness failures do not become measured energy.
AGENTS.md reference: AGENTS.md:L22-L23
Useful? React with 👍 / 👎.