How is your HC-SR04 since last update?

mine stopped then started working again but with wrong distances, I kinda really need this to be working for my drip irrigation system… it was working perfectly just 1 week ago, but since then all is wrong,

my code didn’t change though could anyone be kind enough to help me out ?

here’s the code:

  - platform: ultrasonic
    trigger_pin: GPIO 4
    echo_pin: GPIO 5
    name: 'Water tank Level'
    unit_of_measurement: '%'
    accuracy_decimals: 1
    update_interval: 1s
    icon: mdi:water-percent
    filters:
      - lambda: return ((((x*100)-8)-(46-8))/(46-8))*-100;
      - sliding_window_moving_average:
          window_size: 10 #creates a moving average of the last 10 values
          send_every: 1 #sends the moving average at every measurement (but only if it passes the delta filter below)) 
          send_first_at: 1 #after startup immediately start sending the result rather than wait for the first 10 measurements
      - delta : 0.05 #only send the result if the level difference with the last sent result is higher than this
      - filter_out: nan
      
      
  - platform: ultrasonic
    trigger_pin: GPIO 4
    echo_pin: GPIO 5
    update_interval: 2s
    name: 'Water Tank Volume'
    unit_of_measurement: 'l'
    icon: mdi:cup-water
    accuracy_decimals: 2
    filters:
      - lambda: return ((((x*100)-8)-(46-8))/(46-8))*-70;
      - sliding_window_moving_average:
          window_size: 10 #creates a moving average of the last 10 values
          send_every: 1 #sends the moving average at every measurement (but only if it passes the delta filter below)) 
          send_first_at: 1 #after startup immediately start sending the result rather than wait for the first 10 measurements
      - delta : 0.05 #only send the result if the level difference with the last sent result is higher than this
      - filter_out: nan

Hi.

It seems that you are running two sensors on the same gpio pins. This is probably going to cause strange behavior. I guess that you only have one sensor and then calculate the volume and the percentage of the tank.

The easiest solution is to have the distance as one sensors.
Then make two template sensors with lambda.

  - platform: ultrasonic
    trigger_pin: GPIO 4
    echo_pin: GPIO 5
    name: 'distance'
    id: dist
    unit_of_measurement: 'm'
    accuracy_decimals: 1
    update_interval: 1s
    filters:
      - sliding_window_moving_average:
          window_size: 10 #creates a moving average of the last 10 values
          send_every: 1 #sends the moving average at every measurement (but only if it passes the delta filter below)) 
          send_first_at: 1 #after startup immediately start sending the result rather than wait for the first 10 measurements
      - delta : 0.05 #only send the result if the level difference with the last sent result is higher than this
      - filter_out: nan

  - platform: template
    name: 'Water tank Level'
    unit_of_measurement: '%'
    accuracy_decimals: 1
    update_interval: 1s
    icon: mdi:water-percent
    lambda: |- 
        return ((((id(dist).state*100)-8)-(46-8))/(46-8))*-100;
  - platform: template
    name: 'Water Tank Volume'
    unit_of_measurement: 'l'
    icon: mdi:cup-water
    accuracy_decimals: 2
    lambda: |-
       return ((((id(dist).state*100)-8)-(46-8))/(46-8))*-70;

So the template values are referring to distance forme the distance sensor.
Then I refer the value by id(dist).state in the template
I’m not sure that I got everything right. But maybe this gives something to try.

/Mattias

It’s not two sensors, its 2 versions of displaying the same sensor :wink: One in liter one in %.

this was working perfectly a week ago, i havent changed anything, it just stopped for a few days, then re-started, but with the wrong distances.

i tried using the esphome base code, same result!

wait what?
can you please use my info from the code and give me an explanation using my infos, i really am not so well versed in esphome yet. or HA for that matter!
lol

thanks

I will try to explain what I wrote.

You have told esphome that you have 2 sensors connected to the same pins.
According to you that worked for avail at least. It was little bit surprising. That means that esphome is measuring distance on the same sensors from two different parts of the “system”. There is no guarantee that these will not interfere with each other and give strange values back. Its very hard to know whats going to happen.
So the theory is that it causes your problem. Sometimes it works and sometimes it doesn’t.

So my example above trying to solve this.
I have defined 1 sensor because you have one sensor. It measures the distance.
Then I defined two template sensors that take that raw distance value and send them up as the percentage and the volume.

One other way to try if these interfere with each other is do comment one of the senors away in the code and see if you get the correct measurement.

It can also hardware issues or moisture condensation on the ultrasonic sensor or something.
Does it say anything in the logs.

/Mattias

thanks for the reply, I obviously tried to use them individually to the same sudden bad results.

I wouldnt know how to implement your example, im quite new to all this language, i am learning but there’s a steep learning curve for a non-coding person. i’m getting so much better thanks to people like you who are willing to help.

ill try your config suggestion ill let you know in 5 mins

It always a little bit of a guessing game.

