Need Help with Ultrasonic Sensor value calulation

I have an HC-SR04 Ultrasound sensor that I use to check the water level in a tank.
Of course out of the box it displays the water level like 5cm when full and 65cm when almost empty.
I use a lovelace gauge card to display this.
I am struggling to find info on how to change my configuration file so it would display 100% when full ( equal to 5cm distance) and 10% when almost empty at 65cm .

I was hoping there was someone here that knows how to modify my yaml file below. Thanks in advance !!

esphome:
name: salt-water-level
friendly_name: Salt Water Level

esp8266:
board: esp01_1m

Enable logging

logger:

Enable Home Assistant API

api:
encryption:
key: “7YDalWEzkF3UJArcDl5PVjukiJVtWhFjuMrfC2XFSt4=”

ota:

  • platform: esphome
    password: “f80a92f20e08e9d4e36b23eb87d”

wifi:
ssid: xxxxxxxx
password: xxxxxxxx

Enable fallback hotspot (captive portal) in case wifi connection fails

ap:
ssid: “Salt-Water-Level”
password: “kR3oyTOdvmw9”

captive_portal:
sensor:

  • platform: ultrasonic
    trigger_pin: GPIO4 # Replace with your actual trigger pin (e.g., GPIO2)
    echo_pin: GPIO2 # Replace with your actual echo pin (e.g., GPIO4)
    name: “Salt Water Level”
    unit_of_measurement: “cm” # Or your preferred unit
    filters:
    • multiply: 100 # convert meters to cm
      accuracy_decimals: 1
      update_interval: 2s # Adjust as needed

    filters:
      - calibrate_linear:
         method: least_squares
         datapoints:
          - 5 -> 100
          - 72 -> 0

Try a linear calibration. You may have to adjust the numbers to suit.

unit_of_measurement: "%"
1 Like

Tried it but getting an error

sensor:

  • platform: ultrasonic
    trigger_pin: GPIO4 # Replace with your actual trigger pin (e.g., GPIO2)
    echo_pin: GPIO2 # Replace with your actual echo pin (e.g., GPIO4)
    name: “Salt Water Level”
    unit_of_measurement: “%” # Or your preferred unit
    filters:
    • multiply: 100 # convert meters to cm
      accuracy_decimals: 1
      update_interval: 2s # Adjust as needed
    • calibrate_linear:
      method: least_squares
      datapoints:
      • 5 → 100
      • 72 → 0

Blockquote

INFO ESPHome 2025.4.0
INFO Reading configuration /config/esphome/salt-water-level.yaml…
ERROR Error while reading config: Invalid YAML syntax:

while parsing a block mapping
in “/config/esphome/salt-water-level.yaml”, line 31, column 5
expected , but found ‘’
in “/config/esphome/salt-water-level.yaml”, line 40, column 7

HI wknaus
It would be easier to read you code if you could format it correctly using </> button. The problem is probably just an indentation error.

My apologies… I didn’t see the button but have now found it

sensor:
  - platform: ultrasonic
    trigger_pin: GPIO4    # Replace with your actual trigger pin (e.g., GPIO2)
    echo_pin: GPIO2       # Replace with your actual echo pin (e.g., GPIO4)
    name: "Salt Water Level"
    unit_of_measurement: "%" # Or your preferred unit
    filters:
      - multiply: 100 # convert meters to cm
    accuracy_decimals: 1
    update_interval: 2s # Adjust as needed
      - calibrate_linear:
         method: least_squares
         datapoints:
          - 5 -> 100
          - 72 -> 0
  - platform: ultrasonic
    trigger_pin: GPIO4    
    echo_pin: GPIO2       
    name: "Salt Water Level"
    unit_of_measurement: "%" 
    filters:
      - multiply: 100 
      - calibrate_linear:
         method: least_squares
         datapoints:
          - 5 -> 100
          - 72 -> 0
    accuracy_decimals: 1
    update_interval: 2s

Try this

3 Likes

It’s working :slight_smile: Thank you so much for your help !!! You have no idea how much it’s appreciated !!

For completeness, calibrate_linear is a filter, so it has to slide in below the filters: key.

Please share more info about the hardware part. I’m looking for a way to monitor the groundwater level underneath my home without opening the latch every time.

It’s just a D1 Mini and the HC-SR04 in a plastic case I had laying around. Very simple.

Hm, pretty newbie I guess. I need a bit more info like how to connect, do I need to flash something (how) etc. Sorry if this is a dumb question :pray:

This should help: https://youtu.be/6kzVtV2lZNk?si=Kh4SFAE2NfMkhgzD

That makes it a lot more workable, thnx.
I also noticed a waterproof sensor JSN-SRT04T. See Waterproof Ultrasonic Distance Sensors with Arduino