Help filtering highest values from Sensor results

Hi,
I set up an ultrasonic sensor, but it is returning very variable responses. The only correct thing is:
in a sample of 100 measurements, the top 10 correspond to the correct expected measurement.
I would like to create a filter that selects the highest 10 out of 100. Then it would only pass the value of the highest 10 to the home assistant.

Using Median, would select a wrong number.

JSN-SR04T / AJ-SR04M Waterproof Ultrasonic Sensor Module
NodeMCU
Pins Rx and Tx


esphome:
  name: nodemcu1
  platform: ESP8266
  board: nodemcuv2

## SENSOR ###
sensor:
- platform: ultrasonic
  trigger_pin: GPIO3
  echo_pin: GPIO1
  unit_of_measurement: m
  update_interval: 1s
  name: "Distancia"
  pulse_time: 20us
  timeout: 20m
  filters:
     - lambda: |-
         float MIN_VALUE = 0.25;
         float MAX_VALUE = 6.0;
         if (MIN_VALUE <= x && x <= MAX_VALUE) return x;
         else return {}; 
     - filter_out: 00.0
     - offset: 0.04
     - filter_out: 0.04

I still get lots of time out or wrong readings. The only correct are the top highest values.

I appreciate if someone helps me.

LOG:

Solved all measure errors changing the power source.

Please delete the post.

Regarding deletion, see point 19 here: How to help us help you - or How to ask a good question

Hi Tom,

My question is still not answered. So, in this particular case, my post woudn´thelp anyone, that’s why I told yesterday that we could delete the post.

Today in the morning, I talked to a friend, that is electrical engineer, and he teached me that all neutral (ground) wires should be connected in order to work properly, in case your device has 2 different power sources.

So, completing how I solved my problem:

  • there were no defects on the sensor,
  • the wiring was wrong, because I had 2 different power sources (1 for NodeMCU and another for the sensor).
  • In cases you have 2 different power sources you should connect the ground pins.

Other thoughts:
My sensor is 5V, but is working flawlessly with a Nodemcu 3V pin.

There was no need to implement complicated calculations over the results to filter values.

Final proggraming is: (added ## coments for anyone that needs in the future)

## SENSORES ###
sensor:
- platform: ultrasonic
 trigger_pin: GPIO3
 echo_pin: GPIO1
 unit_of_measurement: cm
 update_interval: 1s
 name: "Distancia"
 pulse_time: 20us
 timeout: 20m  # pick your model distance range in meters
 filters:
    - lambda: |-
        float MIN_VALUE = 0.20; #put your distance range
        float MAX_VALUE = 6.0;  #put your distance range
        if (MIN_VALUE <= x && x <= MAX_VALUE) return x*100;
        else return {}; 
       
    - offset: 3  #just for adjusting the results
    - median:          # this filters possible outliers
       window_size: 5
       send_every: 3
       send_first_at: 3

You solved your problem though. Others may have the same problem and wish to know about your solution.

Yes, agree. In fact, that’s why I made a better explanation later.
Yesterday I was tired.

And I found no coment about this interference (2 power sources) anywhere else during my search for an answer. Maybe now this could be usefull for someone. :grin:

2 Likes