But I see that the filter_out: nan is almost at the bottom.
Try to move it upto the top placement in the filter part.

    filters:
      - filter_out: nan
      - sliding_window_moving_average:
          window_size: 10 #creates a moving average of the last 10 values
          send_every: 1 #sends the moving average at every measurement (but only if it passes the delta filter below)) 
          send_first_at: 1 #after startup immediately start sending the result rather than wait for the first 10 measurements
      - delta : 0.05 #only send the result if the level difference with the last sent result is higher than this

I dont know what going to happen when you do other filters or calculations on nan.
The sliding window maybe update the counter that it got a value but its a bad one that should not update the value. Could be an issue.

/Mattias

ok, but what i dont understand is why it was reporting near perfect results for a few weeks then suddenly stop? no change on my part… appart from HASSIO update.

now its reporting with your code in and only that one
-172 l tank volume
-247% full

small test tank is 56L x35 W x45 H sensor is at 8 cm from water when completely full.

any help would greatly appreciated

thank you

tried with and without sliding window, same bad results

still in the negatives now @ - 279% sometimes sometimes at -216%
with filter out nan as the only filter.

Hi.

What does the log says and the actual “distance” value. Something seems off.

/Mattias

there is definitely something wrong
heres an exerpt of the log:

[19:04:36][I][app:105]: ESPHome version 1.17.2 compiled on May 12 2021, 19:03:57
[19:04:36][C][wifi:443]: WiFi:
[19:04:36][C][wifi:303]:   SSID: [redacted]
[19:04:36][C][wifi:304]:   IP Address: 192.168.1.142
[19:04:36][C][wifi:306]:   BSSID: [redacted]
[19:04:36][C][wifi:307]:   Hostname: 'ultrasonic_v2'
[19:04:36][C][wifi:311]:   Signal strength: -61 dB ▂▄▆█
[19:04:36][C][wifi:315]:   Channel: 6
[19:04:36][C][wifi:316]:   Subnet: 255.255.255.0
[19:04:36][C][wifi:317]:   Gateway: 192.168.1.1
[19:04:36][C][wifi:318]:   DNS1: (IP unset)
[19:04:36][C][wifi:319]:   DNS2: (IP unset)
[19:04:36][C][logger:185]: Logger:
[19:04:36][C][logger:186]:   Level: DEBUG
[19:04:36][C][logger:187]:   Log Baud Rate: 115200
[19:04:36][C][logger:188]:   Hardware UART: UART0
[19:04:36][C][ultrasonic.sensor:035]: Ultrasonic Sensor 'Filter Tank VolumEsti'
[19:04:36][C][ultrasonic.sensor:035]:   Unit of Measurement: 'l'
[19:04:36][C][ultrasonic.sensor:035]:   Accuracy Decimals: 0
[19:04:36][C][ultrasonic.sensor:035]:   Icon: 'mdi:water'
[19:04:36][C][ultrasonic.sensor:036]:   Echo Pin: GPIO4 (Mode: INPUT)
[19:04:36][C][ultrasonic.sensor:037]:   Trigger Pin: GPIO5 (Mode: OUTPUT)
[19:04:36][C][ultrasonic.sensor:038]:   Pulse time: 10 µs
[19:04:36][C][ultrasonic.sensor:039]:   Timeout: 116618 µs
[19:04:36][C][ultrasonic.sensor:040]:   Update Interval: 5.0s
[19:04:36][C][captive_portal:169]: Captive Portal:
[19:04:36][C][ota:029]: Over-The-Air Updates:
[19:04:36][C][ota:030]:   Address: 192.168.1.142:8266
[19:04:36][C][ota:032]:   Using Password.
[19:04:36][C][api:095]: API Server:
[19:04:36][C][api:096]:   Address: 192.168.1.142:6053
[19:04:36][C][wifi_signal.sensor:009]: WiFi Signal 'WiFi Signal Sensor'
[19:04:36][C][wifi_signal.sensor:009]:   Device Class: 'signal_strength'
[19:04:36][C][wifi_signal.sensor:009]:   Unit of Measurement: 'dB'
[19:04:36][C][wifi_signal.sensor:009]:   Accuracy Decimals: 0
[19:04:40][D][api.connection:617]: Client 'Home Assistant 2021.5.2 (192.168.1.144)' connected successfully!
[19:04:41][D][ultrasonic.sensor:026]: 'Filter Tank VolumEsti' - Distance measurement timed out!
[19:04:46][D][ultrasonic.sensor:030]: 'Filter Tank VolumEsti' - Got distance: 2.65 m
[19:04:46][D][sensor:099]: 'Filter Tank VolumEsti': Sending state 30.21954 l with 0 decimals of accuracy
[19:04:51][D][ultrasonic.sensor:030]: 'Filter Tank VolumEsti' - Got distance: 6.54 m
[19:04:51][D][sensor:099]: 'Filter Tank VolumEsti': Sending state 56.86234 l with 0 decimals of accuracy
[19:04:56][D][ultrasonic.sensor:030]: 'Filter Tank VolumEsti' - Got distance: 8.90 m
[19:04:56][D][sensor:099]: 'Filter Tank VolumEsti': Sending state 76.56226 l with 0 decimals of accuracy
[19:05:01][D][ultrasonic.sensor:026]: 'Filter Tank VolumEsti' - Distance measurement timed out!
[19:05:06][D][ultrasonic.sensor:026]: 'Filter Tank VolumEsti' - Distance measurement timed out!
[19:05:11][D][ultrasonic.sensor:030]: 'Filter Tank VolumEsti' - Got distance: 1.65 m
[19:05:11][D][sensor:099]: 'Filter Tank VolumEsti': Sending state 61.52770 l with 0 decimals of accuracy
[19:05:16][D][ultrasonic.sensor:030]: 'Filter Tank VolumEsti' - Got distance: 8.30 m
[19:05:16][D][sensor:099]: 'Filter Tank VolumEsti': Sending state 70.77032 l with 0 decimals of accuracy
[19:05:18][D][sensor:099]: 'WiFi Signal Sensor': Sending state -61.00000 dB with 0 decimals of accuracy
[19:05:21][D][ultrasonic.sensor:030]: 'Filter Tank VolumEsti' - Got distance: 6.93 m
[19:05:21][D][sensor:099]: 'Filter Tank VolumEsti': Sending state 73.80261 l with 0 decimals of accuracy
[19:05:26][D][ultrasonic.sensor:030]: 'Filter Tank VolumEsti' - Got distance: 10.34 m
[19:05:26][D][sensor:099]: 'Filter Tank VolumEsti': Sending state 82.64629 l with 0 decimals of accuracy
[19:05:31][D][ultrasonic.sensor:030]: 'Filter Tank VolumEsti' - Got distance: 11.50 m
[19:05:31][D][sensor:099]: 'Filter Tank VolumEsti': Sending state 91.26496 l with 0 decimals of accuracy

