Skip to content

Commit 9029cae

Browse files
committed
Fixed some cap issues
1 parent 928e917 commit 9029cae

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

custom_components/sat/pid.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
DERIVATIVE_ALPHA1 = 0.2
2020
DERIVATIVE_ALPHA2 = 0.1
2121
DERIVATIVE_RAW_CAP = 5.0
22+
DEADBAND_EPSILON = 1e-6
2223

2324
PID_UPDATE_INTERVAL = 60
2425

@@ -183,7 +184,7 @@ def update(self, state: TemperatureState) -> None:
183184

184185
def _update_integral(self, state: TemperatureState) -> None:
185186
"""Update the integral value in the PID controller."""
186-
if abs(state.error) > DEADBAND:
187+
if abs(state.error) > (DEADBAND + DEADBAND_EPSILON):
187188
self._integral = 0.0
188189
return
189190

@@ -219,6 +220,11 @@ def _update_derivative(self, state: TemperatureState) -> None:
219220

220221
derivative = -temperature_delta / delta_time
221222

223+
if abs(derivative) >= DERIVATIVE_RAW_CAP:
224+
self._raw_derivative = max(-DERIVATIVE_RAW_CAP, min(derivative, DERIVATIVE_RAW_CAP))
225+
self._last_derivative_updated = state.last_changed.timestamp()
226+
return
227+
222228
# Apply the first low-pass filter.
223229
filtered_derivative = DERIVATIVE_ALPHA1 * derivative + (1 - DERIVATIVE_ALPHA1) * self._raw_derivative
224230

0 commit comments

Comments
 (0)