Laziness triumphs again (or how I automated the opening of my electric gates from my car without a remote) Published on April 19, 2022 Edit article View stats Giordy SalviStatus is online Giordy Salv
I recently had electric gates installed for our house. They're great as you can open them with a remote or using the keypad. But in today's world of automation, that's a bit old fashioned and is a real pain when I leave the house, lock the door, get in the car, start it, manoeuvre to get out and then realise I've forgotten the remote in the house - do I go back in the house, which means switching the car off (as the house key is on the car keys), getting out of the car, unlocking the door, digging out the remote (which the wife, although more likely me, has left in a coat or trousers I have to find, probably by retracing my steps from the other day which I've completely forgotten), re-locking the door, getting back in the car, restarting it and finally opening the gates. Or do I use the keypad, which is on the passenger side of the gate which means another manoeuvre as I'm too close to the wall to open the car door!
First world problems I know!
Surely there's a better way I thought. And of course there is (and there are undoubtedly many). For me, it involved my RaspberryPi Home Assistant (HA) system (which automates my home - see my other articles), a couple of pieces of hardware and some simple "programming".
The requirement
I wanted an approach that:
would notice someone starting the car (and so most likely intending to leave)
would then open the gates in an acceptable time
and ideally open the gates on returning home
Of course any solution would have to adhere to open standards!
My solution
After some detailed research (a few minutes Googling), I settled on the following solution: I'd have a small device plugged into a USB port in my car (which is only live when the ignition is on). My Pi running HA would continuously ping this device and, on detecting it (when I turn the ignition on), send a command to open the gate. To have a command open the gate would need a change to the gate controller.
Of course once the car drives off, it is quickly out of range and so my HA will no longer detect it. This means on its return, it comes back into range, is detected by the Pi and so appears to HA exactly the same as the ignition being turned on and, lo and behold, the gates open.
Well that's the theory!
In my solution, all of this occurs in an acceptable time and so satisfies all my requirements. Moreover, I can also control the gates remotely using the HA app - a bonus! I have a button on the front page of the app to allow me to open the gates quickly, such as when I'm walking home. A quick tap and they're open.
These are the components I've used in my solution, along with some further detail.
ZigBee smart relay switch
The key component is a relay switch that supports the ZigBee protocol (shown at the start of this article). I got this one from Amazon for about £18.
I had it installed in the controller of my gates. Once added to my ZigBee network, my HA system can send it an "open gate" command. This causes the relay to close for about a half second, which is enough to instruct the gates to open.
As it is just another ZigBee device (bizarrely it appears as a light!), it can be used throughout HA - in dashboards and automations for example.
Mini wireless D1 module
This is the device that plugs into the car's USB port. All it has to do is respond to ping messages from my HA's RaspberryPi. I actually bought a couple of SP-COW 2 Pcs ESP8266 ESP-12F Mini Modules 4M Bytes WLAN WiFi Internet Development Board for Arduino from Amazon, dirt cheap at only £7 for them. I also bought a case and USB-A to micro converter so it fits into the car's USB socket nicely.
Programming
A little "programming" was necessary although none of it was complex (and Google, as every, is your friend!).
Using the Arduino IDE (beyond the scope of this article although not rocket science), I programmed the D1 mini to connect to my home's WiFi. I configured it on my router to have a static IP address as I didn't want, say, a visitor's phone accidentally opening the gates!
I then configured a binary sensor in HA, Car sensor, that is on if a ping to the board is returned, with pings tried every second.
I used Node Red in HA to toggle the ZigBee relay switch if the Car Sensor changes to on. Although I used a toggle to close the relay, it is reset to open after half a second so toggle works every time. A similar toggle effect is used in my HA dashboard button to open the gate (the gate in the button turns yellow when the button is tapped and goes back to dark half a second later).
Closing remarks
This solution works as well as, if not better than, I could have hoped.
HA is continuously trying to ping the D1 mini in the car. The D1 mini only replies when the car is turned on, following which HA opens the gate. Once the car has left, HA no longer sees the D1 mini so is ready to open the gate next time it sees it. That happens on the car's return - the D1 mini is back in range of my WiFi, the D1 mini responds and the gate opens as if by magic!
Moreover, I can now control my gates remotely from my (and my wife's) phone. No need for a relatively expensive SIM-based solution and, as it all uses open standards locally, much more secure.
I'm now thinking of what else I might automate in a similar way - suggestions welcome!