Repository navigation
fix(history): count unknown PV as 0 W after sunset #1755
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
+316
−14
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7d2c4ae
fix(history): count unknown PV as 0 W after sunset
frahlg 741a6c9
fix(history): count PV past civil dusk as measured 0 W
frahlg bbf0270
test(history): a full night with a sleeping inverter stays measured
frahlg cc75e99
refactor(history): let the dark rule touch only PV
frahlg 37d52b5
fix(telemetry): name offline drivers through a health-gated accessor
frahlg 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 day's Home use when a PV inverter goes silent at night. While the sun is below the horizon at the site's map pin, an unknown PV source counts as 0 W: past civil dusk as a measurement, so the night also trains the load models, and in twilight as a marked estimate. Without a pin, or by day, the gap stays unknown as before. |
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,83 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "sort" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/srcfl/ftw/go/internal/state" | ||
| "github.com/srcfl/ftw/go/internal/sunpos" | ||
| "github.com/srcfl/ftw/go/internal/telemetry" | ||
| ) | ||
|
|
||
| // Sun positions as zenith angles. After sunset (upper limb below the horizon, | ||
| // refraction included) no panel gets direct sunlight, but diffuse light | ||
| // remains until civil dusk, when the sun is 6° down. | ||
| const ( | ||
| sunsetZenithDeg = 90.833 | ||
| civilDuskZenithDeg = 96 | ||
| ) | ||
|
|
||
| // historySun places the site for history. The zero value has no position, | ||
| // and then nothing is inferred. | ||
| type historySun struct { | ||
| Lat, Lon float64 | ||
| Placed bool | ||
| } | ||
|
|
||
| func historySunOf(lat, lon float64, ok bool) historySun { | ||
| return historySun{Lat: lat, Lon: lon, Placed: ok} | ||
| } | ||
|
|
||
| func (s historySun) zenith(atMS int64) (float64, bool) { | ||
| if !s.Placed { | ||
| return 0, false | ||
| } | ||
| return sunpos.At(time.UnixMilli(atMS), s.Lat, s.Lon).ZenithDeg, true | ||
| } | ||
|
|
||
| // darkPV records an unknown PV source as 0 W while the sun is down at the | ||
| // site: physics, not interpolation. Past civil dusk the 0 W is as good as a | ||
| // reading and carries no marker, so a night with a sleeping inverter also | ||
| // trains the load models; no separate bit records it. In twilight the | ||
| // snapshot carries EstimatedLoad: Home use counts it, training does not. | ||
| // Every other flow, and Load, keeps its own evidence. | ||
| func darkPV(p *state.HistorySources, sun historySun) bool { | ||
| if p == nil || p.Missing&state.MissingPV == 0 { | ||
| return false | ||
| } | ||
| z, placed := sun.zenith(p.AtMS) | ||
| if !placed || z <= sunsetZenithDeg { | ||
| return false | ||
| } | ||
| p.PVW = 0 | ||
| p.Missing &^= state.MissingPV | ||
| if z <= civilDuskZenithDeg { | ||
| p.Missing |= state.EstimatedLoad | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| // historyDevices names the hardware behind a history snapshot, as the | ||
| // topology's devices line. The watchdog drops an offline driver from the | ||
| // energy record. After sunset a silent inverter is asleep, not replaced, so it | ||
| // keeps its place: the night stays one topology, and its minutes stay | ||
| // measured. | ||
| func historyDevices(st *state.Store, tel *telemetry.Store, identity energyIdentityLookup, observations []state.EnergyObservation, dark bool) string { | ||
| var devices []string | ||
| for _, o := range observations { | ||
| if o.DeviceID != "" { | ||
| devices = append(devices, string(o.AssetKind)+":"+o.DeviceID) | ||
| } | ||
| } | ||
| if dark && st != nil && tel != nil && identity != nil { | ||
| known := st.CachedDevices() | ||
| for _, driver := range tel.OfflineDrivers(telemetry.DerPV) { | ||
| if id := confirmedEnergyDeviceID(identity(driver), known); id != "" { | ||
| devices = append(devices, string(state.AssetPV)+":"+id) | ||
| } | ||
| } | ||
| } | ||
| sort.Strings(devices) | ||
| return "\ndevices:" + strings.Join(devices, "\n") | ||
| } | ||
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,137 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "math" | ||
| "path/filepath" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/srcfl/ftw/go/internal/config" | ||
| "github.com/srcfl/ftw/go/internal/state" | ||
| "github.com/srcfl/ftw/go/internal/telemetry" | ||
| ) | ||
|
|
||
| // Göran's box, 19 October 2026: SMA was silent for 145 s at 02:22 local time, | ||
| // 0 W on both sides, and the day's Home use became unknown. In Västerås that | ||
| // day the sun sets at 17:38 and civil dusk is at about 18:20. | ||
| func TestDarkPVByTimeOfDay(t *testing.T) { | ||
| stockholm := time.FixedZone("CEST", 2*3600) | ||
| sun := historySun{Lat: 59.6, Lon: 16.5, Placed: true} | ||
| night := time.Date(2026, 10, 19, 2, 22, 0, 0, stockholm) | ||
| for _, c := range []struct { | ||
| name string | ||
| at time.Time | ||
| sun historySun | ||
| missing state.HistoryMissing | ||
| applied bool | ||
| want state.HistoryMissing | ||
| }{ | ||
| {name: "night", at: night, sun: sun, missing: state.MissingPV, applied: true}, | ||
| {name: "after dusk", at: time.Date(2026, 10, 19, 18, 45, 0, 0, stockholm), sun: sun, missing: state.MissingPV, applied: true}, | ||
| {name: "twilight", at: time.Date(2026, 10, 19, 17, 45, 0, 0, stockholm), sun: sun, missing: state.MissingPV, applied: true, want: state.EstimatedLoad}, | ||
| {name: "noon", at: time.Date(2026, 10, 19, 12, 0, 0, 0, stockholm), sun: sun, missing: state.MissingPV, want: state.MissingPV}, | ||
| {name: "before sunset", at: time.Date(2026, 10, 19, 17, 30, 0, 0, stockholm), sun: sun, missing: state.MissingPV, want: state.MissingPV}, | ||
| {name: "no pin", at: night, missing: state.MissingPV, want: state.MissingPV}, | ||
| // Other flows, and a configuration fault, keep their own evidence. | ||
| {name: "battery also unknown", at: night, sun: sun, missing: state.MissingPV | state.MissingBattery, applied: true, want: state.MissingBattery}, | ||
| {name: "PV array without a measurement source", at: night, sun: sun, missing: state.MissingPV | state.MissingLoad, applied: true, want: state.MissingLoad}, | ||
| } { | ||
| p := &state.HistorySources{AtMS: c.at.UnixMilli(), PVW: -300, Missing: c.missing} | ||
| applied := darkPV(p, c.sun) | ||
| if applied != c.applied || p.Missing != c.want || applied && p.PVW != 0 { | ||
| t.Errorf("%s: applied=%v missing=%b PV=%v W, want applied=%v missing=%b", c.name, applied, p.Missing, p.PVW, c.applied, c.want) | ||
| } | ||
| } | ||
| for _, c := range []struct { | ||
| lat, lon float64 | ||
| ok bool | ||
| }{{0, 0, false}, {unsetPinLat, unsetPinLon, false}, {59.6, 16.5, true}} { | ||
| if got := historySunOf(siteLocation(&config.Config{Weather: &config.Weather{Latitude: c.lat, Longitude: c.lon}})).Placed; got != c.ok { | ||
| t.Errorf("pin %v,%v placed=%v, want %v", c.lat, c.lon, got, c.ok) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // A full night on the equator: 20:00–04:00 UTC at 15° W is past civil dusk in | ||
| // every season. The inverter reports 0 W for ten minutes, then sleeps, and a | ||
| // minute later the watchdog takes it offline. With a pin every snapshot is | ||
| // fully known and of one topology, so the writer records measured minutes that | ||
| // train the load models. Without one, the writer holds PV's last value as an | ||
| // estimate, and the topology changes when the device drops out. Home use is | ||
| // known in both. | ||
| func TestSleepingInverterNightIsMeasured(t *testing.T) { | ||
| const tick, hour = int64(5000), int64(3600000) | ||
| for _, c := range []struct { | ||
| name string | ||
| sun historySun | ||
| measured bool | ||
| }{ | ||
| {"with a pin", historySun{Lat: 1, Lon: -15, Placed: true}, true}, | ||
| {"without a pin", historySun{}, false}, | ||
| } { | ||
| t.Run(c.name, func(t *testing.T) { | ||
| st, err := state.Open(filepath.Join(t.TempDir(), "state.db")) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| t.Cleanup(func() { _ = st.Close() }) | ||
| if err := st.EnableHistoryAggregation(); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if _, err := st.RegisterDevice(state.Device{DriverName: "sma", Make: "SMA", Serial: "PV-7"}); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| identity := testEnergyIdentity(st) | ||
| ctrl := tickPersistControl{SiteMeterDriver: "grid"} | ||
| tel := telemetry.NewStore() | ||
| tel.Update("grid", telemetry.DerMeter, 1000, nil, nil) | ||
| tel.RecordDriverSuccess("grid") | ||
| tel.Update("sma", telemetry.DerPV, 0, nil, nil) | ||
| tel.RecordDriverSuccess("sma") | ||
| base := time.Now().UTC().Truncate(24*time.Hour).UnixMilli() - 28*hour | ||
| topologies := map[string]bool{} | ||
| for at := base; at <= base+8*hour; at += tick { | ||
| off := at - base | ||
| if off == 11*60000 { | ||
| tel.DriverHealthMut("sma").SetOffline() | ||
| } | ||
| r := telemetry.ForecastReading{ | ||
| At: time.UnixMilli(at), | ||
| Known: map[telemetry.DerType]bool{telemetry.DerMeter: true, telemetry.DerPV: true, telemetry.DerBattery: true, telemetry.DerEV: true, telemetry.DerV2X: true}, | ||
| Sources: map[string]telemetry.ForecastSource{ | ||
| "grid:meter": {Flow: telemetry.ForecastFlow{Driver: "grid", DerType: telemetry.DerMeter}}, | ||
| "sma:pv": {Flow: telemetry.ForecastFlow{Driver: "sma", DerType: telemetry.DerPV}}, | ||
| }, | ||
| GridW: 1000, | ||
| } | ||
| if off > 10*60000 { | ||
| r.Known[telemetry.DerPV], r.Reason = false, "offline:sma:pv" | ||
| } | ||
| v := historySources(r) | ||
| dark := darkPV(v, c.sun) | ||
| v.Topology += historyDevices(st, tel, identity, buildEnergyObservations(st, tel, ctrl, state.HistoryPoint{}, identity), dark) | ||
| topologies[v.Topology] = true | ||
| if known := v.Missing == 0; known != (c.measured || off <= 10*60000) { | ||
| t.Fatalf("%d min: snapshot missing %b", off/60000, v.Missing) | ||
| } | ||
| if err := st.RecordTickWithEnergy(state.HistoryPoint{TsMs: at, Sources: v}, nil, nil); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
| if err := st.FlushHistory(context.Background()); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if one := len(topologies) == 1; one != c.measured { | ||
| t.Fatalf("%d topologies over the night", len(topologies)) | ||
| } | ||
| d, err := st.DailyEnergy(base, base+8*hour) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if d.Missing&state.MissingLoad != 0 || math.Abs(d.LoadWh-8000) > 1e-6 { | ||
| t.Fatalf("Home use %v Wh, missing %b; want 8000 Wh", d.LoadWh, d.Missing) | ||
| } | ||
| }) | ||
| } | ||
| } |
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,23 @@ | ||
| package telemetry | ||
|
|
||
| import "sort" | ||
|
|
||
| // OfflineDrivers names the drivers with a reading of kind whose telemetry is | ||
| // offline or has no health yet. Only names leave the store, never a cached | ||
| // value, so a stale reading cannot be mistaken for a live one. | ||
| func (s *Store) OfflineDrivers(kind DerType) []string { | ||
| s.mu.RLock() | ||
| defer s.mu.RUnlock() | ||
| var out []string | ||
| for _, r := range s.readings { | ||
| if r.DerType != kind { | ||
| continue | ||
| } | ||
| if h := s.health[r.Driver]; h != nil && h.Status != StatusOffline { | ||
| continue | ||
| } | ||
| out = append(out, r.Driver) | ||
| } | ||
| sort.Strings(out) | ||
| return out | ||
| } |
Oops, something went wrong.
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 site has multiple PV flows and only one becomes unavailable during twilight,
Known[DerPV]is false for the aggregate even thoughForecastReading.PVWstill contains output from the remaining fresh sources.historySourcesconsequently zeroes the aggregate, and clearingMissingPVhere records every inverter as 0 W; for example, one sleeping inverter plus another producing diffuse-light power makes Home use too low for the affected intervals. Apply the zero assumption only to the unavailable PV flows, while retaining the contributions from sources that are still reporting.Useful? React with 👍 / 👎.