This is a bit of a niche post, but it might help someone to accomplish this in less time than I did. The tutorial will be as easy as it gets. Since the guys from ESPHome did a great job and almost everything you could wish for is already managed by ESPHome. I tried to keep it at a low budget total costs were around 4 dollars. After that, you will be able to define a range where motion should be activated and your board will send it to Homeassistant, where you can trigger all the automation you like.

So firstly, why would I not use a simple PIR sensor to motion activate my automation? I came across the problem of my PIR sensors constantly activating when I was too far away. So with an ultrasonic sensor, I am able to define actions for certain ranges and it just looks way cooler.

Required Hardware:

Used Software:

Wiring the hardware

And thats why I work in IT and not as an artist.

The ESPHome Code

Version 1. which sends the status to Homeassistant when the range conditions are met

This version allows you to update the sensor way faster, since we do not publish it to Homeassistant (the internal: true line). That way we are able to check way faster and only send on or off to Homeassistant via the GPIO platform when the conditions are met.

sensor:
  - platform: ultrasonic
    trigger_pin: D1
    echo_pin: D2
    name: "Ultrasonic Sensor"
    update_interval: 0.2s
    internal: true
    on_value_range:
      - above: 0.51 # You must have a below and an above part.
        then:
      - below: 0.50 # You must have a below and an above part.
        then:
          - switch.turn_on: usonic
switch:
  - platform: gpio
    pin: D3 # unused pin, just to make sure it's visible in HA.
    name: "Something is too close"
    id: usonic
   #This is needed to turn the sensor off, if you remove it, the sensor will switch on and stay on forever.
    on_turn_on:
          then:
           - delay: 10s
           - switch.turn_off: usonic

Version 2. constantly send the range data to Homeassistant

To be fair I did not test that entirely, but in order to react to motion you need to check a lot faster. And I am not sure how Homeassistant reacts to a flood of data. But if you are interested in constantly measuring the distance between your sensor and an object this is the way to go.

  - platform: ultrasonic
    trigger_pin: D1
    echo_pin: D2
    name: "Ultrasonic Sensor"
    update_interval: 1s #Or any interval you like

ESPHome

This tutorial will not be too detailed, because I expect you to already have an ESP installation at home or enough experience to install it on Docker or Hassbian.

If you are using Hassbian:

This is pretty much installing it in the addons page on Homeassistant.

https://esphome.io/guides/getting_started_hassio.html

If you want to use ESPHome on Docker use the following:

  1. docker pull esphome/esphome
  2. docker run -v /MyDockerVols/ESPHome:/config -p 6052:6052 -d esphome/esphome
  3. Connect to the website on port 6052

Flashing your Device with ESPHome

First, you need to download the ESPHome flasher, this needs to be done the first time. After that, we can update our devices Over the air (OTA).

  1. Add a new node on the website ( “+” on the bottom right)
  2. Follow the steps in the wizard.
  3. I will name my node ultrasonic
  4. Device type will ESP8266
  5. IF you have Wifi add your SSID and password in step 4. (Required for OTA Updates)
  6. Finish and submit
  7. After completing this hit edit on your new node, this is where we will put in our changes. Add the following to your code.
  8. Hit save and click on compile
  9. After successfully compiling the code you can now download the binary.
  10. Now head over to https://github.com/esphome/esphome-flasher and download the ESPHome-Flasher for your OS.
  11. Open ESPHome-Flasher and search your firmware binary.
  12. Hit Flash ESP

The board will now connect to your Wifi network and you can access it via Homeassistant.

Adding the sensor to homeassistant

This one is pretty self-explanatory but I like my tutorials complete 😀

  1. Open Homeassistant and open Integrations
  2. Search for ESPHome
  3. Add the IP of your device (It will be visible on the ESP Home terminal)
  4. Enjoy your advanced motion sensor