diff --git a/.changeset/easee-load-balancer-limit.md b/.changeset/easee-load-balancer-limit.md new file mode 100644 index 000000000..7f48993db --- /dev/null +++ b/.changeset/easee-load-balancer-limit.md @@ -0,0 +1,5 @@ +--- +"ftw": patch +--- + +When an Easee charger's own load balancing holds the car below FTW's order, the EV sheet now says "Easee limits the charger to 12 A (its load balancing). FTW asked for 15 A; the car takes 8.3 kW." instead of "Not following". It needs easee_cloud 1.4.1, which reports the current Easee lets the car take while it is below the order, and names its load balancing when Easee's allocation shows it. Without a named cause the sheet reads "The charger lets the car take 12 A, below the 15 A FTW asked for." A car taking far less or more than the limit allows is still judged as before, and an old charger reason is never shown as the cause. diff --git a/.changeset/pilot-evidence.md b/.changeset/pilot-evidence.md new file mode 100644 index 000000000..124de0a9f --- /dev/null +++ b/.changeset/pilot-evidence.md @@ -0,0 +1,5 @@ +--- +"ftw": patch +--- + +Expose fresh effective EV pilot current separately from the dynamic current setting. A confirmed lower pilot that explains measured delivery is an informational charger limitation even when its internal cause is unknown. Stale or future pilot evidence cannot explain an EV shortfall. A pilot that is only past its 90 s window reads as unknown, and the driver's reported limit, such as Easee's load balancing, still stands. diff --git a/go/internal/api/api_control_feedback.go b/go/internal/api/api_control_feedback.go index f2cc18fdb..bd15a1553 100644 --- a/go/internal/api/api_control_feedback.go +++ b/go/internal/api/api_control_feedback.go @@ -35,28 +35,38 @@ type ControlFeedback struct { SiteEvidence *ControlComparison `json:"site_evidence,omitempty"` ToleranceW *float64 `json:"tolerance_w"` // Internal steps of the verdict; the API exposes their result above. - State string `json:"-"` - VerificationTier *int `json:"-"` - VerificationLost bool `json:"-"` - Escalated bool `json:"-"` // not following long enough to be an alarm - Response string `json:"-"` - SiteBeforeW *float64 `json:"-"` - SiteAfterW *float64 `json:"-"` - SiteBeforeAtMs int64 `json:"-"` - SiteAfterAtMs int64 `json:"-"` - SiteDeltaW *float64 `json:"-"` - DeviceDeltaW *float64 `json:"-"` - VerifiedAtMs int64 `json:"-"` - RequestedW *float64 `json:"requested_w"` - SentW *float64 `json:"sent_w"` - ReadbackW *float64 `json:"readback_w"` - ActualW *float64 `json:"actual_w"` - BatterySoC *float64 `json:"battery_soc,omitempty"` - ChargeResumeSoC *float64 `json:"charge_resume_soc,omitempty"` - RequestedA *float64 `json:"requested_a"` - OfferedA *float64 `json:"offered_a"` - DeviceLimitA *float64 `json:"device_limit_a"` - DeviceReason string `json:"device_reason,omitempty"` + State string `json:"-"` + VerificationTier *int `json:"-"` + VerificationLost bool `json:"-"` + Escalated bool `json:"-"` // not following long enough to be an alarm + Response string `json:"-"` + SiteBeforeW *float64 `json:"-"` + SiteAfterW *float64 `json:"-"` + SiteBeforeAtMs int64 `json:"-"` + SiteAfterAtMs int64 `json:"-"` + SiteDeltaW *float64 `json:"-"` + DeviceDeltaW *float64 `json:"-"` + VerifiedAtMs int64 `json:"-"` + RequestedW *float64 `json:"requested_w"` + SentW *float64 `json:"sent_w"` + ReadbackW *float64 `json:"readback_w"` + ActualW *float64 `json:"actual_w"` + BatterySoC *float64 `json:"battery_soc,omitempty"` + ChargeResumeSoC *float64 `json:"charge_resume_soc,omitempty"` + RequestedA *float64 `json:"requested_a"` + OfferedA *float64 `json:"offered_a"` + DeviceLimitA *float64 `json:"device_limit_a"` + PilotA *float64 `json:"pilot_a"` + PilotObservedAt string `json:"pilot_observed_at,omitempty"` + PilotConfirmedAtMs int64 `json:"pilot_confirmed_at_ms,omitempty"` + DynamicLimitA *float64 `json:"dynamic_limit_a"` + // LimitA is the current a charger lets the car take now, when its driver + // reports it below the order; LimitedBy names the cause when the driver + // shows it, and Make the charger for the sentence. + LimitA *float64 `json:"limit_a,omitempty"` + LimitedBy string `json:"limited_by,omitempty"` + Make string `json:"make,omitempty"` + DeviceReason string `json:"device_reason,omitempty"` // ChargingPhase names the one phase (L1-L3) a one-phase car charges on // under a three-phase order, and ChargingPhaseMaxW the most it takes. ChargingPhase string `json:"charging_phase,omitempty"` @@ -67,13 +77,19 @@ type ControlFeedback struct { } type feedbackReading struct { - SetpointW *float64 `json:"setpoint_w"` - MaxA *float64 `json:"max_a"` - DeviceLimitA *float64 `json:"device_limit_a"` - DeviceLimitAgeS *float64 `json:"device_limit_age_s"` - Reason string `json:"reason_no_current_label"` - Connected *bool `json:"connected"` - Online *bool `json:"is_online"` + SetpointW *float64 `json:"setpoint_w"` + MaxA *float64 `json:"max_a"` + PilotA *float64 `json:"pilot_a"` + PilotObservedAt string `json:"pilot_observed_at"` + PilotConfirmedAtMs *int64 `json:"pilot_confirmed_at_ms"` + DynamicLimitA *float64 `json:"dynamic_limit_a"` + DeviceLimitA *float64 `json:"device_limit_a"` + DeviceLimitAgeS *float64 `json:"device_limit_age_s"` + Reason string `json:"reason_no_current_label"` + LimitA *float64 `json:"current_limit_a"` + LimitedBy string `json:"current_limited_by"` + Connected *bool `json:"connected"` + Online *bool `json:"is_online"` } func watts(v float64) *float64 { @@ -150,6 +166,30 @@ func (s *Server) controlFeedback(now time.Time) []ControlFeedback { var d feedbackReading _ = json.Unmarshal(rd.Data, &d) f.ReadbackW, f.OfferedA, f.DeviceReason = d.SetpointW, d.MaxA, d.Reason + f.DynamicLimitA = d.DynamicLimitA + pilotAge, pilotValid := pilotReadingAge(d, now) + pilotFresh := pilotValid && pilotAge <= pilotFreshFor + if pilotFresh { + f.PilotA, f.PilotObservedAt, f.PilotConfirmedAtMs = d.PilotA, d.PilotObservedAt, *d.PilotConfirmedAtMs + } + // New pilot-aware drivers must provide usable evidence. Older + // drivers keep their existing normalized-limit contract, and so + // does a valid pilot that is only too old: easee_cloud re-reads it + // once a minute, a late read can pass 90 s, and Core keeps the + // last reading for a minute after the driver stops sending. The + // driver checked its limit against the pilot when it was fresh, + // so only the pilot becomes unknown. + if d.PilotConfirmedAtMs != nil && (!pilotValid || (pilotFresh && *d.PilotA == 0)) { + d.LimitA, d.LimitedBy = nil, "" + } + if d.LimitA != nil { + f.LimitA, f.LimitedBy = d.LimitA, d.LimitedBy + if s.deps.Registry != nil { + if env := s.deps.Registry.Env(rd.Driver); env != nil { + f.Make, _ = env.Identity() + } + } + } if d.DeviceLimitAgeS != nil && *d.DeviceLimitAgeS >= 0 && *d.DeviceLimitAgeS <= 120 { f.DeviceLimitA = d.DeviceLimitA } @@ -359,7 +399,7 @@ func setControlStatus(f *ControlFeedback) { "vehicle_complete", "vehicle_limit_completion", "vehicle_not_requesting": status = "following" case "battery_full", "battery_nearly_full", "battery_nearly_empty", "core_limit", - "fuse_limit", "fuse_cooldown", "charger_limit", "one_phase": + "fuse_limit", "fuse_cooldown", "charger_limit", "load_balancer_limit", "charger_pilot_limit", "one_phase": status = "limited" case "import_limit": status = "limited" @@ -427,7 +467,9 @@ const ( func classifyControlFeedback(f *ControlFeedback, cmd telemetry.CommandEvidence, commanded, fresh bool, coreReason string, connected *bool, now time.Time) { set := func(state, reason, severity string) { f.State, f.Reason, f.Severity = state, reason, severity } if !fresh { - f.ActualW, f.ReadbackW, f.OfferedA, f.DeviceLimitA = nil, nil, nil, nil + f.ActualW, f.ReadbackW, f.OfferedA, f.DeviceLimitA, f.LimitA, f.LimitedBy, f.Make = nil, nil, nil, nil, nil, "", "" + f.PilotA, f.DynamicLimitA = nil, nil + f.PilotObservedAt, f.PilotConfirmedAtMs = "", 0 set("unknown", "telemetry_stale", "warning") return } @@ -462,7 +504,10 @@ func classifyControlFeedback(f *ControlFeedback, cmd telemetry.CommandEvidence, set("warning", "no_power_response", "warning") return } - if f.RequestedA != nil && f.DeviceLimitA != nil && *f.RequestedA > *f.DeviceLimitA+0.5 { + // With a fresh pilot and the current the charger lets the car take, the + // draw is judged against them below, so the charger's own limit cannot + // hide a car taking less. + if !(f.PilotA != nil && f.LimitA != nil) && f.RequestedA != nil && f.DeviceLimitA != nil && *f.RequestedA > *f.DeviceLimitA+0.5 { set("limited", "device_limit", "warning") return } @@ -493,6 +538,34 @@ func classifyControlFeedback(f *ControlFeedback, cmd telemetry.CommandEvidence, set("limited", "offered_current_lower", "warning") return } + // The charger holds the order but its driver reports that it lets the car + // take less, and names its load balancing when the charger's allocation + // shows it (easee_cloud 1.4.1). That explains a car taking about that + // much, never more: within 10 %, or as the car follows a step, within + // 2 A but at most 15 % of the limit. A car taking far less, nothing, or + // more keeps its verdict. A fresh pilot names the charger as the cause + // only when it is below the order and is that current. A lower current + // whose cause the driver does not show still asks for a look. + if f.Kind == "ev" && commanded && cmd.Result == "accepted" && f.SentW != nil && f.RequestedW != nil && f.RequestedA != nil && f.LimitA != nil && f.ActualW != nil && + *f.SentW > 100 && *f.RequestedA > 0 && *f.LimitA > 0 && *f.LimitA+0.5 < *f.RequestedA && settled { + perA := *f.RequestedW / *f.RequestedA + limitW := *f.LimitA * perA + tolerance := telemetry.ControlToleranceW(limitW) + if *f.ActualW >= limitW-math.Max(tolerance, math.Min(2*perA, 0.15*limitW)) && *f.ActualW <= limitW+tolerance && *f.ActualW < *f.SentW { + if f.LimitedBy == "load_balancer" { + set("limited", "load_balancer_limit", "info") + } else if f.PilotA != nil && *f.PilotA+0.5 < *f.RequestedA && math.Abs(*f.PilotA-*f.LimitA) <= 0.5 { + if f.DeviceLimitA != nil && math.Abs(*f.LimitA-*f.DeviceLimitA) <= 0.5 { + set("limited", "device_limit", "info") + } else { + set("limited", "charger_pilot_limit", "info") + } + } else { + set("limited", "offered_current_lower", "warning") + } + return + } + } // This is a known Core stop, not a BMS diagnosis or new measurement proof. // Continued power, changed setpoints, stale readings and faults keep their // own verdicts. A full battery is still allowed to discharge. @@ -626,3 +699,22 @@ func (s *Server) loadpointsWithFeedback(states []loadpoint.State) []loadpointFee } return out } + +// pilotFreshFor is how long a pilot's confirmation can explain a draw; it +// matches the 90 s for which easee_cloud vouches for a cloud read. +const pilotFreshFor = 90 * time.Second + +// pilotReadingAge is how long ago a pilot-aware driver confirmed its pilot. +// ok is false when the pilot is not a current, or its confirmation is +// missing or lies after now, or its source time after the confirmation. +func pilotReadingAge(d feedbackReading, now time.Time) (age time.Duration, ok bool) { + if d.PilotA == nil || d.PilotConfirmedAtMs == nil || *d.PilotConfirmedAtMs <= 0 || math.IsNaN(*d.PilotA) || math.IsInf(*d.PilotA, 0) || *d.PilotA < 0 { + return 0, false + } + confirmed := time.UnixMilli(*d.PilotConfirmedAtMs) + source, err := time.Parse(time.RFC3339Nano, d.PilotObservedAt) + if err != nil || confirmed.After(now) || source.After(confirmed) { + return 0, false + } + return now.Sub(confirmed), true +} diff --git a/go/internal/api/api_control_scenarios_test.go b/go/internal/api/api_control_scenarios_test.go index 365957861..4470550bf 100644 --- a/go/internal/api/api_control_scenarios_test.go +++ b/go/internal/api/api_control_scenarios_test.go @@ -272,6 +272,7 @@ func TestControlStatusAnswersEveryReason(t *testing.T) { "battery_full": {"limited", "info"}, "battery_nearly_full": {"limited", "info"}, "fuse_limit": {"limited", "info"}, + "load_balancer_limit": {"limited", "info"}, "device_limit": {"limited", "warning"}, "power_below_target": {"not_following", "warning"}, "setpoint_changed": {"not_following", "warning"}, diff --git a/go/internal/api/api_ev_load_balancer_test.go b/go/internal/api/api_ev_load_balancer_test.go new file mode 100644 index 000000000..fd8901b97 --- /dev/null +++ b/go/internal/api/api_ev_load_balancer_test.go @@ -0,0 +1,150 @@ +package api + +import ( + "context" + "math" + "os" + "path/filepath" + "testing" + "testing/synctest" + "time" + + "github.com/srcfl/ftw/go/internal/config" + "github.com/srcfl/ftw/go/internal/drivers" + "github.com/srcfl/ftw/go/internal/telemetry" +) + +// Field trace, 23 Oct 2026 (srcfl/ftw#1763), Easee Cloud: FTW ordered 15 A +// (10.35 kW) throughout and the charger read back 15 A, while Easee's own +// load balancing signalled 13 A to the car, which took 8.85 kW. FTW said +// "Not following". The driver now reports the limit and, when its +// allocation shows it, names the load balancing. +func TestEaseeLoadBalancingReadsAsLimited(t *testing.T) { + // The driver always reads back the order it holds (max_a, observation 48). + const held = `,"max_a":15` + const named = held + `,"current_limit_a":13,"current_limited_by":"load_balancer"` + for _, tc := range []struct { + name, extra string + drawW float64 + status, reason, severity string + }{ + {"named", named, 8850, "limited", "load_balancer_limit", "info"}, + {"cause not shown", held + `,"current_limit_a":13`, 8850, "limited", "offered_current_lower", "warning"}, + {"no limit reported", held, 8850, "not_following", "power_below_target", "warning"}, + {"a 13 A limit does not explain zero", named, 0, "not_following", "no_power_response", "warning"}, + {"nor a car taking far less", named, 4000, "not_following", "power_below_target", "warning"}, + {"nor a car 2 A under it", named, 7500, "not_following", "power_below_target", "warning"}, + {"nor a car 1.5 A under a 7 A limit", held + `,"current_limit_a":7,"current_limited_by":"load_balancer"`, 3800, "not_following", "power_below_target", "warning"}, + {"nor a car at the order", named, 10300, "following", "power_observed", "info"}, + {"limit at the order", held + `,"current_limit_a":15,"current_limited_by":"load_balancer"`, 8850, "not_following", "power_below_target", "warning"}, + {"a zero limit", held + `,"current_limit_a":0,"current_limited_by":"load_balancer"`, 0, "not_following", "no_power_response", "warning"}, + {"a zero limit, no readback", `,"current_limit_a":0,"current_limited_by":"load_balancer"`, 0, "not_following", "no_power_response", "warning"}, + // A charger that holds a lower order than FTW sent says so first. + {"charger holds 12 A", `,"max_a":12,"current_limit_a":10,"current_limited_by":"load_balancer"`, 6900, "limited", "offered_current_lower", "warning"}, + } { + t.Run(tc.name, func(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + e := newEVTrace(10370) + e.extra = held + for range 5 * 60 / 5 { + e.tick(10350, "plan") + } + if f := e.verdict(t); f.Status != "following" { + t.Fatalf("before the cut: %s/%s", f.Status, f.Reason) + } + e.drawW, e.changedAt, e.extra = tc.drawW, time.Now(), tc.extra + cut := time.Now() + var f ControlFeedback + for range 6 * 60 / 5 { + e.tick(10350, "plan") + f = e.verdict(t) + if tc.status == "limited" && f.Status == "not_following" { + t.Fatalf("at %v: not following (%s)", time.Since(cut), f.Reason) + } + } + if f.Status != tc.status || f.Reason != tc.reason || f.Severity != tc.severity { + t.Fatalf("got %s/%s/%s, want %s/%s/%s", f.Status, f.Reason, f.Severity, tc.status, tc.reason, tc.severity) + } + if tc.reason != "load_balancer_limit" { + return + } + // Requested, allocated and delivered stay apart. + if f.RequestedA == nil || math.Abs(*f.RequestedA-15) > 0.01 || f.OfferedA == nil || *f.OfferedA != 15 || + f.LimitA == nil || *f.LimitA != 13 || f.LimitedBy != "load_balancer" || f.ActualW == nil || *f.ActualW != 8850 { + t.Fatalf("numbers: requested %v, offer %v, limit %v by %q, actual %v", f.RequestedA, f.OfferedA, f.LimitA, f.LimitedBy, f.ActualW) + } + }) + }) + } +} + +// The trace's balancer stepped between 12, 14 and 13 A. When it lets the +// car take more, the cloud reports the car's old draw for a while; that is +// the balancer's limit, not a charger that stopped following. +func TestEaseeLoadBalancingStepsUp(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + e := newEVTrace(8280) + e.extra = `,"max_a":15,"current_limit_a":12,"current_limited_by":"load_balancer"` + for range 5 * 60 / 5 { + e.tick(10350, "plan") + } + if f := e.verdict(t); f.Reason != "load_balancer_limit" { + t.Fatalf("at 12 A: %s/%s", f.Status, f.Reason) + } + e.extra = `,"max_a":15,"current_limit_a":14,"current_limited_by":"load_balancer"` + for range 4 { + e.tick(10350, "plan") + if f := e.verdict(t); f.Reason != "load_balancer_limit" { + t.Fatalf("after the step to 14 A, car still at 8,280 W: %s/%s", f.Status, f.Reason) + } + } + e.drawW, e.changedAt = 9660, time.Now() + e.tick(10350, "plan") + if f := e.verdict(t); f.Reason != "load_balancer_limit" || *f.LimitA != 14 { + t.Fatalf("at 14 A: %s/%s", f.Status, f.Reason) + } + }) +} + +// The sentence names the charger's make with its limit, from the identity +// its driver reported. Rows without a limit do not carry it. +func TestChargerLimitCarriesTheMake(t *testing.T) { + dir := t.TempDir() + lua := filepath.Join(dir, "easee_like.lua") + src := ` +function driver_init(config) host.set_make("Easee") end +function driver_poll() return 60000 end +function driver_command(action, power_w, cmd) return true end +function driver_default_mode() end +` + if err := os.WriteFile(lua, []byte(src), 0o644); err != nil { + t.Fatal(err) + } + tel := telemetry.NewStore() + reg := drivers.NewRegistry(tel) + t.Cleanup(reg.ShutdownAll) + if err := reg.Add(context.Background(), config.Driver{Name: "easee", Lua: lua}); err != nil { + t.Fatal(err) + } + srv := New(&Deps{Tel: tel, Registry: reg}) + for _, tc := range []struct { + extra, make string + }{ + {`,"current_limit_a":13,"current_limited_by":"load_balancer"`, "Easee"}, + {``, ""}, + } { + c := tel.BeginCommand("easee", []byte(`{"action":"ev_set_current","power_w":10350}`), time.Now()) + tel.CompleteCommand(c, "accepted") + tel.Update("easee", telemetry.DerEV, 8850, nil, []byte(`{"connected":true`+tc.extra+`}`)) + rows := srv.controlFeedback(time.Now()) + if len(rows) != 1 || rows[0].Make != tc.make { + t.Fatalf("extra %q: rows %+v, want make %q", tc.extra, rows, tc.make) + } + } + // Stale readings drop the limit, its cause and the make together. + tel.Update("easee", telemetry.DerEV, 8850, nil, []byte(`{"connected":true,"current_limit_a":13,"current_limited_by":"load_balancer"}`)) + rows := srv.controlFeedback(time.Now().Add(2 * time.Minute)) + if len(rows) != 1 || rows[0].Reason != "telemetry_stale" || rows[0].LimitA != nil || rows[0].LimitedBy != "" || rows[0].Make != "" { + t.Fatalf("stale row keeps its limit: %+v", rows) + } +} diff --git a/go/internal/api/api_ev_not_following_test.go b/go/internal/api/api_ev_not_following_test.go index 2592f6100..cfd9dfdd1 100644 --- a/go/internal/api/api_ev_not_following_test.go +++ b/go/internal/api/api_ev_not_following_test.go @@ -19,6 +19,7 @@ type evTrace struct { srv *Server changedAt time.Time drawW float64 + extra string // more reading fields, each with a leading comma } func newEVTrace(drawW float64) *evTrace { @@ -35,8 +36,8 @@ func (e *evTrace) tick(w float64, reason string) { e.tel.CompleteCommand(c, "accepted") e.mgr.SetCommanded("garage", w, reason) e.tel.Update("easee", telemetry.DerEV, e.drawW, nil, []byte(fmt.Sprintf( - `{"connected":true,"control_power_observed_at":%q,"control_power_confirmed":true,"power_max_age_s":180}`, - e.changedAt.Format(time.RFC3339Nano)))) + `{"connected":true,"control_power_observed_at":%q,"control_power_confirmed":true,"power_max_age_s":180%s}`, + e.changedAt.Format(time.RFC3339Nano), e.extra))) e.mgr.Observe("garage", true, e.drawW, 0, true) time.Sleep(5 * time.Second) } diff --git a/go/internal/api/api_pilot_evidence_test.go b/go/internal/api/api_pilot_evidence_test.go new file mode 100644 index 000000000..35d76c3fe --- /dev/null +++ b/go/internal/api/api_pilot_evidence_test.go @@ -0,0 +1,128 @@ +package api + +import ( + "encoding/json" + "fmt" + "github.com/srcfl/ftw/go/internal/telemetry" + "testing" + "testing/synctest" + "time" +) + +func TestPilotFreshnessRequiresSpecificConfirmation(t *testing.T) { + now := time.Now().Truncate(time.Millisecond) + ms := now.UnixMilli() + base := feedbackReading{PilotA: watts(13), PilotObservedAt: now.Add(-24 * time.Hour).Format(time.RFC3339Nano), PilotConfirmedAtMs: &ms} + if age, ok := pilotReadingAge(base, now); !ok || age != 0 { + t.Fatal("unchanged old source value with current confirmation rejected") + } + // A confirmation past the window is valid but too old to explain a draw. + old := now.Add(-91 * time.Second).UnixMilli() + stale := base + stale.PilotConfirmedAtMs = &old + if age, ok := pilotReadingAge(stale, now); !ok || age <= pilotFreshFor { + t.Fatalf("stale confirmation: age %v ok %v", age, ok) + } + for _, tc := range []struct { + name string + change func(*feedbackReading) + }{ + {"missing confirmation", func(d *feedbackReading) { d.PilotConfirmedAtMs = nil }}, + {"zero confirmation", func(d *feedbackReading) { v := int64(0); d.PilotConfirmedAtMs = &v }}, + {"missing source", func(d *feedbackReading) { d.PilotObservedAt = "" }}, + {"future confirmation", func(d *feedbackReading) { v := now.Add(time.Second).UnixMilli(); d.PilotConfirmedAtMs = &v }}, + {"future source", func(d *feedbackReading) { d.PilotObservedAt = now.Add(time.Second).Format(time.RFC3339Nano) }}, + {"negative pilot", func(d *feedbackReading) { d.PilotA = watts(-1) }}, + } { + t.Run(tc.name, func(t *testing.T) { + d := base + tc.change(&d) + if _, ok := pilotReadingAge(d, now); ok { + t.Fatal("invalid pilot accepted") + } + }) + } +} + +func TestPilotAwareDriverFeedback(t *testing.T) { + // The charger holds FTW's 15 A order; limit is the driver's current the + // car may take, and age the pilot confirmation's age at the verdict. + const at13 = `,"current_limit_a":13` + const balanced = at13 + `,"current_limited_by":"load_balancer"` + for _, tc := range []struct { + name string + draw, pilot float64 + limit string + device bool // the charger's own 13 A limit + age time.Duration + result, reason, severity string + }{ + {"fresh lower pilot, internal cause unknown", 8850, 13, at13, false, 5 * time.Second, "limited", "charger_pilot_limit", "info"}, + {"confirmed static limit", 8850, 13, at13, true, 5 * time.Second, "limited", "device_limit", "warning"}, + {"static limit cannot hide low draw", 4000, 13, at13, true, 5 * time.Second, "not_following", "power_below_target", "warning"}, + {"static limit without a current limit", 8850, 13, "", true, 5 * time.Second, "limited", "device_limit", "warning"}, + {"pilot at the end of its window", 8850, 13, at13, false, pilotFreshFor, "limited", "charger_pilot_limit", "info"}, + {"stale pilot cannot explain power", 8850, 13, at13, false, pilotFreshFor + time.Second, "limited", "offered_current_lower", "warning"}, + {"stale pilot keeps the load balancing", 8850, 13, balanced, false, pilotFreshFor + time.Second, "limited", "load_balancer_limit", "info"}, + {"future confirmation cannot explain power", 8850, 13, at13, false, -time.Minute, "not_following", "power_below_target", "warning"}, + {"zero delivery stays visible", 0, 13, at13, false, 5 * time.Second, "not_following", "no_power_response", "warning"}, + {"zero pilot cannot explain ongoing draw", 8850, 0, at13, false, 5 * time.Second, "not_following", "power_below_target", "warning"}, + {"delivery below effective pilot stays visible", 4000, 13, at13, false, 5 * time.Second, "not_following", "power_below_target", "warning"}, + {"pilot above the order", 8850, 20, at13, false, 5 * time.Second, "limited", "offered_current_lower", "warning"}, + {"pilot above the limit", 8850, 14.2, at13, false, 5 * time.Second, "limited", "offered_current_lower", "warning"}, + {"pilot within 0.5 A of the order", 9900, 14.8, `,"current_limit_a":14.4`, false, 5 * time.Second, "limited", "offered_current_lower", "warning"}, + } { + t.Run(tc.name, func(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + e := newEVTrace(tc.draw) + for range 6 * 60 / 5 { + // tick waits five seconds before the verdict. + confirmed := time.Now().Add(5*time.Second - tc.age) + e.extra = fmt.Sprintf(`,"max_a":15,"dynamic_limit_a":15%s,"pilot_a":%g,"pilot_observed_at":%q,"pilot_confirmed_at_ms":%d`, tc.limit, tc.pilot, time.Now().Add(-time.Hour).Format(time.RFC3339Nano), confirmed.UnixMilli()) + if tc.device { + e.extra += `,"device_limit_a":13,"device_limit_age_s":0` + } + e.tick(10350, "plan") + } + f := e.verdict(t) + if f.Status != tc.result || f.Reason != tc.reason || f.Severity != tc.severity { + t.Fatalf("got %s/%s/%s want %s/%s/%s", f.Status, f.Reason, f.Severity, tc.result, tc.reason, tc.severity) + } + // A pilot past its window is unknown, not shown. + if tc.age > pilotFreshFor && (f.PilotA != nil || f.PilotConfirmedAtMs != 0) { + t.Fatalf("stale pilot shown: %+v", f) + } + if tc.reason == "charger_pilot_limit" { + if f.Severity != "info" || f.PilotA == nil || *f.PilotA != 13 || f.DynamicLimitA == nil || *f.DynamicLimitA != 15 { + t.Fatalf("pilot evidence: %+v", f) + } + raw, err := json.Marshal(f) + if err != nil { + t.Fatal(err) + } + var out map[string]any + if err = json.Unmarshal(raw, &out); err != nil { + t.Fatal(err) + } + if out["pilot_a"] != float64(13) || out["dynamic_limit_a"] != float64(15) { + t.Fatal(string(raw)) + } + } + }) + }) + } +} + +func TestPilotLimitNeverHidesCommandFailureOrPendingCommand(t *testing.T) { + now := time.Now() + for _, tc := range []struct{ result, reason string }{{"failed", "command_failed"}, {"unconfirmed", "command_unconfirmed"}, {"pending", "waiting_response"}} { + t.Run(tc.result, func(t *testing.T) { + f := ControlFeedback{Kind: "ev", SentW: watts(10350), RequestedW: watts(10350), RequestedA: watts(15), OfferedA: watts(15), PilotA: watts(13), LimitA: watts(13), ActualW: watts(8850), ObservedAtMs: now.UnixMilli()} + cmd := telemetry.CommandEvidence{Result: tc.result, Since: now.Add(-3 * time.Minute)} + classifyControlFeedback(&f, cmd, true, true, "", nil, now) + if f.Reason != tc.reason { + t.Fatalf("got %s want %s", f.Reason, tc.reason) + } + }) + } +} diff --git a/go/internal/control/sitesim_field_test.go b/go/internal/control/sitesim_field_test.go index 83de1bf02..094df99be 100644 --- a/go/internal/control/sitesim_field_test.go +++ b/go/internal/control/sitesim_field_test.go @@ -82,6 +82,7 @@ type fieldCase struct { exportW []float64 // surplus: export each quarter hour houseStep time.Duration // > 0: L1 +8 A for 10 min from this time into the run warm bool // the fuse starts warm: the house has carried its load all evening + balancerA float64 // > 0: the charger's own load balancing keeps each phase under this } func (c fieldCase) key() string { @@ -99,7 +100,7 @@ func (c fieldCase) key() string { text string }{ {c.battery, "battery"}, {c.charger != "", "charger" + c.charger}, {c.planW > 0, fmt.Sprintf("plan%gW", c.planW)}, {c.needW > 0, fmt.Sprintf("need%gW", c.needW)}, - {c.goal, "goal"}, {export != "", export}, {c.houseStep > 0, "L1step"}, + {c.goal, "goal"}, {export != "", export}, {c.houseStep > 0, "L1step"}, {c.balancerA > 0, fmt.Sprintf("balancer%gA", c.balancerA)}, } { if v.on { name += "+" + v.text @@ -172,7 +173,9 @@ func helpReport(use fieldUse, tm fieldTiming) fieldCase { // pr1763Site is the site in srcfl/ftw#1763's comments: 3 × 25 A, Easee Cloud // and Pixii, the battery charging from the plan while the car charges, -// offers to 15 A of which the car takes about 12 A, pause and restart. +// offers to 15 A of which the car takes about 12 A, pause and restart. Here +// the car holds itself at 8.3 kW. The trace of 23 Oct 2026 showed that on +// this site Easee's own load balancing did it; probeCases has that charger. func pr1763Site(use fieldUse, tm fieldTiming) fieldCase { return fieldCase{report: "pr1763", fuseA: 25, mode: "3p", steps: "3p-15A", timing: tm, carPhases: 3, use: use, house: [3]float64{6, 2, 2}, houseName: "6-2-2", battery: true, @@ -316,6 +319,14 @@ func probeCases() []fieldCase { out = append(out, fieldCase{report: "car2300W", fuseA: 25, steps: steps, timing: timingFast, carPhases: 1, use: useNoPlan, house: houseBalanced, houseName: "balanced", car: func(car *sitesim.Car) { car.MaxW = 2300 }}) } + for _, use := range []fieldUse{usePlanned, useNow} { + // The charger in srcfl/ftw#1763's trace: Easee's own load balancing + // keeps each phase under 18 A after the house, so the car gets 12 A + // of a 15 A order, and the driver reports it (easee_cloud 1.4.1). + c := pr1763Site(use, timingEasee) + c.battery, c.balancerA, c.car = false, 18, nil + out = append(out, c) + } c := helpReport(usePlanned, timingEasee) c.houseStep = 30 * time.Minute return append(out, c) @@ -444,6 +455,8 @@ type fieldRun struct { trace func(format string, args ...any) // FTW_FIELD_TRACE names the case tracedReason string exportPeak func(window time.Duration) (float64, bool) // surplus: the forecast from now + balancedA float64 // what the charger's own load balancing lets the car take, 0 for none + balancedSince time.Time } // FieldVerdict returns a reader of the EV verdict the API shows for a store @@ -477,6 +490,24 @@ func (s *simSite) evData() map[string]any { if age := s.field.c.timing.declared; age > 0 { d["power_max_age_s"] = age.Seconds() } + // The charger's own load balancing, reported as easee_cloud 1.4.1 does + // from Easee's allocation and pilot: the whole amps it lets the car take + // while that is below the order in force. + f, e, limitA := s.field, s.p.EVSE, 0.0 + if e.LoadBalancerA > 0 && s.ev.Plugged && e.OfferA > 0 { + d["max_a"] = e.OfferA // the order the charger holds, Easee's observation 48 + a := e.OfferA + for i := range min(max(e.OfferPhases, 1), e.Phases, s.p.Car.Phases) { + a = math.Min(a, e.LoadBalancerA-s.p.HouseW[i]/s.p.Voltage) + } + if a = math.Floor(a); a >= e.MinA && a < e.OfferA-0.5 { + limitA = a + d["current_limit_a"], d["current_limited_by"] = a, "load_balancer" + } + } + if limitA != f.balancedA { + f.balancedA, f.balancedSince = limitA, s.p.Now + } return d } @@ -683,9 +714,22 @@ func (f *fieldRun) check(s *simSite) { // Core waits its response time for an order to take hold, then two // minutes of measured disagreement (srcfl/ftw#1795). A car marked // one-phase is judged against a third of a three-phase order, the - // expected_w Core sends with it (srcfl/ftw#1801). + // expected_w Core sends with it (srcfl/ftw#1801). A car held at what the + // charger's own load balancing allows, as its driver reports, is + // limited and must say so (srcfl/ftw#1763); in the first 30 s after the + // balancer moves, Core's reading may still show the old draw. if f.verdict != nil && !f.mismatchSince.IsZero() && now.Sub(f.mismatchSince) >= 2*time.Minute+30*time.Second { - if status, reason := f.verdict(); status != "not_following" && reason != "one_phase" { + status, reason := f.verdict() + ok := status == "not_following" || reason == "one_phase" + if limitW := f.balancedA * p.Voltage * float64(p.EVSE.DrawPhases); f.balancedA > 0 && + math.Abs(p.EVSE.DrawW-limitW) <= telemetry.ControlToleranceW(limitW) { + if now.Sub(f.balancedSince) >= 30*time.Second { + ok = reason == "load_balancer_limit" + } else { + ok = ok || reason == "load_balancer_limit" + } + } + if !ok { f.fail(s, failVerdict, "the car draws %.0f W against an expected %.0f W (%v), the verdict says %s/%s", p.EVSE.DrawW, f.expectW, now.Sub(f.orderAt).Round(time.Second), status, reason) } @@ -701,6 +745,9 @@ func fieldRoomW(c fieldCase, e *sitesim.EVSE, car *sitesim.Car) float64 { worst = math.Max(worst, c.house[i]) } roomA := math.Min(c.fuseA-simFuseSafetyA-worst, e.MaxA) + if e.LoadBalancerA > 0 { + roomA = math.Min(roomA, e.LoadBalancerA-worst) + } if roomA < e.MinA { return 0 } @@ -726,6 +773,7 @@ func runFieldCase(c fieldCase, trace func(string, ...any)) (*simSite, *fieldRun) if c.charger == "1p32A" { p.EVSE.Phases, p.EVSE.MaxA = 1, 32 } + p.EVSE.LoadBalancerA = c.balancerA if c.timing.cloud { p.EVSE.ReadbackLag, p.EVSE.SplitAtA, p.EVSE.PhaseHold = 10*time.Second, c.fuseA, 90*time.Second } diff --git a/web/control-feedback.js b/web/control-feedback.js index e81644039..9d0d3c0d1 100644 --- a/web/control-feedback.js +++ b/web/control-feedback.js @@ -66,6 +66,7 @@ const REASON_TITLES = { core_limit: 'Limited by FTW', fuse_limit: 'Limited by the main fuse', fuse_cooldown: 'Waiting after a site power limit', import_limit: 'Limited by the hourly import limit', charger_limit: 'Limited by FTW’s charger setting', device_limit: 'Charger limit', offered_current_lower: 'Charger offers less current', + load_balancer_limit: 'Limited by the charger’s load balancing', charger_pilot_limit: 'Limited by the charger', one_phase: 'Charging on one phase', device_fault: 'Device fault', telemetry_stale: 'No fresh readings', readings_lost: 'Lost control', command_failed: 'Commands failing', command_unconfirmed: 'Command not confirmed', default_failed: 'Cannot hand back control', @@ -99,7 +100,12 @@ function sentence(row) { case 'fuse_cooldown': return 'FTW waits a moment after a site power limit before it raises the rate.'; case 'charger_limit': return 'The charger limit in FTW’s settings caps the rate.'; case 'device_limit': return `The charger’s own limit is ${amps(row.device_limit_a)}, below the ${amps(row.requested_a)} FTW asked for.`; - case 'offered_current_lower': return `The charger offers ${amps(row.offered_a)}, below the ${amps(row.requested_a)} FTW asked for.`; + case 'offered_current_lower': return num(row.limit_a) && !(num(row.offered_a) && num(row.requested_a) && row.offered_a + 0.5 < row.requested_a) + ? `The charger lets the car take ${amps(row.limit_a)}, below the ${amps(row.requested_a)} FTW asked for.` + : `The charger offers ${amps(row.offered_a)}, below the ${amps(row.requested_a)} FTW asked for.`; + case 'charger_pilot_limit': return `The charger signals ${amps(row.pilot_a)} to the car. FTW asked for ${amps(row.requested_a)}; measured charging is ${controlPower(actual)}. The internal limiting cause is unknown.`; + case 'load_balancer_limit': return `${row.make ? `${row.make} limits the charger to ${amps(row.limit_a)} (its load balancing)` + : `The charger’s load balancing limits it to ${amps(row.limit_a)}`}. FTW asked for ${amps(row.requested_a)}; the car takes ${controlPower(actual)}.`; case 'one_phase': return `The car charges on ${row.charging_phase || 'one phase'} only${num(row.charging_phase_max_w) ? `, at most ${controlPower(row.charging_phase_max_w)}` : ''}. FTW plans with what it takes.`; case 'power_below_target': case 'power_above_target': return `Asked for ${target(row)}, delivering ${controlPower(actual)}.`; case 'power_wrong_direction': return `Asked for ${target(row)}, but it is ${doing(row, actual)} ${controlPower(actual)}.`; @@ -137,6 +143,8 @@ function nextStep(row) { case 'site_meter_stale': case 'site_phase_currents_stale': return 'Check the grid meter’s connection.'; case 'device_limit': return 'Raise the limit in the charger’s app if you want faster charging.'; case 'offered_current_lower': return 'Check the charger’s load balancing and limits.'; + case 'charger_pilot_limit': return 'Check Available energy in the charger’s app for the active limit.'; + case 'load_balancer_limit': return 'If this happens often, compare the charger’s load balancing settings with FTW’s main fuse setting.'; case 'observe_only': return 'Turn on control in the device settings if you want FTW to steer it.'; default: return ''; } @@ -225,8 +233,11 @@ export function controlNumbers(row, live = true) { if (num(row.battery_soc)) values.push(['Battery charge', percent(row.battery_soc)]); if (num(row.tolerance_w)) values.push(['Response tolerance', `±${controlPower(row.tolerance_w)}`]); if (num(row.requested_a)) values.push(['Requested current', amps(row.requested_a)]); - if (num(row.offered_a)) values.push(['Charger offer', amps(row.offered_a)]); + if (num(row.pilot_a)) values.push(['Actual pilot current', amps(row.pilot_a)]); + if (num(row.dynamic_limit_a)) values.push(['Dynamic current setting', amps(row.dynamic_limit_a)]); + else if (num(row.offered_a)) values.push(['Reported current setting', amps(row.offered_a)]); if (num(row.device_limit_a)) values.push(['Charger limit', amps(row.device_limit_a)]); + if (num(row.limit_a)) values.push([row.limited_by === 'load_balancer' ? 'Load balancing allows' : 'Charger lets the car take', amps(row.limit_a)]); const e = row.site_evidence; if (e && typeof e === 'object') { for (const [label, key] of [['Grid before', 'grid_before_w'], ['Grid after', 'grid_after_w']]) { diff --git a/web/control-feedback.test.mjs b/web/control-feedback.test.mjs index 9250313ca..08df34995 100644 --- a/web/control-feedback.test.mjs +++ b/web/control-feedback.test.mjs @@ -59,6 +59,27 @@ test('expected limits stay calm and explain themselves', () => { assert.equal(car.title, 'Car is full'); const limit = controlStatus(row({kind: 'ev', status: 'limited', reason: 'device_limit', severity: 'warning', device_limit_a: 8, requested_a: 16})); assert.equal(limit.text, 'The charger’s own limit is 8 A, below the 16 A FTW asked for.'); + // Field trace, 23 Oct 2026: 15 A ordered, Easee's load balancing allowed 13 A. + const lb = {kind: 'ev', mode: 'plan', status: 'limited', reason: 'load_balancer_limit', severity: 'info', + sent_w: 10350, actual_w: 8850, requested_a: 15, offered_a: 15, device_limit_a: 16, limit_a: 13, limited_by: 'load_balancer'}; + const balanced = controlStatus(row({...lb, make: 'Easee'})); + assert.equal(balanced.title, 'Limited by the charger’s load balancing'); + assert.equal(balanced.text, 'Easee limits the charger to 13 A (its load balancing). FTW asked for 15 A; the car takes 8.8 kW.'); + assert.equal(balanced.next, 'If this happens often, compare the charger’s load balancing settings with FTW’s main fuse setting.'); + assert.equal(balanced.tone, 'neutral'); + assert.equal(controlStatus(row(lb)).text, 'The charger’s load balancing limits it to 13 A. FTW asked for 15 A; the car takes 8.8 kW.'); + assert.deepEqual(controlNumbers(row(lb)).filter(([label]) => /current|offer|limit|allows/i.test(label)), [ + ['Requested current', '15 A'], ['Reported current setting', '15 A'], ['Charger limit', '16 A'], ['Load balancing allows', '13 A']]); + // A lower current with no cause shown says what the charger does, no more. + const unexplained = {...lb, reason: 'offered_current_lower', severity: 'warning', limited_by: undefined}; + assert.equal(controlStatus(row(unexplained)).text, 'The charger lets the car take 13 A, below the 15 A FTW asked for.'); + assert.deepEqual(controlNumbers(row(unexplained)).at(-1), ['Charger lets the car take', '13 A']); + // A charger holding a lower order than FTW sent. + const lower = controlStatus(row({kind: 'ev', status: 'limited', reason: 'offered_current_lower', severity: 'warning', offered_a: 12, requested_a: 15})); + assert.equal(lower.text, 'The charger offers 12 A, below the 15 A FTW asked for.'); + // A lower held order is said first, even when the driver reports less. + const both = controlStatus(row({kind: 'ev', status: 'limited', reason: 'offered_current_lower', severity: 'warning', offered_a: 12, limit_a: 10, requested_a: 15})); + assert.equal(both.text, 'The charger offers 12 A, below the 15 A FTW asked for.'); const onePhase = controlStatus(row({kind: 'ev', mode: 'plan', status: 'limited', reason: 'one_phase', severity: 'info', sent_w: 3680, actual_w: 3650, charging_phase: 'L1', charging_phase_max_w: 3680})); assert.equal(onePhase.title, 'Charging on one phase'); @@ -160,3 +181,16 @@ test('limited positive EV offer does not explain prolonged zero delivery', () => assert.match(hour.text,/this hour’s grid import/); assert.doesNotMatch(hour.text,/fuse/); }); + +test('a confirmed lower pilot is an explained restriction with separate current values', () => { + const r = row({kind:'ev',status:'limited',reason:'charger_pilot_limit',severity:'info', + requested_a:15,offered_a:15,dynamic_limit_a:15,pilot_a:13,actual_w:8850,sent_w:10350}); + const words=controlStatus(r); + assert.match(words.title,/Limited by the charger/); + assert.match(words.text,/signals 13 A/); + assert.match(words.text,/internal limiting cause is unknown/); + const numbers=controlNumbers(r); + assert.ok(numbers.some(([label,value])=>label==='Actual pilot current'&&value==='13 A')); + assert.ok(numbers.some(([label,value])=>label==='Dynamic current setting'&&value==='15 A')); + assert.ok(!numbers.some(([label])=>label==='Charger offer')); +});