The outlier filter seems to indeed work as designed but not as expected.
I’m using it to filter glitches from my heatpump MQTT interface which glitches a bit if the device is disconnected from the network. It the nsends a single wrong value that is several million times greater than the old one which especially screws up any utility meter that e.g. tracks energy consumption throughout the year.
I created a simple test for the outlier filter with an input number and the following outlier config
- platform: filter
name: "outlier_test"
entity_id: input_number.outlier_test_raw
filters:
- filter: outlier
precision: 1
window_size: 1
radius: 10
From the docs I would have expected that if the input_number changes ±10 in a single step, the value is accepted and if it is more than that it uses the median of the previous window, hence the last value in case window_size is 1.
Setting it from 1 to 9 works as expected.
Setting the input number from 9 to 100 the resulting value is 9 .
The debug log notes that an outlier is detected
2024-04-26 07:20:12.779 DEBUG (MainThread) [homeassistant.components.filter.sensor] Update filter on event: <Event state_changed[L]: entity_id=input_number.outlier_test_raw, old_state=<state input_number.outlier_test_raw=9.0; initial=None, editable=True, min=0.0, max=1e+17, step=1.0, mode=box, friendly_name=outlier_test_raw @ 2024-04-26T07:19:57.840015+02:00>, new_state=<state input_number.outlier_test_raw=100.0; initial=None, editable=True, min=0.0, max=1e+17, step=1.0, mode=box, friendly_name=outlier_test_raw @ 2024-04-26T07:20:12.777708+02:00>>
2024-04-26 07:20:12.780 DEBUG (MainThread) [homeassistant.components.filter.sensor] Outlier nr. 1 in input_number.outlier_test_raw: 100.0
2024-04-26 07:20:12.780 DEBUG (MainThread) [homeassistant.components.filter.sensor] outlier(input_number.outlier_test_raw=100.0) -> 9.0
It goes wrong if now a normal value is set again. E.g. setting the input number from 100 (the glitch value) to e.g. 10
The resulting value is not 9 as one would expect, but 100, the previously filtered value as that of course is now part of the window_size.
2024-04-26 07:29:48.964 DEBUG (MainThread) [homeassistant.components.filter.sensor] Outlier nr. 2 in input_number.outlier_test_raw: 10.0
2024-04-26 07:29:48.964 DEBUG (MainThread) [homeassistant.components.filter.sensor] outlier(input_number.outlier_test_raw=10.0) -> 100.0```
Using a larger window_size does not change this problem, it would just decrase the overall value which might not be enough depending on how different the outlier is.
It would help if the median was calculated based on the outlier filter result and not the input entity as that would exclude any previously ignored values.
Update: I’ve raised this as issue Outlier filter not dropping filtered value · Issue #116226 · home-assistant/core · GitHub. The filter code has an explicit flag to include the raw input value in the previous states. Changing this or making it configurable would make the filter work as expected.