From e7656a4a43b20258766129d686e0e452f1e0fc96 Mon Sep 17 00:00:00 2001 From: Jean-Marc Collin Date: Fri, 2 Feb 2024 20:56:23 +0000 Subject: [PATCH] Change reset cumulated_error formula --- .../versatile_thermostat/pi_algorithm.py | 11 ++++++++--- tests/test_auto_regulation.py | 8 +++----- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/custom_components/versatile_thermostat/pi_algorithm.py b/custom_components/versatile_thermostat/pi_algorithm.py index 89c1027..bd0767b 100644 --- a/custom_components/versatile_thermostat/pi_algorithm.py +++ b/custom_components/versatile_thermostat/pi_algorithm.py @@ -47,10 +47,10 @@ class PITemperatureRegulator: def set_target_temp(self, target_temp): """Set the new target_temp""" self.target_temp = target_temp - # Do not reset the accumulated error # Discussion #191. After a target change we should reset the accumulated error which is certainly wrong now. - if self.accumulated_error < 0: - self.accumulated_error = 0 + # Discussion #384. Finally don't reset the accumulated error but smoothly reset it if the sign is inversed + # if self.accumulated_error < 0: + # self.accumulated_error = 0 def calculate_regulated_temperature( self, room_temp: float, external_temp: float @@ -71,6 +71,11 @@ class PITemperatureRegulator: error = self.target_temp - room_temp # Calculate the sum of error (I) + # Discussion #384. Finally don't reset the accumulated error but smoothly reset it if the sign is inversed + # If the error have change its sign, reset smoothly the accumulated error + if error * self.accumulated_error < 0: + self.accumulated_error = self.accumulated_error / 2.0 + self.accumulated_error += error # Capping of the error diff --git a/tests/test_auto_regulation.py b/tests/test_auto_regulation.py index 375952a..0302f8b 100644 --- a/tests/test_auto_regulation.py +++ b/tests/test_auto_regulation.py @@ -127,9 +127,7 @@ async def test_over_climate_regulation( # the regulated temperature should be under assert entity.regulated_target_temp < entity.target_temperature - assert ( - entity.regulated_target_temp == 18 - 2 - ) # normally 0.6 but round_to_nearest gives 0.5 + assert entity.regulated_target_temp == 18 - 2.5 @pytest.mark.parametrize("expected_lingering_tasks", [True]) @@ -525,7 +523,7 @@ async def test_over_climate_regulation_use_device_temp( # the regulated temperature should be upper (device offset is +2) assert entity.regulated_target_temp < entity.target_temperature - assert entity.regulated_target_temp == 22.9 + assert entity.regulated_target_temp == 22.4 mock_service_call.assert_has_calls( [ @@ -534,7 +532,7 @@ async def test_over_climate_regulation_use_device_temp( "set_temperature", { "entity_id": "climate.mock_climate", - "temperature": 24.9, # 22.9 + 2° of offset + "temperature": 24.4, # 22.4 + 2° of offset "target_temp_high": 30, "target_temp_low": 15, },