With this part of the ESPHome .yaml code loaded into an ESP32-WROOM-32 for a JSN-SR04T-2.0 sensor (default settings / no resistor soldered into R27):
sensor:
- platform: ultrasonic
trigger_pin: GPIO5 # D5
echo_pin: GPIO18 # D18
name: "JSN-SR04T Distance"
update_interval: 5s
unit_of_measurement: "cm"
accuracy_decimals: 1
timeout: 2.0m
filters:
- lambda: |-
if (x < 0.1 || x > 4.5) {
return NAN; // Return NAN for out-of-range values
}
float value_in_cm = x * 100; // Convert meters to cm height above floor
return int(value_in_cm * 100 + 0.5) / 100.0; // Round to 2 decimal places
I’m wondering why I periodically see readings like this:
[21:40:45.775][D][ultrasonic.sensor:035]: 'JSN-SR04T Distance' - Distance measurement timed out!
[21:40:45.831][D][sensor:135]: 'JSN-SR04T Distance': Sending state 21474836.00000 cm with 1 decimals of accuracy
… and how to prevent them. I’m just getting started with ESPHome, but what is a return value of NAN actually supposed to accomplish when the reading is out of range?
It looks like the timeout products invalid value (either nan or infinite) that doesn’t get filtered out with your lambda.
Try something like this:
(not tested, if it doesn’t compile, comment out both filter_out: nan lines)
sensor:
- platform: ultrasonic
trigger_pin: GPIO5 # D5
echo_pin: GPIO18 # D18
name: "JSN-SR04T Distance"
update_interval: 5s
unit_of_measurement: "cm"
accuracy_decimals: 1
timeout: 2.0m
filters:
- filter_out: nan
- lambda: |-
if (!isfinite(x)) return NAN;
if (x < 0.22 || x > 4.5) return NAN;
return x * 100.0;
- filter_out: nan # to filter out nans fron lamda
But why you have such a small timeout value? 4.5m would be better to prevent timeouts in the first place.
Also, consider adding median filter (as last filter) to get rid of any outliers or spikes.
Karosm, thank you for your suggestions! I did find that I had to revise the first line of the lambda filter to:
if (!infinity) return NAN;
To answer your question - no good reason for the listed syntax, other than it was based on the first coding I found that would validate, compile and install! Everything else I’d tried beforehand had failed.
This sensor is watching the level in the basement sump hole, and when it rains, the level can rise rather quickly. Not sure if a long or short timeout is better in this particular application.
Code validated, compiled, downloaded! Currently watching the trend …
Next step - configuring SMS messaging to alert me of a high level.
Sensors have timeout filter.
Yours is not under filters, but parameter for ultrasonic sensor. " timeout (Optional, float): The number of meters for the timeout. Most sensors can only sense up to 2 meters. Defaults to 2 meters."