hoylabs / OpenDTU-OnBattery

Software for ESP32 to talk to Hoymiles/TSUN/Solenso Inverters, VE.Direct devices, battery management systems, and related peripherals
GNU General Public License v2.0
301 stars 63 forks source link

Fix DPL nighttime discharging #1126

Closed schlimmchen closed 1 month ago

schlimmchen commented 2 months ago

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.

closes #1123.

schlimmchen commented 2 months ago

@greymda, are you able to test the firmware build as part of this PR's build run?

greymda commented 2 months ago

@schlimmchen is this firmware upgradeable via OTA on a 4MB board? if not, i don't have access physically to the setup and can't write it using a cable :( the board is still using the "old" partition schema...

schlimmchen commented 2 months ago

is this firmware upgradeable via OTA on a 4MB board?

No.

In the long run, I advise you to upgrade your hardware if you usually don't have physical access to it.

greymda commented 2 months ago

yep, I am planning to get the ESP32-WROVER-VIE as it has the same pin layout with the one i'm using now on Lukas's board. but it will take a while.

AndreasBoehm commented 2 months ago

@schlimmchen what do you think about adding this change to use day/night from 'SunPosition' instead of relying on solarPower to detect day/night?

diff --git a/src/PowerLimiter.cpp b/src/PowerLimiter.cpp
index 58e8713a..c2ad86f9 100644
--- a/src/PowerLimiter.cpp
+++ b/src/PowerLimiter.cpp
@@ -17,6 +17,7 @@
 #include <ctime>
 #include <cmath>
 #include <frozen/map.h>
+#include "SunPosition.h"

 PowerLimiterClass PowerLimiter;

@@ -239,7 +240,10 @@ void PowerLimiterClass::loop()
     auto getBatteryPower = [this,&config]() -> bool {
         if (config.PowerLimiter.IsInverterSolarPowered) { return false; }

-        if (_nighttimeDischarging && getSolarPower() > 0) {
+        auto isDayPeriod = (SunPosition.isSunsetAvailable() && SunPosition.isDayPeriod())
+            || getSolarPower() > 0;
+
+        if (_nighttimeDischarging && isDayPeriod) {
             _nighttimeDischarging = false;
             return isStartThresholdReached();
         }
@@ -248,12 +252,10 @@ void PowerLimiterClass::loop()

         if (isStartThresholdReached()) { return true; }

-        // 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 &&
-                getSolarPower() == 0 && !_batteryDischargeEnabled) {
+                !isDayPeriod &&
+                !_batteryDischargeEnabled) {
             _nighttimeDischarging = true;
             return true;
         }
schlimmchen commented 2 months ago

yes, please. this is what I had in mind. I was too lazy to do it until now. can you create a PR against this one?

AndreasBoehm commented 2 months ago

Here you go: https://github.com/helgeerbe/OpenDTU-OnBattery/pull/1158

schlimmchen commented 2 months ago

Awesome 🚀

AndreasBoehm commented 2 months ago

I realised that I made a mistake which would still take solarPower into consideration when its night. A new PR fixes this issue: https://github.com/helgeerbe/OpenDTU-OnBattery/pull/1160

AndreasBoehm commented 1 month ago

I finished my battery setup today and installed this PRs build. I will report back after 2 or 3 full days and nights if this now works as expected.

AndreasBoehm commented 1 month ago

I can confirm that this works as expected

github-actions[bot] commented 3 weeks ago

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new discussion or issue for related concerns.