Hi all,
I’m using the A01NYUB with an Arduino Nano ESP32 to measure the water in my water tank. I’m using ESPHome to link it with Home Assistant.
I’m having issues with the readings however. I think my code is ok, but looking at the logs I can see a lot of errors and the values keep jumping from the the expected value to approx 38cm so I think the issue lies in the sensor.
The ultrasonic sensor is attached to a threaded PVC cap looking directly down into the water tank. This is a large cement water tank. Not sure how much water it holds, but think 20,000L type of tank.
Any ideas how I can fix this?
I’m assuming I should keep the bell mouth on as I’d estimate the tank to be a little over 2m tall so it needs a good bit of range. The water shouldn’t come too close to the sensor, I’d say maybe 40cm away if the tank was completely full.
Here’s an example of ESPHome logs:
As you can see most of the readings are errors and when it does get a reading it keeps getting around 38cm which is not accurate and 74cm which I believe to be accurate (I know this because it was 1.4m before it started raining so I do have some valid data to show the tank filling up), but since the majority of readings are 38cm I’m getting bad data in Home Assistant.
I should note that there was a few hours when I did get consistent readings when I first set it up, so it seems that the sensor can work correctly. I’m thinking perhaps it’s temperature related or humidity related or something like that? Could it be caused by echos interfering? If so how could I fix that?
I’ve played around with the code a little bit but can’t seem to fix it
esphome:
name: water-tank
friendly_name: Water-Tank
esp32:
board: esp32-s3-devkitc-1
framework:
type: arduino
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: -REDACTED
ota:
- platform: esphome
password: -REDACTED
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Water-Tank Fallback Hotspot"
password: -REDACTED-
captive_portal:
# Water Tank - Ultrasonic Sensor A01NYUB
output:
- platform: gpio
pin: GPIO10
id: A01NYUB_Sensor_Power
uart:
rx_pin: GPIO8
tx_pin: GPIO9
baud_rate: 9600
sensor:
- platform: "a01nyub"
name: "Water Level"
id: "Water_Level"
filters:
# - max:
# window_size: 60
# send_every: 300
# send_first_at: 30
- quantile:
window_size: 7
send_every: 4
send_first_at: 3
quantile: .9
## chatgpt code
- platform: template
name: "Water Tank Fill Percentage"
unit_of_measurement: "%"
lambda: |-
if (id(Water_Level).state == NAN) {
return NAN; // Return NAN if the distance is invalid
}
// Calculate percentage based on the distance
float distance = id(Water_Level).state;
// Define the max and min distance for full and empty tanks
float min_distance = 2.00; // Empty tank distance (in metres)
float max_distance = .30; // Full tank distance (in metres)
// Clamp the value between 0 and 100 to avoid out-of-bounds errors
float percentage = 100.0 * (1.0 - (distance - max_distance) / (min_distance - max_distance));
// Ensure the percentage is between 0 and 100
if (percentage < 0) {
return 0.0;
} else if (percentage > 100) {
return 100.0;
}
return percentage;
Thanks for taking the time to read.
Cheers