MQTT Car Presence


Automating your garage doors makes a lot of sense.  I’ve done two videos already on how to make your garage doors more smarter. In this video I’ll show you how to use a D1 mini, in your car, to have your garage doors opening and closing when you need them to. The best part is you don’t have to ever think about them or touch anything.  Because sometimes, pushing a button is just so hard….

All the credit for this idea goes to aderusha. He’s a lvl 40 automation wizard. He’s got some cool projects on github.  This one, the MQTT-Car-Presence was even featured on Hackaday. And, thanks to John Boehm for showing it to me.  Now, here’s how it works.

In aderusha’s instructions he uses a D1 mini pro with an external antenna to extend the range. I used a plain D1 mini and it works great.  I’m going to talk about the range a little more at the end.

Ingredients:

  • D1 mini (or Pro w/ antenna)
  • Micro USB to Male type 2 USB connector
  • Some kind of container
  • Arduino IDE – https://www.arduino.cc/en/Main/Software
  • And of course – Home Assistant

aderusha has a box to 3d print. There are others on Thingiverse. Or you could use something like a TicTac box.

Head to aderusha’s github and download everything: https://github.com/aderusha/MQTTCarPresence
Extract it and you’ll find MQTTCarPresence.ino Open that with the Arduino IDE.  There are a few things you need to customize here. Wifi SSID/PW, MQTT IP, user/pw, and MQTTNode.  If you’re going to make more than one of these the node needs to be unique.  I’m not using OTA update, and you don’t need to change the Discovery Prefix.  When you’ve made those changes, connect your D1 mini and set your tool settings like this:
Arduino IDE Tool settings

If you’ve flashed a sonoff before then your pubsubclient library should be ready to go.  If what I just said makes no sense to you, then you’ve got one more step before you’re ready to upload this sketch to your D1 mini.

These are the libraries this sketch uses:
Required Libraries

The one called PubSubClient needs a small modification. Find the file, it’s probably in your Arduino/Libraries/PubSubClient/src folder. Open it with VisualStudioCode or NotePad++, and change the MaxPacketSize line to 512.  Again if you’ve flashed Tasmota on a Sonoff, this should already be done and you can skip that step.

Now you should be ready to upload the sketch. Connect your D1 mini, select the right port (the one that showed up when you plugged in the D1 Mini), and hit upload.

I printed this tight little case, and I’m using this really short Micro to Male type 2 USB cable.  Now before we go plug this in and test it out, we need to set it up in Home Assistant.

Open your configuration.yaml file to add “discovery” and “discovery prefix” to our MQTT section, like this:

mqtt:
  discovery: true
  discovery_prefix: homeassistant
broker: your.HA.pi.IP
port: 1883
client_id: home-assistant-1
username: yourUSER
password: yourPW

After that, restart Home Assistant and plug in your D1 Mini. After a few seconds the blue LED will start flashing like crazy. This sketch is genius. It adds itself as an entity to Home Assistant.  Go to your states page and you should see a new “binary_sensor.CarPresence”.  Once you see that, you’re ready for the next step.

Now, we add two automations.  I like using the Automation Editor. It gives you some guidance, so for me at least, I make less mistakes when I use it.

If you want to do the automations the old fashion way here you go:
Automations in yaml format

Make sure the entity_id matches your “cover” (garage door) entity in HA, and the “binary_sensor” matches the MQTTNode from your sketch.  The first automation is triggered when the D1 connects, which happens when you turn on your car, or when you get home from some voyage hither. If HA sees the D1 is on and the garage is closed it will open it.
The second automation is triggered when the D1 is off. Which happens when you drive out of your network range, or when you just pulled in the garage and turned the car off. It will check the state of the garage door and if it is open it will close it. If you have 2 garage doors, you’ll need 2 sets of these automations, and the entity_id for the cover and the binary_sensor will be different.
Reload the automations, and go install the D1 in your car. It’s important that you plug the D1 into an outlet in your car that turns off when you shut down the car. Not every outlet in your car will do that. So with the car off, plug the D1 in. If it turns on when the car is off, then find another outlet. When you’ve got the right outlet it’s time to test it out.  Unfortunately, it is very unlikely that anything could explode in this case.

For us, I think the non-Pro D1 is the right one.  With our router, we have to be pretty far away for our garage door to close. By the time it closes we’re out of site of the door, which isn’t great because we can’t be sure the door actually closed.
Other than that, it’s working perfectly. Another High Score on the WAF board.
D1 Mini Pro w/ Antenna from BangGood.com
D1 mini (non-Pro)




Social Links

Menu