diff --git a/include/PowerLimiter.h b/include/PowerLimiter.h index 8da2628f9..db69a303b 100644 --- a/include/PowerLimiter.h +++ b/include/PowerLimiter.h @@ -75,6 +75,7 @@ class PowerLimiterClass { Mode _mode = Mode::Normal; std::shared_ptr _inverter = nullptr; bool _batteryDischargeEnabled = false; + bool _nighttimeDischarging = false; uint32_t _nextInverterRestart = 0; // Values: 0->not calculated / 1->no restart configured / >1->time of next inverter restart in millis() uint32_t _nextCalculateCheck = 5000; // time in millis for next NTP check to calulate restart bool _fullSolarPassThroughEnabled = false; diff --git a/src/PowerLimiter.cpp b/src/PowerLimiter.cpp index 39c9fd6d3..1c471a3dc 100644 --- a/src/PowerLimiter.cpp +++ b/src/PowerLimiter.cpp @@ -17,6 +17,7 @@ #include #include #include +#include "SunPosition.h" PowerLimiterClass PowerLimiter; @@ -239,19 +240,23 @@ void PowerLimiterClass::loop() auto getBatteryPower = [this,&config]() -> bool { if (config.PowerLimiter.IsInverterSolarPowered) { return false; } + auto isDayPeriod = SunPosition.isSunsetAvailable() ? SunPosition.isDayPeriod() : getSolarPower() > 0; + + if (_nighttimeDischarging && isDayPeriod) { + _nighttimeDischarging = false; + return isStartThresholdReached(); + } + if (isStopThresholdReached()) { return false; } if (isStartThresholdReached()) { return true; } - // with solar passthrough, and the respective switch enabled, we - // may start discharging the battery when it is nighttime. we also - // stop the discharge cycle if it becomes daytime again. - // TODO(schlimmchen): should be supported by sunrise and sunset, such - // that a thunderstorm or other events that drastically lower the solar - // power do not cause the start of a discharge cycle during the day. if (config.PowerLimiter.SolarPassThroughEnabled && - config.PowerLimiter.BatteryAlwaysUseAtNight) { - return getSolarPower() == 0; + config.PowerLimiter.BatteryAlwaysUseAtNight && + !isDayPeriod && + !_batteryDischargeEnabled) { + _nighttimeDischarging = true; + return true; } // we are between start and stop threshold and keep the state that was