From 46b5b6240f910ca4f21f73ede9ee3544576db8fb Mon Sep 17 00:00:00 2001 From: ehendrix23 Date: Mon, 5 Nov 2018 15:12:46 -0700 Subject: [PATCH] Improve debug log information (#18230) Added debug log information for when records are purged and added entity_id to existing debug information to identify the entity the debug information is for. --- homeassistant/components/sensor/statistics.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/sensor/statistics.py b/homeassistant/components/sensor/statistics.py index 453acb94b11..6181a4ae094 100644 --- a/homeassistant/components/sensor/statistics.py +++ b/homeassistant/components/sensor/statistics.py @@ -173,12 +173,20 @@ class StatisticsSensor(Entity): """Remove states which are older than self._max_age.""" now = dt_util.utcnow() + _LOGGER.debug("%s: purging records older then %s(%s)", + self.entity_id, dt_util.as_local(now - self._max_age), + self._max_age) + while self.ages and (now - self.ages[0]) > self._max_age: + _LOGGER.debug("%s: purging record with datetime %s(%s)", + self.entity_id, dt_util.as_local(self.ages[0]), + (now - self.ages[0])) self.ages.popleft() self.states.popleft() async def async_update(self): """Get the latest data and updates the states.""" + _LOGGER.debug("%s: updating statistics.", self.entity_id) if self._max_age is not None: self._purge_old() @@ -191,7 +199,7 @@ class StatisticsSensor(Entity): self.median = round(statistics.median(self.states), self._precision) except statistics.StatisticsError as err: - _LOGGER.debug(err) + _LOGGER.debug("%s: %s", self.entity_id, err) self.mean = self.median = STATE_UNKNOWN try: # require at least two data points @@ -200,7 +208,7 @@ class StatisticsSensor(Entity): self.variance = round(statistics.variance(self.states), self._precision) except statistics.StatisticsError as err: - _LOGGER.debug(err) + _LOGGER.debug("%s: %s", self.entity_id, err) self.stdev = self.variance = STATE_UNKNOWN if self.states: @@ -241,7 +249,7 @@ class StatisticsSensor(Entity): list so that we get it in the right order again. """ from homeassistant.components.recorder.models import States - _LOGGER.debug("initializing values for %s from the database", + _LOGGER.debug("%s: initializing values from the database", self.entity_id) with session_scope(hass=self._hass) as session: @@ -254,4 +262,5 @@ class StatisticsSensor(Entity): for state in reversed(states): self._add_state_to_queue(state) - _LOGGER.debug("initializing from database completed") + _LOGGER.debug("%s: initializing from database completed", + self.entity_id)