Skip to content

Commit

Permalink
Ignore initial sensor reading on flow rate calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
rgc99 committed May 12, 2024
1 parent 9691dc9 commit 7ff9b51
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions custom_components/irrigation_unlimited/irrigation_unlimited.py
Original file line number Diff line number Diff line change
Expand Up @@ -1151,15 +1151,16 @@ def read_sensor(self, stime: datetime) -> Decimal:
f"current: {volume}"
)

# Update moving average of the rate
rate = volume_delta * Decimal(self._flow_rate_scale) / time_delta
self._flow_rate_sum += rate
self._flow_rates.append(rate)
if len(self._flow_rates) > IUVolume.SMA_WINDOW:
self._flow_rate_sum -= self._flow_rates.pop(0)
self._flow_rate_sma = (
self._flow_rate_sum / len(self._flow_rates)
).quantize(Decimal(10) ** -self._flow_rate_precision)
# Update moving average of the rate. Ignore initial reading.
if len(self._sensor_readings) > 1:
rate = volume_delta * Decimal(self._flow_rate_scale) / time_delta
self._flow_rate_sum += rate
self._flow_rates.append(rate)
if len(self._flow_rates) > IUVolume.SMA_WINDOW:
self._flow_rate_sum -= self._flow_rates.pop(0)
self._flow_rate_sma = (
self._flow_rate_sum / len(self._flow_rates)
).quantize(Decimal(10) ** -self._flow_rate_precision)

# Update bookkeeping
self._sensor_readings.append(IUVolumeSensorReading(stime, volume))
Expand Down Expand Up @@ -1201,6 +1202,9 @@ def sensor_state_change(event: HAEvent):
self._start_volume = self._total_volume = None
self._start_time = stime
self._sensor_readings.clear()
self._flow_rates.clear()
self._flow_rate_sum = 0
self._flow_rate_sma = None

try:
self._start_volume = self.read_sensor(stime)
Expand Down

0 comments on commit 7ff9b51

Please sign in to comment.