Car/automobile presence detection

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:

Steps

  1. Flash your D1 mini with the configuration below. Make sure you change values as work best for your setup.
  2. 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
  3. 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.
  4. Then I closed up the box and mounted it above my vehicle in my car port.
  5. Add it to Home Assistant. Go to Configuration, then Integrations. Your device should be auto-discovered. Add it to HA.
  6. 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 the value_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

2 Likes

Looks good!

I did basically the same for my non-MyQ garage door operating system. I use the US sensors to measure the distance to determine if the garage door is open or not (if < 25 then the garage door is open).

Do you have a link to those project boxes?

Whilst I appreciate your work on this, why not simply get:

  1. An opengarage: https://opengarage.io/
  2. An ODBII vehicle tracker to use with Traccar (Like this: https://www.aliexpress.com/item/32793306073.html?spm=a2g0o.productlist.0.0.b199a95fa8QINC&algo_pvid=84c816c3-1487-4ae1-92b1-95d1c30b5235&algo_expid=84c816c3-1487-4ae1-92b1-95d1c30b5235-2&btsid=84fb63b6-94f2-4734-baff-d7c93dfa2aa7&ws_ab_test=searchweb0_0,searchweb201602_6,searchweb201603_55)

Both have availalble official plugins to HA…

I just wanted to know what advantages this has over the above…?

About $60 less in parts and fun to put together.

1 Like

I got them off of Amazon: https://amazon.com/gp/product/B07G8S6XLV/ref=ppx_yo_dt_b_asin_title_o04_s00?ie=UTF8&psc=1