From a014175b09f2ac42933048ad52c40e963265eafd Mon Sep 17 00:00:00 2001 From: andig Date: Mon, 6 May 2024 21:09:08 +0200 Subject: [PATCH] Load mangement: fix handling overloaded circuits (#13787) --- core/circuit.go | 17 ++++++++--------- core/circuit_test.go | 4 ++-- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/core/circuit.go b/core/circuit.go index 7feab2d66..cab7c12e4 100644 --- a/core/circuit.go +++ b/core/circuit.go @@ -2,7 +2,6 @@ package core import ( "fmt" - "math" "sync" "github.com/evcc-io/evcc/api" @@ -176,16 +175,14 @@ func (c *Circuit) updateLoadpoints(loadpoints []api.CircuitLoad) { func (c *Circuit) updateMeters() error { if f, err := c.meter.CurrentPower(); err == nil { - // TODO handle negative powers - c.power = math.Abs(f) + c.power = f } else { return fmt.Errorf("circuit power: %w", err) } if phaseMeter, ok := c.meter.(api.PhaseCurrents); ok { if l1, l2, l3, err := phaseMeter.Currents(); err == nil { - // TODO handle negative currents - c.current = max(math.Abs(l1), math.Abs(l2), math.Abs(l3)) + c.current = max(l1, l2, l3) } else { return fmt.Errorf("circuit currents: %w", err) } @@ -246,8 +243,9 @@ func (c *Circuit) ValidatePower(old, new float64) float64 { delta := max(0, new-old) if c.maxPower != 0 { - if c.power+delta > c.maxPower { - new = max(0, new-(c.power+delta-c.maxPower)) + potential := c.maxPower - c.power + if delta > potential { + new = max(0, old+potential) c.log.DEBUG.Printf("validate power: %gW -> %gW <= %gW at %gW: capped at %gW", old, new, c.maxPower, c.power, new) } else { c.log.TRACE.Printf("validate power: %gW -> %gW <= %gW at %gW: ok", old, new, c.maxPower, c.power) @@ -270,8 +268,9 @@ func (c *Circuit) ValidateCurrent(old, new float64) float64 { delta := max(0, new-old) if c.maxCurrent != 0 { - if c.current+delta > c.maxCurrent { - new = max(0, new-(c.current+delta-c.maxCurrent)) + potential := c.maxCurrent - c.current + if delta > potential { + new = max(0, old+potential) c.log.DEBUG.Printf("validate current: %gA -> %gA <= %gA at %gA: capped at %gA", old, new, c.maxCurrent, c.current, new) } else { c.log.TRACE.Printf("validate current: %gA -> %gA <= %gA at %gA: ok", old, new, c.maxCurrent, c.current) diff --git a/core/circuit_test.go b/core/circuit_test.go index 785ba1546..734049086 100644 --- a/core/circuit_test.go +++ b/core/circuit_test.go @@ -37,7 +37,7 @@ func circuitTests() []circuitTest { {0, 2, 0, 1, 1, 0}, // = {0, 2, 0, 2, 2, 1}, // = {0, 2, 0, 2, 3, 1}, // + - // {0, 2, 0, 2, 1, 1}, // - + {0, 2, 0, 2, 1, 1}, // - // parent loaded {1, 0, 0, 0, 0, 0}, // = @@ -52,7 +52,7 @@ func circuitTests() []circuitTest { {2, 0, 0, 1, 1, 0}, // = {2, 0, 0, 2, 2, 1}, // = {2, 0, 0, 2, 3, 1}, // + - // {2, 0, 0, 2, 1, 1}, // - + {2, 0, 0, 2, 1, 1}, // - } }