Chicken Coop Food Sensor not working well

In my chicken coop I have builded a long pipe filled with grains, the chickens eat from the bottom. I figured I would install an ultrasonic sensor (HC-SR04) and measure the distance from the top of the pipe to the top of the grains. It’s not working well because the measured distances change :frowning_face:

  - platform: ultrasonic
    trigger_pin: D5
    echo_pin: D6
    update_interval: 60s
    id: distance_sensor
    name: "Tau7 Food Sensor"
    filters:
      - median:
         window_size: 7
  - platform: template
    id: level
    name: "Tau7 Food Level"
    unit_of_measurement: '%'
    update_interval: 60s
    lambda: |-
      auto r = (id(distance_sensor).state - 0.41) * (100.0 - 0.0) / (0.13 - 0.41) + 0.0;
      if (r > 100) return 100;
      if (r < 0) return 0;
      return r;

The problem is that when empty the sensor measures between 0.43 to 0.36 (big differences) when full it shows between 0.10 to 0.13 (the variation when full can be normal, as it can have a little more or little less grains, besides when full the variation is little, when empty the variation is bigger).

Also when half full, the sensor sometimes will change the measure, so it can show 0.25, and suddently a few minutes later (not chickens involved) it will show 0.20 and then later 0.24 or 0.26…

you see the pattern. It’s the first time I’m using an ultrasonic sensor, but it seems very unprecise measurements, so I wonder what I’m doing wrong. I played with the filters, but I don’t understand how to use them properly, so pretty sure it’s not okay.

Please advise. Thanks a lot.

Did you consider the time factor when you did your measurements? Your code outputs median of values over 7minutes.
Does it give better results if you set update intervals to 2s and window_size to 5?

Also, sensor’s wide beam of view will not work in narrow pipes, as it will get reflections from pipe walls.

Have you considered using a scale under the pipe?

I don’t understand well how it works for median and window size or stuff like that, what should I put to make it work better ? I don’t need readings very often, just a way to know when the food is getting low.

I don’t have a scale sensor, besides the pipe is attached to the wall, so it would be complex to make it vertically movable with a scale under it.

I have a couple different ultrasonic sensors for my sump pit and softer salt and the one’s I’ve tested are noisy like this as well. You can look into filters for esphome to try to get more consistant numbers, but it will delay changes.

It’s ok to have long interval and large window in final setup. But when you are testing it, it might be confusing since any changes to the level can affect the readings for next 7 minutes.

The problem I’m having it’s that the sensor gives me 0.43 and 10 min later 0.36 (it’s getting emptyer ? but it was already empty) and a while later 0.41 (what ?? it magically got filled up ???). There was no food in the tube all along…

so the sensor seems to be not acurate nor precise… :frowning:

Ok, that’s definitely not a problem that can be resolved by filters.
How is your sensor wired / powered?
Is it well fixed in place?

yes, well fixed in place, powered by a small usb phone charger, it power the wemos and the sensor with 5v. It’s outside and it’s cold (-2C today) well protected from rain and humidity, not exposed to elements. I have other HC-SR04, maybe I will swap the sensor itself in case it’s the sensor who doesn’t work well.

You need voltage level shifter between Esp and sensor that is powered at 5V.
Is that sudden drop at 4pm something that happens every day?