esphome / feature-requests

ESPHome Feature Request Tracker
https://esphome.io/
411 stars 26 forks source link

standard deviation support for sensor component #2295

Open ghost opened 1 year ago

ghost commented 1 year ago

Describe the problem you have/What new integration you would like

It's was so helpful if sensor component have a running standard deviation output or filter, sometimes when condition in environment is not ideal or internal sensor part damaged, sensor keep starting publish random values so user get notification alert or disable other component rely on specific condition.

[18:08:37][D][ultrasonic.sensor:040]: 'Ping Sensor' - Got distance: 0.86 m
[18:08:37][D][ultrasonic.sensor:040]: 'Ping Sensor' - Got distance: 0.82 m
[18:08:37][D][ultrasonic.sensor:040]: 'Ping Sensor' - Got distance: 0.77 m
[18:08:37][D][ultrasonic.sensor:040]: 'Ping Sensor' - Got distance: 0.76 m
[18:08:37][D][ultrasonic.sensor:040]: 'Ping Sensor' - Got distance: 0.89 m
[18:08:37][D][ultrasonic.sensor:040]: 'Ping Sensor' - Got distance: 0.85 m
[18:08:37][D][ultrasonic.sensor:040]: 'Ping Sensor' - Got distance: 0.78 m
[18:08:37][D][ultrasonic.sensor:040]: 'Ping Sensor' - Got distance: 0.78 m
[18:08:37][D][ultrasonic.sensor:040]: 'Ping Sensor' - Got distance: 0.82 m
...
[18:33:06][D][ultrasonic.sensor:040]: 'Ping Sensor' - Got distance: 0.84 m
[18:33:07][D][ultrasonic.sensor:040]: 'Ping Sensor' - Got distance: 0.92 m
[18:33:07][D][ultrasonic.sensor:040]: 'Ping Sensor' - Got distance: 0.74 m
[18:33:07][D][ultrasonic.sensor:040]: 'Ping Sensor' - Got distance: 0.91 m
[18:33:07][D][ultrasonic.sensor:040]: 'Ping Sensor' - Got distance: 0.71 m
[18:33:07][D][ultrasonic.sensor:040]: 'Ping Sensor' - Got distance: 0.92 m
[18:33:07][D][ultrasonic.sensor:040]: 'Ping Sensor' - Got distance: 0.75 m
[18:33:07][D][ultrasonic.sensor:040]: 'Ping Sensor' - Got distance: 0.97 m
[18:33:07][D][ultrasonic.sensor:040]: 'Ping Sensor' - Got distance: 0.90 m

Please describe your use case for this integration and alternatives you've tried:

Sensor value keep fluctuating in not ideal environment. Before request i already tried many arduino library's which provide std_dev they all sending same state but for unknown reason they keep sending nan, the current working library break in last update. Additional context

sensor:
  - platform: ultrasonic
     ...
    standard_deviation:
      above: 14%
      then:
         - id(fluctuation).publish_state(true);

for current situation i implement this work well but look's weird

 - platform: ultrasonic
   filters:
      - delta: 0.01
      - lambda: |-
          id(fluctuation).publish_state(true);
          return x;
      - debounce: 0.6s
      - lambda: |-
          id(fluctuation).publish_state(false);
          return x;
ghost commented 1 year ago

Don't know why esphome conflict with arduino library, but this one work when add unnecessary function call getAverage.

on_value:
      then:
        - lambda: |-
            ESP_LOGD("Stats", "%f %f %f", stats.getStandardDeviation(), stats.getAverage(), stats.getMaxInBuffer());
includes:
    - l4-tank.h
  libraries:
    - RobTillaart/RunningAverage

Replace by above filter

   filters:
      - lambda: |-
          stats.addValue(x);
          if (stats.getStandardDeviation() >= 0.02) {
            return {};
          } else {
            return x;
          }

so much improvement in output value, see after timestamp 1:20AM

Screenshot 2023-07-01 at 1 32 11 AM

Maybe esphome should have standard deviation filter to deal with sensor fluctuation