Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
magnuselden authored and magnuselden committed Sep 16, 2024
1 parent 0551b4e commit e8358d0
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,10 @@ async def async_calculated_offsetdata(self, current_offset: int) -> CalculatedOf
self._current_tolerances
)
temptrend = get_temp_trend_offset(
self.hub.sensors.temp_trend_indoors.is_clean,
self.hub.sensors.predicted_temp,
self.hub.sensors.set_temp_indoors.adjusted_temp
do_calc=self.hub.sensors.temp_trend_indoors.is_clean,
temp_diff_offset=tempdiff,
predicted_temp=self.hub.sensors.predicted_temp,
adjusted_temp=self.hub.sensors.set_temp_indoors.adjusted_temp
)

return CalculatedOffsetModel(current_offset=current_offset,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,38 @@ def get_tempdiff_inverted(current_offset: int, temp_diff: float, determine_toler
return ret * -1


def get_temp_trend_offset(temp_trend_is_clean: bool, predicted_temp: float, adjusted_temp: float) -> float:
if not temp_trend_is_clean:
return 0
def get_temp_trend_offset(do_calc: bool, temp_diff_offset: float, predicted_temp: float, adjusted_temp: float) -> float:
ret = 0
if not do_calc:
return ret

new_temp_diff = round(predicted_temp - adjusted_temp, 3)

if abs(new_temp_diff) <= 0.1:
return 0
return ret

# Protect against overcompensating
if temp_diff_offset > 0:
if predicted_temp < adjusted_temp:
return ret
elif temp_diff_offset < 0:
if predicted_temp > adjusted_temp:
return ret

trend_factor = 1
if abs(temp_diff_offset) > 1:
trend_factor = min(1 + (abs(temp_diff_offset) - 1) * 0.3, 3.0)

if predicted_temp >= adjusted_temp:
ret = max(round(new_temp_diff, 1), 0)
ret = max(round(new_temp_diff * trend_factor, 1), 0)
print(f"Positive Adjustment: {ret} (Trend Factor: {trend_factor})")
else:
ret = min(round(new_temp_diff, 1), 0)
return min((round(ret / 2, 1)), 1) * (-1 if ret > 0 else 1)
ret = min(round(new_temp_diff * trend_factor, 1), 0)
print(f"Negative Adjustment: {ret} (Trend Factor: {trend_factor})")

# Apply a softer correction when the offset is already significant to avoid overcompensation
ret = round(ret/2,1)
ret = min(ret, 1.0)
return ret * -1


def _temp_extremas_return(diffs, tolerance) -> float:
avg_diff = max(diffs[:-1])
dev = 1 if avg_diff >= 0 else -1
ret = (abs(avg_diff) - tolerance) * dev
ret = max(ret, 0) if dev == 1 else min(ret, 0)
return round(ret, 2)
44 changes: 42 additions & 2 deletions custom_components/peaqhvac/test/test_temperature_helpers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from ..service.hub.target_temp import adjusted_tolerances
from ..service.hvac.house_heater.temperature_helper import get_tempdiff_inverted
from ..service.hvac.house_heater.temperature_helper import get_tempdiff_inverted, get_temp_trend_offset

MINTOLERANCE = 0.2
MAXTOLERANCE = 0.5
Expand All @@ -25,6 +25,7 @@ def test_tempdiff_cold():
ret4 = get_tempdiff_inverted(-3, tempdiff, _current_tolerances)
assert ret4 == 2


def test_tempdiff_hot():
tempdiff = 0.5
ret1 = get_tempdiff_inverted(0, tempdiff, _current_tolerances)
Expand All @@ -34,4 +35,43 @@ def test_tempdiff_hot():
ret3 = get_tempdiff_inverted(-2, tempdiff, _current_tolerances)
assert ret3 == -1
ret4 = get_tempdiff_inverted(-3, tempdiff, _current_tolerances)
assert ret4 == -1
assert ret4 == -1


def test_temp_trend_offset_expected_colder_than_desired_with_diff_offset():
for i in range(1, 10):
ret = get_temp_trend_offset(True, i, 19.8, 20)
assert ret == 0


def test_temp_trend_offset_expected_colder_than_desired_with_diff_neg_offset():
for i in range(1, 10):
ret = get_temp_trend_offset(True, i*-1, 19.8, 20)
assert ret != 0


def test_temp_trend_offset_expected_warmer_than_desired_with_diff_offset():
for i in range(1, 10):
ret = get_temp_trend_offset(True, i, 20.8, 20)
assert ret != 0


def test_temp_trend_offset_expected_warmer_than_desired_with_diff_neg_offset():
for i in range(1, 10):
ret = get_temp_trend_offset(True, i*-1, 20.8, 20)
assert ret == 0


def test_too_cold_only_pos_trend():
for i in range(1, 10):
icheck = i*-1
ret = get_temp_trend_offset(True, icheck, 19.5, 20)
assert ret > 0 if icheck < 0 else ret < 0


def test_assert_too_hot_only_neg_trend():
for i in range(1, 10):
icheck = i
ret = get_temp_trend_offset(True, icheck, 20.5, 20)
assert ret > 0 if icheck < 0 else ret < 0

0 comments on commit e8358d0

Please sign in to comment.