From 2e5fe0ce63231d78c263061ec14b102d8d996ad4 Mon Sep 17 00:00:00 2001 From: Richard van den Berg Date: Mon, 1 Jan 2024 23:44:55 +0100 Subject: [PATCH] Only recalculate MaxAmpsToDivideFromGrid every 30 seconds --- lib/TWCManager/TWCMaster.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/TWCManager/TWCMaster.py b/lib/TWCManager/TWCMaster.py index 882b31b6..75e6f497 100644 --- a/lib/TWCManager/TWCMaster.py +++ b/lib/TWCManager/TWCMaster.py @@ -28,6 +28,7 @@ class TWCMaster: consumptionAmpsValues = {} debugOutputToFile = False generationValues = {} + lastMaxAmpsToDivideFromGrid = 0 lastkWhMessage = time.time() lastkWhPoll = 0 lastSaveFailed = 0 @@ -665,10 +666,13 @@ def getMaxAmpsToDivideGreenEnergy(self): return round(amps, 2) def getMaxAmpsToDivideFromGrid(self): - currentOffer = min( - self.getTotalAmpsInUse(), - self.getMaxAmpsToDivideAmongSlaves(), - ) if self.getTotalAmpsInUse() > 0 else self.getMaxAmpsToDivideAmongSlaves() + # Only recalculate once every 30 seconds to allow things to settle + now = time.time() + if now - self.lastMaxAmpsToDivideFromGrid < 30: + logger.debug(f"getMaxAmpsToDivideFromGrid returns cashed value {self.maxAmpsToDivideFromGrid}") + return self.maxAmpsToDivideFromGrid + + currentOffer = self.getTotalAmpsInUse() if self.getTotalAmpsInUse() > 0 else self.getMaxAmpsToDivideAmongSlaves() # Get consumptions in Amps, if the EMS source supports it consumptionA = float(self.getConsumptionAmps()) @@ -688,6 +692,9 @@ def getMaxAmpsToDivideFromGrid(self): amps = amps / self.getRealPowerFactor(amps) logger.debug("MaxAmpsToDivideFromGrid: +++++++++++++++: " + str(amps)) + # Update time for comparing next time + self.lastMaxAmpsToDivideFromGrid = now + return round(amps, 2)