Is it possible to implent the ability to have an ‘update time’.
Used to check if the entity has been updated within a defined timeframe (say 10s. settable via parameter).
For example: if a sensor reads 5°, and it jumps constantly around 5 and 5.1°. This floods Home Assistant with updates, and database grows exponentially with meaningless data.
I’ve tried to implement this with a custom component, code snipplet below, but I’m not sure this is the right approach:
def update(name, value):
_LOGGER.debug("Variable %s changed its value to %d", name, value)
# Modification
needsupdate = False
if self._state_dict[state_key] == None:
needsupdate = True
else:
states = self.hass.states.get('sensor.' + self._name)
_LOGGER.debug("Sensor %s has state: %s", self._name, states)
update_after = dt_util.utcnow() - timedelta(seconds=update_interval)
if states != None and update_after > states.last_updated:
needsupdate = True
if needsupdate == True:
if factor is None:
tempvalue = value
else:
tempvalue = value / factor
if round_perform:
x = Decimal(tempvalue)
if round_decimal == 0:
value = round(x, None)
else:
value = round(x, round_decimal)
else:
value = tempvalue
self._state_dict[state_key] = value
asyncio.run_coroutine_threadsafe(async_event_set(), self.hass.loop)
self.schedule_update_ha_state()