filter_out sensor filter is filtering out too many values on certain conditions
- Dominant language
- No language data
- Stars
- 313
- Forks
- 40
- PR merge metrics
- No merged PRs in 30d
Description
### The problem
The `filter_out` filter for the sensor component is filtering out values unexpectedly on certain occasions. The filter uses the `accuracy_decimals` property of the sensor and uses this to compare the values (see [source](https://github.com/esphome/esphome/blob/f6e5a8cb2a9840840239006fd7649f56cab02d02/esphome/components/sensor/filter.cpp#L294-L298)). This causes problems if this filter is used before an e.g. `multiply` filter, as `filter_out: 0.0` will filter out all values below 0.5 if `accuracy_decimals` is 0 as the filter multiplies the incoming values by `10^accuracy_decimals` and then rounds to the nearest integer for comparison (Which is also not documented).
The documentation for `accuracy_decimals` says that the setting will only affect the reported values, not the raw values processed by the filters.
> Manually set the number of decimals to use when reporting values. This does not impact the actual value reported to Home Assistant, it just sets the number of decimals to use when displaying it.
So using
``` yaml
accuracy_decimals: 0
filters:
- filter_out: 0.0
- multiply: 100.0
```
for any sensors which report non integer values will result in unexpected behaviour.
I encountered this for a water sensor, where I measure the distance from a reference point above the water to the surface using the `ultrasonic` component which reports a value in meters, filter out too short distances and then convert the value to the actual volume in the container. (See snippet below, but the issue only became clear while looking at the source)
### Which version of ESPHome has the issue?
2022.8.3
### What type of installation are you using?
pip
### Which version of Home Assistant has the issue?
_No response_
### What platform are you using?
ESP8266
### Board
_No response_
### Component causing the issue
sensor
### Example YAML snippet
```yaml
sensor:
- platform: ultrasonic
trigger_pin: D6
echo_pin: D5
name: capacity
unit_of_measurement: l
accuracy_decimals: 0
filters:
- filter_out: 0.00
- multiply: -1.0
- offset: 1.36
- multiply: 2702.7
```
### Anything in the logs that might be useful for us?
_No response_
### Additional information
_No response_
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.