From 3dfb7eb05206899068f6a7fcd1db530391fc5daa Mon Sep 17 00:00:00 2001 From: Mike Bishop Date: Sat, 24 Jul 2021 16:42:39 -0400 Subject: [PATCH 1/7] Move debounce logic into TWCSlave.py --- lib/TWCManager/TWCSlave.py | 63 ++++++++++++++++++++++++------ lib/TWCManager/Vehicle/TeslaAPI.py | 18 +-------- 2 files changed, 51 insertions(+), 30 deletions(-) diff --git a/lib/TWCManager/TWCSlave.py b/lib/TWCManager/TWCSlave.py index e7d5b162..0e38dc11 100644 --- a/lib/TWCManager/TWCSlave.py +++ b/lib/TWCManager/TWCSlave.py @@ -51,9 +51,12 @@ class TWCSlave: timeReportedAmpsActualChangedSignificantly = time.time() lastAmpsOffered = -1 + lastAmpsDesired = -1 useFlexAmpsToStartCharge = False timeLastAmpsOfferedChanged = 0 + timeLastAmpsOfferedFlipped = 0 lastHeartbeatDebugOutput = "" + startStopDelay = 0 timeLastHeartbeatDebugOutput = 0 wiringMaxAmps = 0 lifetimekWh = 0 @@ -77,6 +80,8 @@ def __init__(self, TWCID, maxAmps, config, master): self.useFlexAmpsToStartCharge = self.configConfig.get( "useFlexAmpsToStartCharge", False ) + self.startStopDelay = self.configConfig.get("startStopDelay", 60) + def print_status(self, heartbeatData): @@ -794,22 +799,30 @@ def receive_slave_heartbeat(self, heartbeatData): # stick with whole amps. desiredAmpsOffered = int(desiredAmpsOffered) - if self.lastAmpsOffered == 0 and now - self.timeLastAmpsOfferedChanged < 60: - # Keep charger off for at least 60 seconds before turning back - # on. See reasoning above where I don't turn the charger off + dampenChanges = True if now - self.timeLastAmpsOfferedFlipped < self.startStopDelay else False + + if (self.lastAmpsDesired == 0 and desiredAmpsOffered > 0) or (self.lastAmpsDesired > 0 and desiredAmpsOffered == 0): + self.lastAmpsDesired = desiredAmpsOffered + self.timeLastAmpsOfferedFlipped = now + logger.info("lastAmpsDesired flipped - now " + desiredAmpsOffered) + + if self.lastAmpsOffered > 0 and desiredAmpsOffered == 0 and dampenChanges: + # Keep charger on or off for at least startStopDelay before + # toggling. See reasoning above where I don't turn the charger off # till it's been on at least 60 seconds. - logger.debug( - "Don't start charging TWC: " + logger.info( + "Don't stop charging TWC " + self.master.hex_str(self.TWCID) - + " yet because: " - + "self.lastAmpsOffered " - + str(self.lastAmpsOffered) - + " == 0 " - + "and time - self.timeLastAmpsOfferedChanged " - + str(int(now - self.timeLastAmpsOfferedChanged)) - + " < 60" + + " yet." ) - desiredAmpsOffered = self.lastAmpsOffered + desiredAmpsOffered = self.minAmpsTWCSupports + elif self.lastAmpsOffered == 0 and desiredAmpsOffered > 0 and dampenChanges: + logger.info( + "Don't start charging TWC " + + self.master.hex_str(self.TWCID) + + " yet." + ) + desiredAmpsOffered = 0 else: # Mid Oct 2017, Tesla pushed a firmware update to their cars # that seems to create the following bug: @@ -938,6 +951,30 @@ def receive_slave_heartbeat(self, heartbeatData): if now - self.timeLastAmpsOfferedChanged < 5: desiredAmpsOffered = self.lastAmpsOffered + if (self.lastAmpsDesired <= 0 and desiredAmpsOffered > 0) or ( + self.lastAmpsDesired > 0 and desiredAmpsOffered == 0 + ): + self.lastAmpsDesired = desiredAmpsOffered + self.timeLastAmpsOfferedFlipped = now + logger.debug("lastAmpsDesired flipped - now " + str(desiredAmpsOffered)) + + # Keep charger on or off if dampening changes. See reasoning above where + # I don't turn the charger off till it's been on for a bit. + if self.reportedAmpsActual > 0 and desiredAmpsOffered == 0 and dampenChanges: + logger.debug( + "Don't stop TWC " + + self.master.hex_str(self.TWCID) + + " yet." + ) + desiredAmpsOffered = self.minAmpsTWCSupports + elif self.lastAmpsOffered == 0 and desiredAmpsOffered > 0 and dampenChanges: + logger.debug( + "Don't start charging TWC " + + self.master.hex_str(self.TWCID) + + " yet." + ) + desiredAmpsOffered = 0 + # set_last_amps_offered does some final checks to see if the new # desiredAmpsOffered is safe. It should be called after we've picked a # final value for desiredAmpsOffered. diff --git a/lib/TWCManager/Vehicle/TeslaAPI.py b/lib/TWCManager/Vehicle/TeslaAPI.py index 7037a481..d55d48aa 100644 --- a/lib/TWCManager/Vehicle/TeslaAPI.py +++ b/lib/TWCManager/Vehicle/TeslaAPI.py @@ -24,15 +24,12 @@ class TeslaAPI: carApiRefreshToken = "" carApiTokenExpireTime = time.time() carApiLastStartOrStopChargeTime = 0 - carApiLastStartOrStopChargeAction = None - carApiLastStartOrStopFlipTime = 0 carApiLastChargeLimitApplyTime = 0 clientID = "81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384" clientSecret = "c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3" lastChargeLimitApplied = 0 lastChargeCheck = 0 chargeUpdateInterval = 1800 - startStopDelay = 60 carApiVehicles = [] config = None master = None @@ -70,7 +67,6 @@ def __init__(self, master): try: self.config = master.config self.minChargeLevel = self.config["config"].get("minChargeLevel", -1) - self.startStopDelay = self.config["config"].get("startStopDelay", 60) self.chargeUpdateInterval = self.config["config"].get( "cloudUpdateInterval", 1800 ) @@ -708,15 +704,7 @@ def car_api_charge(self, charge): for vehicle in self.getCarApiVehicles(): vehicle.stopAskingToStartCharging = False - if (now - self.getLastStartOrStopChargeTime() < 60) or ( - now - self.carApiLastStartOrStopFlipTime < self.startStopDelay - and charge != self.carApiLastStartOrStopChargeAction - ): - if self.carApiLastStartOrStopChargeAction != charge: - # If we're repeatedly changing our minds about whether to charge or not, - # stay how we are until the system settles down. - self.carApiLastStartOrStopChargeAction = charge - self.carApiLastStartOrStopFlipTime = now + if (now - self.getLastStartOrStopChargeTime() < 60): # Don't start or stop more often than once a minute logger.log( @@ -763,10 +751,6 @@ def car_api_charge(self, charge): # more than once per minute. self.updateLastStartOrStopChargeTime() - if self.carApiLastStartOrStopChargeAction != charge: - self.carApiLastStartOrStopChargeAction = charge - self.carApiLastStartOrStopFlipTime = now - if ( self.config["config"]["onlyChargeMultiCarsAtHome"] and self.getVehicleCount() > 1 From cad2dedf04dedff208f803d91cbe3ca495b3f5a4 Mon Sep 17 00:00:00 2001 From: Mike Bishop Date: Mon, 26 Jul 2021 15:47:58 -0400 Subject: [PATCH 2/7] Fix initial value --- lib/TWCManager/TWCSlave.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/TWCManager/TWCSlave.py b/lib/TWCManager/TWCSlave.py index 0e38dc11..ee6cd176 100644 --- a/lib/TWCManager/TWCSlave.py +++ b/lib/TWCManager/TWCSlave.py @@ -82,7 +82,6 @@ def __init__(self, TWCID, maxAmps, config, master): ) self.startStopDelay = self.configConfig.get("startStopDelay", 60) - def print_status(self, heartbeatData): try: @@ -799,12 +798,18 @@ def receive_slave_heartbeat(self, heartbeatData): # stick with whole amps. desiredAmpsOffered = int(desiredAmpsOffered) - dampenChanges = True if now - self.timeLastAmpsOfferedFlipped < self.startStopDelay else False + dampenChanges = ( + True + if now - self.timeLastAmpsOfferedFlipped < self.startStopDelay + else False + ) - if (self.lastAmpsDesired == 0 and desiredAmpsOffered > 0) or (self.lastAmpsDesired > 0 and desiredAmpsOffered == 0): + if (self.lastAmpsDesired <= 0 and desiredAmpsOffered > 0) or ( + self.lastAmpsDesired > 0 and desiredAmpsOffered == 0 + ): self.lastAmpsDesired = desiredAmpsOffered self.timeLastAmpsOfferedFlipped = now - logger.info("lastAmpsDesired flipped - now " + desiredAmpsOffered) + logger.info("lastAmpsDesired flipped - now " + str(desiredAmpsOffered)) if self.lastAmpsOffered > 0 and desiredAmpsOffered == 0 and dampenChanges: # Keep charger on or off for at least startStopDelay before From 75076ba68c4821943b481ccfc89cb14a207b5770 Mon Sep 17 00:00:00 2001 From: Mike Bishop Date: Wed, 28 Jul 2021 15:47:26 -0400 Subject: [PATCH 3/7] Move logic outside if/else branch --- lib/TWCManager/TWCSlave.py | 299 ++++++++++++++++++------------------- 1 file changed, 149 insertions(+), 150 deletions(-) diff --git a/lib/TWCManager/TWCSlave.py b/lib/TWCManager/TWCSlave.py index ee6cd176..7b944139 100644 --- a/lib/TWCManager/TWCSlave.py +++ b/lib/TWCManager/TWCSlave.py @@ -698,6 +698,12 @@ def receive_slave_heartbeat(self, heartbeatData): ): desiredAmpsOffered = minAmpsToOffer + dampenChanges = ( + True + if now - self.timeLastAmpsOfferedFlipped < self.startStopDelay + else False + ) + if desiredAmpsOffered < minAmpsToOffer: logger.debug( "desiredAmpsOffered: " @@ -798,163 +804,156 @@ def receive_slave_heartbeat(self, heartbeatData): # stick with whole amps. desiredAmpsOffered = int(desiredAmpsOffered) - dampenChanges = ( - True - if now - self.timeLastAmpsOfferedFlipped < self.startStopDelay - else False + # Mid Oct 2017, Tesla pushed a firmware update to their cars + # that seems to create the following bug: + # If you raise desiredAmpsOffered AT ALL from the car's current + # max amp limit, the car will drop its max amp limit to the 6A + # setting (5.14-5.23A actual use as reported in + # heartbeatData[2-3]). The odd fix to this problem is to tell + # the car to raise to at least spikeAmpsToCancel6ALimit for 5 or + # more seconds, then tell it to lower the limit to + # desiredAmpsOffered. Even 0.01A less than + # spikeAmpsToCancel6ALimit is not enough to cancel the 6A limit. + # + # I'm not sure how long we have to hold spikeAmpsToCancel6ALimit + # but 3 seconds is definitely not enough but 5 seconds seems to + # work. It doesn't seem to matter if the car actually hits + # spikeAmpsToCancel6ALimit of power draw. In fact, the car is + # slow enough to respond that even with 10s at 21A the most I've + # seen it actually draw starting at 6A is 13A. + logger.debug( + "TWCID=" + + self.master.hex_str(self.TWCID) + + " desiredAmpsOffered=" + + str(desiredAmpsOffered) + + " spikeAmpsToCancel6ALimit=" + + str(self.master.getSpikeAmps()) + + " self.lastAmpsOffered=" + + str(self.lastAmpsOffered) + + " self.reportedAmpsActual=" + + str(self.reportedAmpsActual) + + " now - self.timeReportedAmpsActualChangedSignificantly=" + + str(int(now - self.timeReportedAmpsActualChangedSignificantly)) ) - if (self.lastAmpsDesired <= 0 and desiredAmpsOffered > 0) or ( - self.lastAmpsDesired > 0 and desiredAmpsOffered == 0 - ): - self.lastAmpsDesired = desiredAmpsOffered - self.timeLastAmpsOfferedFlipped = now - logger.info("lastAmpsDesired flipped - now " + str(desiredAmpsOffered)) - - if self.lastAmpsOffered > 0 and desiredAmpsOffered == 0 and dampenChanges: - # Keep charger on or off for at least startStopDelay before - # toggling. See reasoning above where I don't turn the charger off - # till it's been on at least 60 seconds. - logger.info( - "Don't stop charging TWC " - + self.master.hex_str(self.TWCID) - + " yet." - ) - desiredAmpsOffered = self.minAmpsTWCSupports - elif self.lastAmpsOffered == 0 and desiredAmpsOffered > 0 and dampenChanges: - logger.info( - "Don't start charging TWC " - + self.master.hex_str(self.TWCID) - + " yet." + if ( + # If we just moved from a lower amp limit to + # a higher one less than spikeAmpsToCancel6ALimit. + ( + desiredAmpsOffered < self.master.getSpikeAmps() + and desiredAmpsOffered > self.reportedAmpsMax + and self.master.settings.get("spikeAmpsProactively", 1) ) - desiredAmpsOffered = 0 - else: - # Mid Oct 2017, Tesla pushed a firmware update to their cars - # that seems to create the following bug: - # If you raise desiredAmpsOffered AT ALL from the car's current - # max amp limit, the car will drop its max amp limit to the 6A - # setting (5.14-5.23A actual use as reported in - # heartbeatData[2-3]). The odd fix to this problem is to tell - # the car to raise to at least spikeAmpsToCancel6ALimit for 5 or - # more seconds, then tell it to lower the limit to - # desiredAmpsOffered. Even 0.01A less than - # spikeAmpsToCancel6ALimit is not enough to cancel the 6A limit. - # - # I'm not sure how long we have to hold spikeAmpsToCancel6ALimit - # but 3 seconds is definitely not enough but 5 seconds seems to - # work. It doesn't seem to matter if the car actually hits - # spikeAmpsToCancel6ALimit of power draw. In fact, the car is - # slow enough to respond that even with 10s at 21A the most I've - # seen it actually draw starting at 6A is 13A. - logger.debug( - "TWCID=" - + self.master.hex_str(self.TWCID) - + " desiredAmpsOffered=" - + str(desiredAmpsOffered) - + " spikeAmpsToCancel6ALimit=" - + str(self.master.getSpikeAmps()) - + " self.lastAmpsOffered=" - + str(self.lastAmpsOffered) - + " self.reportedAmpsActual=" - + str(self.reportedAmpsActual) - + " now - self.timeReportedAmpsActualChangedSignificantly=" - + str(int(now - self.timeReportedAmpsActualChangedSignificantly)) + or ( + # ...or if we've been offering the car more amps than it's + # been using for at least 10 seconds, then we'll change the + # amps we're offering it. For some reason, the change in + # amps offered will get the car to up its amp draw. + # + # First, check that the car is drawing enough amps to be + # charging... + self.reportedAmpsActual > 2.0 + and + # ...and car is charging at under spikeAmpsToCancel6ALimit. + # I think I've seen cars get stuck between spikeAmpsToCancel6ALimit + # and lastAmpsOffered, but more often a car will be limited + # to under lastAmpsOffered by its UI setting or by the + # charger hardware it has on board, and we don't want to + # keep reducing it to spikeAmpsToCancel6ALimit. + # If cars really are getting stuck above + # spikeAmpsToCancel6ALimit, I may need to implement a + # counter that tries spikeAmpsToCancel6ALimit only a + # certain number of times per hour. + (self.reportedAmpsActual <= self.master.getSpikeAmps()) + and + # ...and car is charging at over two amps under what we + # want it to charge at. I have to use 2 amps because when + # offered, say 40A, the car charges at ~38.76A actual. + # Using a percentage instead of 2.0A doesn't work because + # 38.58/40 = 95.4% but 5.14/6 = 85.6% + (self.lastAmpsOffered - self.reportedAmpsActual) > 2.0 + and + # ...and car hasn't changed its amp draw significantly in + # over 10 seconds, meaning it's stuck at its current amp + # draw. + now - self.timeReportedAmpsActualChangedSignificantly > 10 + and self.master.settings.get("spikeAmpsReactively", 1) ) - + ): + # We must set desiredAmpsOffered to a value that gets + # reportedAmpsActual (amps the car is actually using) up to + # a value near lastAmpsOffered. At the end of all these + # checks, we'll set lastAmpsOffered = desiredAmpsOffered and + # timeLastAmpsOfferedChanged if the value of lastAmpsOffered was + # actually changed. if ( - # If we just moved from a lower amp limit to - # a higher one less than spikeAmpsToCancel6ALimit. - ( - desiredAmpsOffered < self.master.getSpikeAmps() - and desiredAmpsOffered > self.reportedAmpsMax - and self.master.settings.get("spikeAmpsProactively", 1) - ) - or ( - # ...or if we've been offering the car more amps than it's - # been using for at least 10 seconds, then we'll change the - # amps we're offering it. For some reason, the change in - # amps offered will get the car to up its amp draw. - # - # First, check that the car is drawing enough amps to be - # charging... - self.reportedAmpsActual > 2.0 - and - # ...and car is charging at under spikeAmpsToCancel6ALimit. - # I think I've seen cars get stuck between spikeAmpsToCancel6ALimit - # and lastAmpsOffered, but more often a car will be limited - # to under lastAmpsOffered by its UI setting or by the - # charger hardware it has on board, and we don't want to - # keep reducing it to spikeAmpsToCancel6ALimit. - # If cars really are getting stuck above - # spikeAmpsToCancel6ALimit, I may need to implement a - # counter that tries spikeAmpsToCancel6ALimit only a - # certain number of times per hour. - (self.reportedAmpsActual <= self.master.getSpikeAmps()) - and - # ...and car is charging at over two amps under what we - # want it to charge at. I have to use 2 amps because when - # offered, say 40A, the car charges at ~38.76A actual. - # Using a percentage instead of 2.0A doesn't work because - # 38.58/40 = 95.4% but 5.14/6 = 85.6% - (self.lastAmpsOffered - self.reportedAmpsActual) > 2.0 - and - # ...and car hasn't changed its amp draw significantly in - # over 10 seconds, meaning it's stuck at its current amp - # draw. - now - self.timeReportedAmpsActualChangedSignificantly > 10 - and self.master.settings.get("spikeAmpsReactively", 1) - ) + self.lastAmpsOffered == self.master.getSpikeAmps() + and now - self.timeLastAmpsOfferedChanged > 10 ): - # We must set desiredAmpsOffered to a value that gets - # reportedAmpsActual (amps the car is actually using) up to - # a value near lastAmpsOffered. At the end of all these - # checks, we'll set lastAmpsOffered = desiredAmpsOffered and - # timeLastAmpsOfferedChanged if the value of lastAmpsOffered was - # actually changed. - if ( - self.lastAmpsOffered == self.master.getSpikeAmps() - and now - self.timeLastAmpsOfferedChanged > 10 - ): - # We've been offering the car spikeAmpsToCancel6ALimit - # for over 10 seconds but it's still drawing at least - # 2A less than spikeAmpsToCancel6ALimit. I saw this - # happen once when an error stopped the car from - # charging and when the error cleared, it was offered - # spikeAmpsToCancel6ALimit as the first value it saw. - # The car limited itself to 6A indefinitely. In this - # case, the fix is to offer it lower amps. - logger.info( - "Car stuck when offered spikeAmpsToCancel6ALimit. Offering 2 less." - ) - desiredAmpsOffered = self.master.getSpikeAmps() - 2.0 - elif now - self.timeLastAmpsOfferedChanged > 5: - # self.lastAmpsOffered hasn't gotten the car to draw - # enough amps for over 5 seconds, so try - # spikeAmpsToCancel6ALimit - desiredAmpsOffered = self.master.getSpikeAmps() - else: - # Otherwise, don't change the value of lastAmpsOffered. - desiredAmpsOffered = self.lastAmpsOffered - - # Note that the car should have no problem increasing max - # amps to any whole value over spikeAmpsToCancel6ALimit as - # long as it's below any upper limit manually set in the - # car's UI. One time when I couldn't get TWC to push the car - # over 21A, I found the car's UI had set itself to 21A - # despite setting it to 40A the day before. I have been - # unable to reproduce whatever caused that problem. - elif desiredAmpsOffered < self.lastAmpsOffered: - # Tesla doesn't mind if we set a lower amp limit than the - # one we're currently using, but make sure we don't change - # limits more often than every 5 seconds. This has the side - # effect of holding spikeAmpsToCancel6ALimit set earlier for - # 5 seconds to make sure the car sees it. - logger.debug( - "Reduce amps: time - self.timeLastAmpsOfferedChanged " - + str(int(now - self.timeLastAmpsOfferedChanged)) + # We've been offering the car spikeAmpsToCancel6ALimit + # for over 10 seconds but it's still drawing at least + # 2A less than spikeAmpsToCancel6ALimit. I saw this + # happen once when an error stopped the car from + # charging and when the error cleared, it was offered + # spikeAmpsToCancel6ALimit as the first value it saw. + # The car limited itself to 6A indefinitely. In this + # case, the fix is to offer it lower amps. + logger.info( + "Car stuck when offered spikeAmpsToCancel6ALimit. Offering 2 less." ) - if now - self.timeLastAmpsOfferedChanged < 5: - desiredAmpsOffered = self.lastAmpsOffered + desiredAmpsOffered = self.master.getSpikeAmps() - 2.0 + elif now - self.timeLastAmpsOfferedChanged > 5: + # self.lastAmpsOffered hasn't gotten the car to draw + # enough amps for over 5 seconds, so try + # spikeAmpsToCancel6ALimit + desiredAmpsOffered = self.master.getSpikeAmps() + else: + # Otherwise, don't change the value of lastAmpsOffered. + desiredAmpsOffered = self.lastAmpsOffered + + # Note that the car should have no problem increasing max + # amps to any whole value over spikeAmpsToCancel6ALimit as + # long as it's below any upper limit manually set in the + # car's UI. One time when I couldn't get TWC to push the car + # over 21A, I found the car's UI had set itself to 21A + # despite setting it to 40A the day before. I have been + # unable to reproduce whatever caused that problem. + elif desiredAmpsOffered < self.lastAmpsOffered: + # Tesla doesn't mind if we set a lower amp limit than the + # one we're currently using, but make sure we don't change + # limits more often than every 5 seconds. This has the side + # effect of holding spikeAmpsToCancel6ALimit set earlier for + # 5 seconds to make sure the car sees it. + logger.debug( + "Reduce amps: time - self.timeLastAmpsOfferedChanged " + + str(int(now - self.timeLastAmpsOfferedChanged)) + ) + if now - self.timeLastAmpsOfferedChanged < 5: + desiredAmpsOffered = self.lastAmpsOffered + + if (self.lastAmpsDesired <= 0 and desiredAmpsOffered > 0) or ( + self.lastAmpsDesired > 0 and desiredAmpsOffered == 0 + ): + self.lastAmpsDesired = desiredAmpsOffered + self.timeLastAmpsOfferedFlipped = now + logger.info("lastAmpsDesired flipped - now " + str(desiredAmpsOffered)) + + # Keep charger on or off if dampening changes. See reasoning above where + # I don't turn the charger off till it's been on for a bit. + if self.lastAmpsOffered > 0 and desiredAmpsOffered == 0 and dampenChanges: + logger.info( + "Don't stop charging TWC " + + self.master.hex_str(self.TWCID) + + " yet." + ) + desiredAmpsOffered = self.minAmpsTWCSupports + elif self.lastAmpsOffered == 0 and desiredAmpsOffered > 0 and dampenChanges: + logger.info( + "Don't start charging TWC " + + self.master.hex_str(self.TWCID) + + " yet." + ) + desiredAmpsOffered = 0 if (self.lastAmpsDesired <= 0 and desiredAmpsOffered > 0) or ( self.lastAmpsDesired > 0 and desiredAmpsOffered == 0 From 0a56f4198784e832dfbd9c3e4d06b518ad16e36e Mon Sep 17 00:00:00 2001 From: Mike Bishop Date: Thu, 29 Jul 2021 16:02:27 -0400 Subject: [PATCH 4/7] Only if actually drawing power --- lib/TWCManager/TWCSlave.py | 22 +++++----------------- lib/TWCManager/Vehicle/TeslaAPI.py | 2 +- 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/lib/TWCManager/TWCSlave.py b/lib/TWCManager/TWCSlave.py index 7b944139..0dfc31c6 100644 --- a/lib/TWCManager/TWCSlave.py +++ b/lib/TWCManager/TWCSlave.py @@ -940,18 +940,12 @@ def receive_slave_heartbeat(self, heartbeatData): # Keep charger on or off if dampening changes. See reasoning above where # I don't turn the charger off till it's been on for a bit. - if self.lastAmpsOffered > 0 and desiredAmpsOffered == 0 and dampenChanges: - logger.info( - "Don't stop charging TWC " - + self.master.hex_str(self.TWCID) - + " yet." - ) + if self.reportedAmpsActual > 0 and desiredAmpsOffered == 0 and dampenChanges: + logger.info("Don't stop TWC " + self.master.hex_str(self.TWCID) + " yet.") desiredAmpsOffered = self.minAmpsTWCSupports elif self.lastAmpsOffered == 0 and desiredAmpsOffered > 0 and dampenChanges: logger.info( - "Don't start charging TWC " - + self.master.hex_str(self.TWCID) - + " yet." + "Don't start charging TWC " + self.master.hex_str(self.TWCID) + " yet." ) desiredAmpsOffered = 0 @@ -965,17 +959,11 @@ def receive_slave_heartbeat(self, heartbeatData): # Keep charger on or off if dampening changes. See reasoning above where # I don't turn the charger off till it's been on for a bit. if self.reportedAmpsActual > 0 and desiredAmpsOffered == 0 and dampenChanges: - logger.debug( - "Don't stop TWC " - + self.master.hex_str(self.TWCID) - + " yet." - ) + logger.debug("Don't stop TWC " + self.master.hex_str(self.TWCID) + " yet.") desiredAmpsOffered = self.minAmpsTWCSupports elif self.lastAmpsOffered == 0 and desiredAmpsOffered > 0 and dampenChanges: logger.debug( - "Don't start charging TWC " - + self.master.hex_str(self.TWCID) - + " yet." + "Don't start charging TWC " + self.master.hex_str(self.TWCID) + " yet." ) desiredAmpsOffered = 0 diff --git a/lib/TWCManager/Vehicle/TeslaAPI.py b/lib/TWCManager/Vehicle/TeslaAPI.py index d55d48aa..20e1f493 100644 --- a/lib/TWCManager/Vehicle/TeslaAPI.py +++ b/lib/TWCManager/Vehicle/TeslaAPI.py @@ -704,7 +704,7 @@ def car_api_charge(self, charge): for vehicle in self.getCarApiVehicles(): vehicle.stopAskingToStartCharging = False - if (now - self.getLastStartOrStopChargeTime() < 60): + if now - self.getLastStartOrStopChargeTime() < 60: # Don't start or stop more often than once a minute logger.log( From fde7c84c68ad1a445bc79200005336b1fb7cfd07 Mon Sep 17 00:00:00 2001 From: Mike Bishop Date: Mon, 2 Aug 2021 15:44:27 -0400 Subject: [PATCH 5/7] Rename variable --- lib/TWCManager/TWCSlave.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/TWCManager/TWCSlave.py b/lib/TWCManager/TWCSlave.py index 0dfc31c6..602e961b 100644 --- a/lib/TWCManager/TWCSlave.py +++ b/lib/TWCManager/TWCSlave.py @@ -54,7 +54,7 @@ class TWCSlave: lastAmpsDesired = -1 useFlexAmpsToStartCharge = False timeLastAmpsOfferedChanged = 0 - timeLastAmpsOfferedFlipped = 0 + timeLastAmpsDesiredFlipped = 0 lastHeartbeatDebugOutput = "" startStopDelay = 0 timeLastHeartbeatDebugOutput = 0 @@ -700,7 +700,7 @@ def receive_slave_heartbeat(self, heartbeatData): dampenChanges = ( True - if now - self.timeLastAmpsOfferedFlipped < self.startStopDelay + if now - self.timeLastAmpsDesiredFlipped < self.startStopDelay else False ) @@ -935,7 +935,7 @@ def receive_slave_heartbeat(self, heartbeatData): self.lastAmpsDesired > 0 and desiredAmpsOffered == 0 ): self.lastAmpsDesired = desiredAmpsOffered - self.timeLastAmpsOfferedFlipped = now + self.timeLastAmpsDesiredFlipped = now logger.info("lastAmpsDesired flipped - now " + str(desiredAmpsOffered)) # Keep charger on or off if dampening changes. See reasoning above where @@ -953,7 +953,7 @@ def receive_slave_heartbeat(self, heartbeatData): self.lastAmpsDesired > 0 and desiredAmpsOffered == 0 ): self.lastAmpsDesired = desiredAmpsOffered - self.timeLastAmpsOfferedFlipped = now + self.timeLastAmpsDesiredFlipped = now logger.debug("lastAmpsDesired flipped - now " + str(desiredAmpsOffered)) # Keep charger on or off if dampening changes. See reasoning above where From 1115cc42999350cb74bae3ce67436fdc5ec6ff84 Mon Sep 17 00:00:00 2001 From: Mike Bishop Date: Mon, 2 Aug 2021 15:55:51 -0400 Subject: [PATCH 6/7] Remove rebase duplication --- lib/TWCManager/TWCSlave.py | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/lib/TWCManager/TWCSlave.py b/lib/TWCManager/TWCSlave.py index 602e961b..a901962c 100644 --- a/lib/TWCManager/TWCSlave.py +++ b/lib/TWCManager/TWCSlave.py @@ -938,24 +938,6 @@ def receive_slave_heartbeat(self, heartbeatData): self.timeLastAmpsDesiredFlipped = now logger.info("lastAmpsDesired flipped - now " + str(desiredAmpsOffered)) - # Keep charger on or off if dampening changes. See reasoning above where - # I don't turn the charger off till it's been on for a bit. - if self.reportedAmpsActual > 0 and desiredAmpsOffered == 0 and dampenChanges: - logger.info("Don't stop TWC " + self.master.hex_str(self.TWCID) + " yet.") - desiredAmpsOffered = self.minAmpsTWCSupports - elif self.lastAmpsOffered == 0 and desiredAmpsOffered > 0 and dampenChanges: - logger.info( - "Don't start charging TWC " + self.master.hex_str(self.TWCID) + " yet." - ) - desiredAmpsOffered = 0 - - if (self.lastAmpsDesired <= 0 and desiredAmpsOffered > 0) or ( - self.lastAmpsDesired > 0 and desiredAmpsOffered == 0 - ): - self.lastAmpsDesired = desiredAmpsOffered - self.timeLastAmpsDesiredFlipped = now - logger.debug("lastAmpsDesired flipped - now " + str(desiredAmpsOffered)) - # Keep charger on or off if dampening changes. See reasoning above where # I don't turn the charger off till it's been on for a bit. if self.reportedAmpsActual > 0 and desiredAmpsOffered == 0 and dampenChanges: From e6c2ad924c98ddd3b46f24765c64cc72704a7426 Mon Sep 17 00:00:00 2001 From: Mike Bishop Date: Mon, 2 Aug 2021 16:07:37 -0400 Subject: [PATCH 7/7] Keep timeLastAmpsOfferedChanged check --- lib/TWCManager/TWCSlave.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/TWCManager/TWCSlave.py b/lib/TWCManager/TWCSlave.py index a901962c..7f4827ac 100644 --- a/lib/TWCManager/TWCSlave.py +++ b/lib/TWCManager/TWCSlave.py @@ -700,7 +700,9 @@ def receive_slave_heartbeat(self, heartbeatData): dampenChanges = ( True - if now - self.timeLastAmpsDesiredFlipped < self.startStopDelay + if now + - min(self.timeLastAmpsDesiredFlipped, self.timeLastAmpsOfferedChanged) + < self.startStopDelay else False ) @@ -936,7 +938,7 @@ def receive_slave_heartbeat(self, heartbeatData): ): self.lastAmpsDesired = desiredAmpsOffered self.timeLastAmpsDesiredFlipped = now - logger.info("lastAmpsDesired flipped - now " + str(desiredAmpsOffered)) + logger.debug("lastAmpsDesired flipped - now " + str(desiredAmpsOffered)) # Keep charger on or off if dampening changes. See reasoning above where # I don't turn the charger off till it's been on for a bit.