Need help with Advanced noisy sensor filter

I have this data from an ultrasonic sensor. it works great when the pellet stove is off, but when its on, all kinds of noise is introduced by heat, vibrations, fan noise, etc.

the bottom of the graph is where the correct data is. I already set up a lambda to filter unwanted values, no I just want the bottom data points. avg doesnt work as it skews high.

    filters:
      #throw away unusable values
      - filter_out: nan
      - lambda: |-
          if (x >= 0.4){      
            return 0.4;
          } else if (x <= 0){
            return 0.01;}
          return x;

now I need an advanced filter to grab only the lowest values from the graph, where the real data is!

https://esphome.io/components/sensor/index.html#min

I see your deleted post :slight_smile: try the one I linked to. It may take some fiddling with the window size to get the result you want.

It totally works. red is min filtered, the yellow line is then averaged. so, after I string them all together, it should be good to go. That sensor (HC-SR04) really sucks without all this filtering. I appreciate the link. I had not seen that particular filter before and I thought Id seen them all!

#red line
      - min:
          window_size: 10
          send_every: 15
          send_first_at: 3  
#yellow line
      - sliding_window_moving_average:
          window_size: 600
          send_every: 15  

image