Control Your Automatic Sliding Gate With Home Assistant and ESPHome

1,657

10

5

Introduction: Control Your Automatic Sliding Gate With Home Assistant and ESPHome

About: Curious guy starting one thousand projects and finishing one every ten years.

The following article is some feedback on my personal experience controlling the automatic sliding gate that I had installed on my house. This gate, branded "V2 Alfariss", was provided with a few Phox V2 remotes to control it. I also have a Google Nest Hello doorbell, which unfortunately cannot be plugged into the sliding gate controller to remotely open the gate from the mobile app. For me, one way to solve this limitation was to find a way to connect the automatic sliding gate controller to the Internet. With this new, connected gate, I can answer use cases like controlling the sliding gate with my mobile phone. I achieved this by using Home Assistant, ESPhome and a few electronic parts.

The idea behind this article is not to give you something ready-to-use, but rather to inspire you. Remember, if you don't have the exact same automatic gate, don't forget to download and read the technical documentation for your model. Adapt it and improve it. Warning: Be careful and don't forget to switch off the electrical power before opening the main controller. Enjoy!

Supplies

  • Tools :
    • Screw driver
    • Soldering iron
    • Multi-meter
  • Parts :
    • Raspberry PI 3 (full set : 2A alim + 32gb sd card)
    • ESP8266 Wemos D1 mini
    • 2 relay modules
    • 2 resistors 10k
    • PCB for prototyping
    • Wires

Step 1: How Does It Work?

The main challenge here is to connect the automatic sliding gate to a smart phone. To do this, let's turn the automatic sliding gate into an IOT device. Of course, there are multiple ways to achieve this. In my case, after some experiments, I decided to go with :

  • Using Home Assistant as a hub for controlling the portal and receiving requests from a user.
  • Installing ESPHome as the firmware inside the ESP8266.
  • Electronic parts :
    • A prototype PCB to solder components and connect them
    • Two relays to simulate push button to open or close the gate
    • An old 5V power supply to power the ESP8266
    • Two resistors to divide voltage from the open/close sensor
  • An Internet connection with a router (need NAT rule configuration feature)
  • A DuckDNS account to allow the name resolution to your Home Assistant
  • A mobile phone to install Home Assistant App and a widget on screen

The flow

