diff --git a/homeassistant/components/sensor/statistics.py b/homeassistant/components/sensor/statistics.py index e7a35b5fdf0..e011121f4a2 100644 --- a/homeassistant/components/sensor/statistics.py +++ b/homeassistant/components/sensor/statistics.py @@ -120,7 +120,7 @@ class StatisticsSensor(Entity): self.hass, self._entity_id, async_stats_sensor_state_listener) if 'recorder' in self.hass.config.components: - # only use the database if it's configured + # Only use the database if it's configured self.hass.async_create_task( self._async_initialize_from_database() ) @@ -129,11 +129,20 @@ class StatisticsSensor(Entity): EVENT_HOMEASSISTANT_START, async_stats_sensor_startup) def _add_state_to_queue(self, new_state): + """Add the state to the queue.""" + if new_state.state == STATE_UNKNOWN: + return + try: - self.states.append(float(new_state.state)) + if self.is_binary: + self.states.append(new_state.state) + else: + self.states.append(float(new_state.state)) + self.ages.append(new_state.last_updated) except ValueError: - pass + _LOGGER.error("%s: parsing error, expected number and received %s", + self.entity_id, new_state.state) @property def name(self): diff --git a/tests/components/sensor/test_statistics.py b/tests/components/sensor/test_statistics.py index 8552ed9efad..9b4e53dbab9 100644 --- a/tests/components/sensor/test_statistics.py +++ b/tests/components/sensor/test_statistics.py @@ -40,7 +40,7 @@ class TestStatisticsSensor(unittest.TestCase): def test_binary_sensor_source(self): """Test if source is a sensor.""" - values = [1, 0, 1, 0, 1, 0, 1] + values = ['on', 'off', 'on', 'off', 'on', 'off', 'on'] assert setup_component(self.hass, 'sensor', { 'sensor': { 'platform': 'statistics',