Skip to content

Commit

Permalink
Fix: Improve DPL nighttime discharging (#1126)
Browse files Browse the repository at this point in the history
* fix: DPL: start discharging at night logic error

the switch "always start discharging battery at night" would cause to
stop discharging the battery when there was solar power and the battery
was discharged below the start threshold.

this change introduces a nighttime discharging boolean variable, which
is enabled the instant we decide to start a battery discharge cycle due
to nighttime havin arrived. we reset this variable as soon as it is
daytime (solar power available). in that case, we allow discharging the
battery if the start threshold was reached. this can actually be the
case if the battery is charged with cheap electricity during the night.

removed comments as they merely spell out what the if statement already
expresses quite nicely.

* use SunPosition.isDayPeriod() to check for daytime

---------

Co-authored-by: Andreas Böhm <[email protected]>
  • Loading branch information
schlimmchen and AndreasBoehm authored Aug 15, 2024
1 parent 599c5ce commit 5d1d071
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
1 change: 1 addition & 0 deletions include/PowerLimiter.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class PowerLimiterClass {
Mode _mode = Mode::Normal;
std::shared_ptr<InverterAbstract> _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;
Expand Down
21 changes: 13 additions & 8 deletions src/PowerLimiter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <ctime>
#include <cmath>
#include <frozen/map.h>
#include "SunPosition.h"

PowerLimiterClass PowerLimiter;

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5d1d071

Please sign in to comment.