This one’s pretty simple, but I thought I’d share because it took me a while to piece together from various other instructions across the forum.
I wanted to start quantifying whether I’ve left home with my vehicle or not. I often leave on foot or bike, so it’s just one of those “nice to know” things for me. For others, they may want to run different automations depending on if vehicles are present or not.
Shopping list:
Product | Count | Cost | Cost per unit |
---|---|---|---|
Wemos D1 Mini (with unattached headers) | 5 | $17.99 | $3.60 |
HC-SR04 5V ultrasonic distance sensor | 5 | $8.98 | $1.80 |
Plastic project boxes 3.94" x 2.36" x 0.98" | 10 | $11.99 | $1.20 |
Micro USB cables | 5 | $8.97 | $1.80 |
5v USB wall charger | 5 | $10.79 | $2.16 |
Total cost per distance sensor: $10.56
Previously owned, but necessary or used to make life easier:
- Soldering gun w/ solder
- Breadboard (to make soldering easier)
- Drill and many various size bits
Software required:
- Esphome
- Home Assistant
Steps
- Flash your D1 mini with the configuration below. Make sure you change values as work best for your setup.
- Drill out your project box. For the distance sensor, I wanted just the trigger and echo cylinders sticking out. I first traced an outline of them, then drilled a small pilot hole through the center of the marking, then used a 5/8 inch “spade bit” to drill out a perfectly sized hole for each cylinder. Finally, I did something similar to make a hole for my mini USB cable to power the D1
- Hook it all up. I used a hot glue gun and slowly built up pads to attach the D1 to the project box in the corners near the USB port (where there aren’t any pins). Then I inserted the cable and plugged it in. The HC-SR04 just slid right into the drilled holes and didn’t require anything to stick it down.
- Then I closed up the box and mounted it above my vehicle in my car port.
- Add it to Home Assistant. Go to
Configuration
, thenIntegrations
. Your device should be auto-discovered. Add it to HA. - Bonus: Make a binary_sensor presence detection. Edit your
configuration.yaml
and add a template platform binary sensor. Make sure you change the entity ID and distance accordingly in thevalue_template
. For me, if the distance is less than 2 meters, the car is present.
binary_sensor:
- platform: template
sensors:
car_presence:
friendly_name: Car presence
device_class: presence
value_template: "{{ states('sensor.distance_00_ultrasonic_sensor')|int < 2 }}"
Pictures (sorry for blur)
Esphome sensor yaml configuration
esphome:
name: distance_sensor_00
platform: ESP8266
board: d1_mini
# Without wifi, this won't do anything
wifi:
ssid: <redacted>
password: <redacted>
# Enable Home Assistant API
api:
# Over the air updates
ota:
sensor:
- platform: ultrasonic
trigger_pin: D6
echo_pin: D7
name: "Distance 00 Ultrasonic Sensor"
update_interval: 5s
filters:
# Configure this and the above update_interval to your needs
# Lower window sizes means you're going to get a lot more data
# Higher sizes means smoother graphs and less noise
- sliding_window_moving_average:
window_size: 30
send_every: 30
send_first_at: 30
# Not required, but it's nice to know that my wifi signal is strong
- platform: wifi_signal
name: "Distance 00 Wifi Signal"
update_interval: 3s
filters:
- sliding_window_moving_average:
window_size: 30
send_every: 30
send_first_at: 30
Note that I only hooked one distance sensor up to my unit. It would absolutely possible to add 2 or more, depending on the number of usable GPIO pins on the board that you use. From that, you could sense for different vehicles by running the sensors further from the ESP board