Need help with HC-SR04 as tripwire - I would like it to return only if distance changes radically

Hi,

I’m working on a project to control 15 RGB LED strips that will be installed in a staircase. I’ve got that part working just great, so it’s time to work on the trigger mechanism.

Right now I’m thinking of using HC-SR04 ultrasonic sensor as the trigger. It’ll measure the distance from the outside of the bottom stair to the wall (about 1m), and if a person steps on it, it’ll measure less than 1m. But I would only like to let HA know if that happens. The refresh rate is going to have to be high to be effective, but I don’t want to pound HA with every single reading.

Can this be done with a Lambda? Or a switch? or something? Any advice welcome.

No need for lambdas, use the on_value_range automation:

In your case you probably only want to define the below option.

1 Like

I use these for car sensors. Set up a template binary sensor, and read the ultrasonic every second. Remove the name from the ultrasonic sensor and remove it from HA. The logic will be done by the ESP, and it won’t be spamming HA every second. My binary sensor looks like this

binary_sensor:
  - platform: template
    name: "Ford Escape"
    lambda: |-
      if (id(northsensor).state < 1.5) {
        // car is home.
        return true;
      } else {
        // car is not home.
        return false;
      }

“northsensor” is the ultrasonic sensor, replace that with the correct ID and change the math to work.

1 Like

Thank you @NK553, that’s exactly the sort of example I needed. Should be no trouble from here. You’re a top bloke. Top.

So you completely ignored my advice that lambdas aren’t needed?

sensor:
  - platform: ultrasonic
    trigger_pin: D1
    echo_pin: D2
    name: "Ultrasonic Sensor"
    on_value_range:
      below: 0.9
      then:
        - switch.turn_on: relay_1

Honestly, sometimes I don’t now why I bother.

2 Likes

Hi @tom_l, I did heart your post, your solution was good. However, while you pointed me in the right direction, you didn’t provide a code example I could understand. Moreover, your post appeared to be missing something, in that you referred to “below option” when there wasn’t one.

It was only when you got upset with me that you offered a code example that someone of my limited intelligence could understand.

Also, if you compare the two examples posted in this thread, they both make different assumptions. Yours assumes I want to keep all the automation inside ESPHome. Brian’s assumes I just want a trigger. Both approaches are valid, but different.

Anyway I hope you feel better soon. You are a valuable resource and you have helped me in the past, but you are grumpy and terse and it doesn’t help you. I’m sorry if that upsets you even more, I don’t mean to hurt your feelings. I think it might help if you remember that the reason people ask for help is because they are not as smart as you are - I know I’m not.

From the link I included (that had a clear example)
Screenshot_2020-05-10 Sensor Component

A reasonable assumtion considering your original statement:

You ignored this part

it’ll measure less than 1m. But I would only like to let HA know if that happens.

He didn’t say anything about the sensor controlling the output directly. If that was the case, why would HA be necessary at all? By separating the sensor from the switch, he can add logic in HA, you know, don’t turn the lights on in the day time…

It was an example. The controlled device can be anything. A template binary sensor for example. Or even a binary sensor in home assistant.

Why needlessly complicate things with lambdas when the function is baked in?

1 Like

Guys please don’t fight on my account. @NK553, this is just what @tom_l is like, it’s nothing personal.

@tom_l, you’re quite right, that’s a great solution, if you want to keep the automation inside ESPHome. As it happens, I’d prefer to do it in Node-RED, because I have layers of complexity that I think would be more difficult in ESPHome, such as setting the colour temperature of the LEDs based on actual LUX levels of sensors not attached to this ESP, and also making the LEDs red after people have gone to bed, so that anyone using the stairs after lights out won’t get their eyeballs attacked by the stairs. This being so, the lambda method seems to work better for my use-case, but under the circumstances, I can see that I didn’t give enough information in my original post.

Thank you for your contribution in any case. Anyone who finds this thread via search will be very well served.

1 Like

How did the ultrasonic sensor work out for you as a tripwire? Thinking of doing something similar.