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.
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
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.