home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
72.84k stars 30.51k forks source link

Exception in the filter sensor if the source sensor state is 'unknown' #32395

Closed epiniguin closed 4 years ago

epiniguin commented 4 years ago

The problem

I use the outlier and median filters for some of my sensors. Sometimes these sensors have 'unknown' state. After the source sensor received unknown state the filter sensor receives also 'unknown' and in the log there is an error(see below). The filter sensor continues to receive errors after the source sensor returns to normal functioning and the filter sensor state is 'unknown'.

Only restart of Home Assistant solves a problem.

Environment

Problem-relevant configuration.yaml

- platform: filter
  name: "Luftdaten PM10"
  entity_id: sensor.luftdaten_pm10_raw
  filters:
    - filter: outlier
      window_size: 3
      radius: 10.0
    - filter: time_simple_moving_average
      window_size: 00:20
      precision: 2

Traceback/Error logs

ERROR (MainThread) [homeassistant.core] Error doing job: Exception in callback async_track_state_change.<locals>.state_change_listener(<Event st
ate_...128022+02:00>>) at /srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/event.py:81
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/event.py", line 103, in state_change_listener
    event.data.get("new_state"),
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/core.py", line 364, in async_run_job
    target(*args)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/filter/sensor.py", line 189, in filter_sensor_state_listener
    filtered_state = filt.filter_state(copy(temp_state))
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/filter/sensor.py", line 389, in filter_state
    filtered = self._filter_state(FilterState(new_state))
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/filter/sensor.py", line 472, in _filter_state
    median = statistics.median([s.state for s in self.states]) if self.states else 0
  File "/usr/local/lib/python3.7/statistics.py", line 376, in median
    data = sorted(data)
TypeError: '<' not supported between instances of 'float' and 'str'

Additional information

probot-home-assistant[bot] commented 4 years ago

Hey there @dgomes, mind taking a look at this issue as its been labeled with a integration (filter) you are listed as a codeowner for? Thanks!