actual disrtance from top of water to sensor: 22-23cm

total capacity of reservoir 70l
empty res distance: 46cm
dist. to top of water from sensor when completely full: 8cm

no condensation happening, sensor is clean… this is really weird.
used to be working perfectly, it just stopped being really accurate and started to do this , just like that…

heres the conf im using right now with @birchman suggestions and some “timeout + update interval” modifications added!

  - platform: ultrasonic
    trigger_pin: GPIO05
    echo_pin: GPIO04
    name: "Filter Tank VolumEsti"
    unit_of_measurement: "l"
    icon: "mdi:water"
    accuracy_decimals: 0
    update_interval: 5s
    pulse_time: 10us
    timeout: 20m
    filters:
      - filter_out: nan 
      - lambda: return 0.56 * 0.35 * (0.45-x) * -70.0 ;
      - sliding_window_moving_average:
          window_size: 10 
          send_every: 1 
          send_first_at: 1 
      - delta : 0.05 

Thank you @birchman

1 Like

The measurements are really bad.

I see now that you have set the max distance (timout:) to “20m”

Try to set it to 1m or like 50cm and see if it helps. I’m not sure that you can set cm. The maximum distance you are looking for is 46 cm right.

Maybe the sensor is broken or come kabel issue that are not obious.

/Mattias

The timeout is supposed to be some kind of delay in time I thought? Am I wrong?yes the bottom of the tank is 46cm (rubbermaid bin)
ill look for issues with the cables but i doubt that is the issue since the sensor+nodeMCU box is really close from the reservoir like less than 20cm…

I received a pressure transducer, Ill also try that.
if you or anyone have any suggestion for me I’m really open! :wink:

I’m really frustrated about this thing. I’ve already setup my secondary method using aquarium float switches, they work perfectly, but I wanted 1 main method and a backup because i do not want to flood my neighbours balcony!

anyways if you think of anything let me know!

The timeout is the maximum time that the sensor should wait on an echo to come back expressed in a distance (So the user doesnt need to calculate the number microseconds it takes for sound to travel the distance to the maximum distence back and forth.
In your case you have configure it to be able listen to echos that have travel 20 meters. Which is more than the senor can detect.
Because you are only interested to measure distances between 0-0.46m then you should lower that value to “1m” . I dont know how much it helps its all depending on the implementation.

Thats the only thing i can see from the configuration.

I suggested to check the cables or the sensor because we have soon checked all configuration.
Then its only cabeling or faulty hardware or bugs that we dont know about. But it easier to just to poke on the cabelng and see whats going to happen.

The pressure transducer I have not tried my self. I guess a analog out. The ADC in the esp8266 and esp32 is rather bad. So you need to filter it quite hard or use a ads1115 external adc.

/Mattias

I tried 50 cm, no change, tried 1m no change… will dismount the box today I have to add a sensor to it anyways. Ill keep you posted.

thanks for the help

I hope you find out what the problem is and it’s a easy fix.

/Mattias

ive had it working with "close to the good measure for 2 days but on restart due to power on/off i’ts now in the unknown state due to “timed out”

anyone ?