Look at the schema to better understand.

  1. From you mobile phone, you push on the Home Assistant mobile app widget
  2. A request is sent to your Home Assistant Web address (resolved by DuckDNS and TLS by let's encrypt).
  3. Your Internet router route the request to the Home assistant application
  4. Home Assistant send the request action to ESPHome
  5. ESPHome trigger the Automatic Sliding Gate motor
  6. Champagne !

Step 2: Install and Setting Up Home Assistant on Raspberry PI

Home Assistant can be seen as a hub for all your home IOT stuff. It will be a good place to control all the connected devices and add a lot of features. The main features that I like the most are the dashboard, the API and the diversity of add-ons.

Installation

I clearly don't have any added values to told you how to setup and install Home Assistant. In fact, before this project I didn't know this software. The key articles are :

After all these points, you have a nice Home Assistant running on your Raspberry PI. You must be able to reach it through HTTP and a local IP or try http://homeassistant.local:8123.

Configuration

Configure DuckDNS with you domain and with the token. For example, you can see in the pictures, a piece of my configuration. Don't forget to accept_terms with "true".

lets_encrypt:
  accept_terms: true
  certfile: fullchain.pem
  keyfile: privkey.pem
token: 92f56bb2-2c26-4802-8d4d-xxxxxxxxxxxx
domains:
  - nameofyourchoice.duckdns.org
seconds: 300

I decide to configure HA with a static IP because it is simpler and you are sure that the IP will be the same after a reboot and then your port forwarding rules will continue to work : https://github.com/home-assistant/operating-syste...

In my case, I add a piece of configuration inside the configuration.yaml file because DuckDNS doesn't manage the https, just the let's encrypt certificate and the DuckDns update :

http:
  ssl_certificate: /ssl/fullchain.pem
  ssl_key: /ssl/privkey.pem
  base_url: https://nameofyourchoice.duckdns.org:8123


Step 3: The Custom Electronic Board to Enhance the Automatic Sliding Gate

Maybe, it was the most unusual part for me because I am usually more tweaking softwares stuff than hardwares. I have started with a breadboard and a very basic circuit, just to check that I am able to upload a program inside the Wemos D1 mini and to blink a LED. Then, I have changed the firmware for the ESPhome and follow this perfect getting started article : https://esphome.io/guides/getting_started_hassio.html

After some iterations to find the right circuit, I have documented it with Fritzing. Be sure to spend times on this step as there is no going back when everything is soldered (not exactly but not easy rollback). I have tried to simulate input sensor from Automatic sliding gate but this was a failure (I explain later the why).Personally, I found that Fritzing is a good tool to document what you wire on the breadboard.

Note : You'll find a good starting point here in ESPhome FAQ if you cannot plug your ESPHome to Home Assistant for the first initialization. After the firmware is uploaded, you will be able to upload "over the air" (OTA).

Relay to activate open/close trigger

In the automatic sliding gate technical specification, it is specified that you have to close a circuit between "START" and "COM" to trigger an action open/close the gate (the full opening). A close circuit between "START.P" and "COM" trigger an action open/close the gate for pedestrian. I didn't use the "STOP" but this is the same concept but to stop the gate when opening or closing.

On the electronic component choice, I have decided to go for a relay instead of a transistor. Transistors are nice but they doesn't provide a guarantee that the circuit is perfectly close. I think that they can let some very low current going in the circuit. Another benefit of relay is that when you test your circuit, you basically hear when the contact is close with the sound "clic".

Get the open sensor state

At the beginning, I was thinking of using some magnetic sensors to detect the gate open or close states. But I have realized that there is a plug which mention "limit sensor" on the controller. Clearly not made to be used (by geek like me), I found with a contact tester that I can read the state open or close when I open the gate. I have made a big mistake thinking that the state was some kind of TTL logic voltage (3.3v). In this case, you can directly plug them to Wemos Input. But in fact, when the gate is opened, there is a 6.3v output voltage. To use this signal, you must divide the voltage with this kind of circuit http://www.learningaboutelectronics.com/Articles/H... The solution is pretty much straightforward because the voltage need to be divided by two. So, I have used two 10 Kohms resistors between the limit sensor output and the Wemos input (again, look in the attached pictures for a schema).

Cheap 5v power supply

To power all these components, I use an old mobile phone power supply. I opened it and changed plug to small plug cable with screw. I also decided to merge the two pcb (esp and power supply) with hot glue (yes I know, this is a bit dirty but it works :-). Not the cleanest thing ever but easier to handle and avoid touching the 220v.

That's all for the hardware part.

Step 4: ESPHome Firmware to Run on the PCB Prototype

The logic inside the ESP8266 is inject through Home Assistant portal ESPHome Add-on. You code the logic that the ESPHome execute.The very small kind of application must be code with ESPHome syntax. It is really easy and it's fun because, just with few lines of YAML, your ESP8266 become quickly a smart thing. The full source code is available here : https://gist.github.com/toomone/819112ea1b04937912... Lets focus on the important part of the code.

Controlling the relays

As I said previously, the automatic sliding gate controller can trigger an open or close action with just a short impulse (close circuit) between two inputs. To do that, you need to configure a switch section with little configuration. For example, lets focus on the part that activate (open or close) the partial opening of the gate (needed for a pedestrian for example).

switch:
  - platform: gpio
    pin: D3 // the PIN number where the signal will be output on the Wemos
    id: relay
    restore_mode: ALWAYS_OFF 
  - platform: template
    name: "Gate Pedestrian Remote"
    icon: "mdi:walk"
    turn_on_action: // the logic to be execute by the ESPHome to simulate a pulse
    - switch.turn_on: relay
    - delay: 500ms
    - switch.turn_off: relay<br>

The previous code will generate a switch and a template. These two concepts allow ESPHome to offer some enhanced mechanism without real coding. I invite you to read ESPHome documentation to deepen on the features offered. https://esphome.io/cookbook/relay.html and https://esphome.io/components/switch/template.html...

Reading open close sensor state

binary_sensor:
  - platform: gpio
    pin: 
      number: D1
      inverted: true
    name: "Open sensor"
    id: open_sensor
    device_class: garage_door

This part of the script tell the wemos board to read the state on the open sensor on D1. To find D1, you just to read on your Wemos PCB. I have used the "inverted" parameter to true to invert the signal value. I don't remember the clear reason but I think that it was simple to display close or open accordingly to the portal state on the HA dashboard.

Allowing API call to ESPHome

If you want to be able to use the Home assistant widget on your phone, you need to add this small piece of code:

# Enable Home Assistant API
api:
  services:
    - service: open_portal_pedestrian
      then:
        - switch.turn_on: relay
        - delay: 500ms
        - switch.turn_off: relay

This way, the Home assistant app configurator will list the action to open the portal.I have chosen only the one for pedestrian because this the one I use the most.

Step 5: Expose Your Home Assistant to the World

Because, you want to be able to open your Automatic Sliding Gate from wherever you are on the planet and not just from you home, you have to expose your HA to the world. By the way, be sure to use a strong password authentication. On your Internet router you have to configure a rule to route all the traffic coming from a specific port to a specific IP and also a port. For example, you will find the configuration I have done on my provider router (sorry, it's in French) but you will notice that it's really simple configuration. The rule must tell your router to accept all protocol on port xxxx to go on Home Assistant IP with port 8123 (if you didn't change it).

That's all. Don't hesitate to ask questions because I certainly forgot to add details on specific part. The system is working everyday without problem. I also like the fact that I can have the state open or close of the portal from my phone.

Be the First to Share

    Recommendations

    • Backyard Contest

      Backyard Contest
    • Fruit and Veggies Speed Challenge

      Fruit and Veggies Speed Challenge
    • Fandom Contest

      Fandom Contest

    5 Comments

    0
    stano.gustafik
    stano.gustafik

    3 months ago

    Thomas, thank you for sharing this project. I am planning to integrate Alfariss in my Home Assistant. But instead of Wemos I am going to use Nodemcu ESP32.. please, do you think I need to use both resistors 10k to connect Open Sensor to Nodemcu? And.. is it necessary to use 10k? Would be 4k7 enough? Thank you...

    0
    Thomas Cousin
    Thomas Cousin

    Reply 14 days ago

    Hey, sorry, just see your message ... I don't know Nodemcu ESP32 but i am pretty sure that it cannot handle the output voltage from the motor which is not TTL standard (confirmed here https://learn.sparkfun.com/tutorials/esp32-thing-hookup-guide/hardware-overview : "The I/O pins are not 5V-tolerant! If you interface the board with 5V (or higher) components, you'll need to do some level shifting."). I think 4k7 is also ok to divide the current (maybe this need to be checked as I am not electronic expert). Cheers.

    0
    Automaticdoorvn
    Automaticdoorvn

    7 months ago

    It very helpful for my business. I think it will be famous in short time.

    0
    Thomas Cousin
    Thomas Cousin

    Reply 7 months ago

    I'm glad it can help you.