Skip to content

Commit

Permalink
Prevent slight decreases in total values
Browse files Browse the repository at this point in the history
  • Loading branch information
ankohanse committed Nov 3, 2024
1 parent 935a4ef commit dec6a6f
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions custom_components/dabpumps/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from homeassistant.helpers.entity_registry import async_get
from homeassistant.helpers.event import async_track_time_interval
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.helpers.significant_change import check_percentage_change

from datetime import timedelta
from datetime import datetime
Expand Down Expand Up @@ -174,6 +175,17 @@ def _update_attributes(self, status, is_create):
)
changed = True

# additional check for TOTAL and TOTAL_INCREASING values:
# ignore decreases that are not significant (less than 10% change)
if self._attr_state_class in [SensorStateClass.TOTAL, SensorStateClass.TOTAL_INCREASING] and \
self._attr_native_value is not None and \
attr_val is not None and \
attr_val < self._attr_native_value and \
not check_percentage_change(self._attr_native_value, attr_val, 10):

_LOGGER.debug(f"Ignore non-significant decrease in sensor '{status.key}' ({status.unique_id}) from {self._attr_native_value} to {attr_val}")
attr_val = self._attr_native_value

# update value if it has changed
if is_create or self._attr_native_value != attr_val:
self._attr_native_value = attr_val
Expand Down

0 comments on commit dec6a6f

Please sign in to comment.