optimizer: fix pause suggestion for idle-but-enabled loadpoints (#31792)

This commit is contained in:
andig 2026-07-14 21:38:39 +02:00 • committed by GitHub
parent 0e62646cd8
commit 3a2711cc4f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 45 additions and 19 deletions

View file

@ -1034,22 +1034,24 @@ func (lp *Loadpoint) charging() bool {
// PvChargeStarting reports a PV loadpoint that claimed surplus via a running
// enable timer but is not yet drawing it and has not reached its goal. See #31194, #31684.
func (lp *Loadpoint) PvChargeStarting() bool {
if lp.GetMode() != api.ModePV || !lp.connected() || lp.chargeGoalReached() {
lp.RLock()
enabled := lp.enabled
pvTimerRunning := !lp.pvTimer.IsZero()
lp.RUnlock()
if lp.GetMode() != api.ModePV || !lp.connected() || lp.chargeGoalReached(enabled) {
return false
}
lp.RLock()
defer lp.RUnlock()
// enable timer running (not yet enabled)
return !lp.enabled && !lp.pvTimer.IsZero()
return !enabled && pvTimerRunning
}
// chargeGoalReached reports whether the loadpoint will not draw more: enabled
// but not charging, an energy limit reached, or soc at/above the limit (#31684).
func (lp *Loadpoint) chargeGoalReached() bool {
func (lp *Loadpoint) chargeGoalReached(enabled bool) bool {
// enabled but drawing nothing: it won't ramp up
if lp.IsEnabled() && !lp.charging() {
if enabled && !lp.charging() {
return true
}

View file

@ -142,13 +142,6 @@ func (lp *Loadpoint) GetStatus() api.ChargeStatus {
return lp.status
}
// IsEnabled returns the charger enabled state
func (lp *Loadpoint) IsEnabled() bool {
lp.RLock()
defer lp.RUnlock()
return lp.enabled
}
// GetMode returns loadpoint charge mode
func (lp *Loadpoint) GetMode() api.ChargeMode {
lp.RLock()

View file

@ -157,6 +157,21 @@ func currentSlotSuggestion(detail batteryDetail, res optimizer.BatteryResult, gr
return s
}
// loadpointCurrentAction returns the loadpoint's current operating mode for
// suggestion comparison, reusing chargeGoalReached so a loadpoint left
// enabled while idle (e.g. vehicle finished at its limit) is treated as
// stopped instead of triggering a spurious pause suggestion.
func loadpointCurrentAction(lp *Loadpoint) string {
lp.RLock()
enabled := lp.enabled
lp.RUnlock()
if enabled && !lp.chargeGoalReached(enabled) {
return actionCharge
}
return actionStop
}
// setBatterySuggestions replaces the suggestions applied on each battery publish
func (site *Site) setBatterySuggestions(suggestions map[string]types.Suggestion) {
site.Lock()
@ -425,11 +440,7 @@ func (site *Site) optimizerUpdate(battery []types.Measurement) error {
if detail.Type == batteryTypeBattery {
current = site.GetBatteryMode().String()
} else if detail.loadpoint != nil {
if site.loadpoints[*detail.loadpoint].IsEnabled() {
current = actionCharge
} else {
current = actionStop
}
current = loadpointCurrentAction(site.loadpoints[*detail.loadpoint])
}
suggestion := currentSlotSuggestion(detail, batResp, gridImporting, gridExporting, slotHours, current)

View file

@ -27,6 +27,26 @@ func TestLoadpointProfile(t *testing.T) {
require.Equal(t, []float64{250, 250, 250, 250, 250, 250, 250, 50}, loadpointProfile(lp, 8))
}
func TestLoadpointCurrentAction(t *testing.T) {
for _, tc := range []struct {
name string
enabled bool
status api.ChargeStatus
soc float64
want string
}{
{"charging", true, api.StatusC, 0, actionCharge},
{"enabled but idle (e.g. vehicle finished at limit)", true, api.StatusB, 0, actionStop},
{"disabled", false, api.StatusB, 0, actionStop},
{"charging at 100% soc with no explicit limit", true, api.StatusC, 100, actionStop},
} {
t.Run(tc.name, func(t *testing.T) {
lp := &Loadpoint{enabled: tc.enabled, status: tc.status, vehicleSoc: tc.soc}
assert.Equal(t, tc.want, loadpointCurrentAction(lp))
})
}
}
func TestAsTimestamps(t *testing.T) {
// now is 10 minutes into a 15-minute slot
now := time.Date(2025, 1, 1, 12, 10, 0, 0, time.UTC)