Successfully Configuring a Water Level Sensor with ESPHome

Hey everyone, I wanted to share my recent success in setting up a water level sensor using ESPHome. This project was a bit challenging, but with some research and experimentation, I managed to get it working perfectly. Let me walk you through my journey and hopefully inspire others to tackle similar projects!

First, I purchased a waterproof ultrasonic distance transducer, which I connected to my ESP32 board. The goal was to measure the water level in my tank, which is about 1.12 meters deep. I mounted the sensor about 25 cm above the tank to account for its inability to read obstacles closer than 25 cm. Initially, I was getting distance readings in meters, but I wanted to convert these into a percentage scale (0-100) to make the data more user-friendly.

The challenge was figuring out how to reverse the distance readings since the water level would only decrease, not increase. After some trial and error, I realized I needed to adjust the formula to reflect the inverse relationship between distance and water level. I ended up using a simple linear transformation to map the distance range (0.25m to 1.12m) to a percentage scale.

To make this work seamlessly within Home Assistant, I decided to use a template sensor. This allowed me to dynamically calculate the water level percentage based on the raw distance readings from the ESP32. Here’s a snippet of the YAML code I used:

yaml
sensor:

  • platform: template
    sensors:
    water_level:
    value_template: >-
    {% if states(‘sensor.esp32_water_distance’) | float %}
    {{ (1.12 - (states(‘sensor.esp32_water_distance’) | float - 0.25)) / 0.87 * 100 | round(2) }}
    {% else %}
    unknown
    {% endif %}
    friendly_name: Water Level
    unit_of_measurement: ‘%’

This setup now provides a clear and intuitive representation of the water level in my tank. It’s been a fantastic learning experience, and I’m proud to have contributed to my smart home ecosystem. If anyone has questions or needs help with similar projects, feel free to reach out!

Happy tinkering everyone! :rocket: