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.
- NodeMcu V3 ESP8266 with a CH340 Wireless Module (This is an affiliate link Aliexpress)
- Extension Module for ESP8266 from above
- HC-SR04 Ultrasonic Module (This is an affiliate link Aliexpress)
- 1x Breadboard
- 4x Jumper Wires M/M
Wiring the hardware
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
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.
If you want to use ESPHome on Docker use the following:
- docker pull esphome/esphome
- docker run -v /MyDockerVols/ESPHome:/config -p 6052:6052 -d esphome/esphome
- 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).
- Add a new node on the website ( “+” on the bottom right)
- Follow the steps in the wizard.
- I will name my node ultrasonic
- Device type will ESP8266
- IF you have Wifi add your SSID and password in step 4. (Required for OTA Updates)
- Finish and submit
- After completing this hit edit on your new node, this is where we will put in our changes. Add the following to your code.
- Hit save and click on compile
- After successfully compiling the code you can now download the binary.
- Now head over to https://github.com/esphome/esphome-flasher and download the ESPHome-Flasher for your OS.
- Open ESPHome-Flasher and search your firmware binary.
- 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 😀
- Open Homeassistant and open Integrations
- Search for ESPHome
- Add the IP of your device (It will be visible on the ESP Home terminal)
- Enjoy your advanced motion sensor