From 3b21471e7eb05f55b9ec04d3c7b48b6efe2b7459 Mon Sep 17 00:00:00 2001 From: Bernhard Kirchen Date: Fri, 6 Oct 2023 21:39:46 +0200 Subject: [PATCH] DPL: (re-)send power limits periodically avoid staleness in case the same power limit is calculated over and over again, hence no new power limit value is calculated and hence no power limit command is sent to the inverter. staleness occurs in this case if the first power limit command to establish the respective limit was not received by the inverter. one can easily simulate a situation where the same power limit is caluclated over and over again: with a battery above the start threshold, set a very low upper power limit for the inverter (DPL setting). that value will be used as the limit as long as the power meter reading is larger than that. we could also check the limit reported by the inverter. however, that value is in percent of the inverter's max AC output, and is often not the same value as we requested as the limit, but slightly off. we then would have to decide how much deviation is okay, which is unreasonably complicated. closes #478. --- include/PowerLimiter.h | 1 + src/PowerLimiter.cpp | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/include/PowerLimiter.h b/include/PowerLimiter.h index 6bf1497aa..c2b92d460 100644 --- a/include/PowerLimiter.h +++ b/include/PowerLimiter.h @@ -64,6 +64,7 @@ class PowerLimiterClass { private: int32_t _lastRequestedPowerLimit = 0; + uint32_t _lastPowerLimitMillis = 0; uint32_t _shutdownTimeout = 0; Status _lastStatus = Status::Initializing; uint32_t _lastStatusPrinted = 0; diff --git a/src/PowerLimiter.cpp b/src/PowerLimiter.cpp index d12d10ac3..c5c3dc868 100644 --- a/src/PowerLimiter.cpp +++ b/src/PowerLimiter.cpp @@ -496,6 +496,7 @@ void PowerLimiterClass::commitPowerLimit(std::shared_ptr inver PowerLimitControlType::AbsolutNonPersistent); _lastRequestedPowerLimit = limit; + _lastPowerLimitMillis = millis(); // enable power production only after setting the desired limit, // such that an older, greater limit will not cause power spikes. @@ -546,17 +547,23 @@ bool PowerLimiterClass::setNewPowerLimit(std::shared_ptr inver // Check if the new value is within the limits of the hysteresis auto diff = std::abs(effPowerLimit - _lastRequestedPowerLimit); - if ( diff < config.PowerLimiter_TargetPowerConsumptionHysteresis) { + auto hysteresis = config.PowerLimiter_TargetPowerConsumptionHysteresis; + + // (re-)send power limit in case the last was sent a long time ago. avoids + // staleness in case a power limit update was not received by the inverter. + auto ageMillis = millis() - _lastPowerLimitMillis; + + if (diff < hysteresis && ageMillis < 60 * 1000) { if (_verboseLogging) { - MessageOutput.printf("[DPL::setNewPowerLimit] reusing old limit: %d W, diff: %d W, hysteresis: %d W\r\n", - _lastRequestedPowerLimit, diff, config.PowerLimiter_TargetPowerConsumptionHysteresis); + MessageOutput.printf("[DPL::setNewPowerLimit] requested: %d W, last limit: %d W, diff: %d W, hysteresis: %d W, age: %ld ms\r\n", + newPowerLimit, _lastRequestedPowerLimit, diff, hysteresis, ageMillis); } return false; } if (_verboseLogging) { - MessageOutput.printf("[DPL::setNewPowerLimit] using new limit: %d W, requested power limit: %d W\r\n", - effPowerLimit, newPowerLimit); + MessageOutput.printf("[DPL::setNewPowerLimit] requested: %d W, (re-)sending limit: %d W\r\n", + newPowerLimit, effPowerLimit); } commitPowerLimit(inverter, effPowerLimit, true);