After getting the range distance, or maybe even before it, get the RangeStatus. This is NOT the status that the function returns, but is a register in the sensor that tells you what the status of your number is.
Zero - is what you are hoping for. That's perfect. But there are a dozen other RangeStatus and you can either treat them as warnings and accept them. Or you can treat them as "no legitimate range" and proceed as if no valid target was within the area.
We've always struggled with what to return if the sensor did not get a good range. Generally the algorithm just gives up, sets the RangeStatus, and the distance is whatever it happened to be when the logic gave up.
So your really, really have to check the RangeStatus. It's arguably more important than the distance.
-john
If this or any post solves your issue, please mark them as 'Accept as Solution' It really helps. And if you notice anything wrong do not hesitate to 'Report Inappropriate Content'. Someone will review it.