Filtering Outliers Before Taking a Minimum
What happened
A minimum statistic over a time window was wrong, because the underlying sensor occasionally reported bogus low readings. A minimum picks up every one of these, so a single bad sample can set the result for the whole window.
After I changed the configuration and restarted, the new sensors didn't appear right away. I wasn't sure whether the system was still loading or the config had failed, so I checked the logs. The system had just been slow to finish loading, and the change was fine.
The fix
- A filtered sensor: I added a new sensor that wraps the raw reading. It discards values that jump too far from the recent samples, which catches the bogus readings.
- Statistic input: The minimum statistic now reads from the filtered sensor instead of the raw one.
Why it works
A real quantity like a battery level doesn't move by large amounts between samples. A large sudden jump is much more likely to be a bad reading than a real change. Filtering before taking the minimum keeps the glitches out of the statistic. Taking the minimum of raw data is especially sensitive to this, because one bad low sample determines the result.
Takeaway
When a statistic such as a minimum or maximum looks wrong, check the raw data for outliers. Filter them out before aggregating.