Tobias Dühr
Digitales Leben

Info: This tutorial is intended for operators of a Home Assistant server, but is potentially interesting for all hobbyists interested in technology.

tl;dr: Connecting a garage door opener to Home Assistant using ESP8266, relay, sound sensor and ESPHome.

Real Life Machine Interface Magic

It's a bit like magic when code interacts in the "real world". A perfect example is the transformation of an ordinary garage into a smart gate that can be controlled with a smartphone. With a few inexpensive electronic components and an ESP8622 developer board, anyone can create their own smart home project.

Initial Situation: Many Users, Few Keys

Our garage is used by 3 families - mainly to store the kids' driving equipment. It is opened electrically (after all), for this we have 2 remote controls, which have a very limited range and constantly empty batteries, and exactly one (1) key. The goal is to have all housemates be able to open and close the gate with their smartphone.

The Plan: Make Connection Smart

Fortunately, most electrically operated garage door openers can be assumed to have a simple circuit to connect a push button or key switch. Here, a connection is simply made briefly between two wires. That's exactly where we start and make this connection "smart".

To check the state of the gate (open or closed), we have to dig a little deeper into the bag of tricks. In this case, the easiest way was to check the position of the gate with an ultrasonic sensor. Depending on the situation, however, a pushbutton or a brightness sensor would also be a good option.

The Hardware: ESP 8622 as the Basis

An ESP 8622 developer board and a few inexpensive electronic components are used.

The short shopping list:

  • D1 Mini V3 NodeMCU ESP8266EX WLAN module
  • 5V single channel relay module
  • HC-SR04 ultrasonic sensor
  • 5V power supply (best to recycle an older one, we only need 500 mAh)

If and how the whole thing has to be packed and mounted depends again on the individual conditions of the garage. In our case the device is mounted below the ceiling, at the height of the guide rail of the garage door.

The Software: Home Assistant Meets ESPHome Plugin

A lot of positive things have already been written about Home Assistant. A particularly interesting connection for me is the ESPHome plugin.

Using ESPHome, relatively simple "recipes" for microcontrollers can be written, these are automatically compiled and distributed directly via WLAN.

The recipe for our microcontroller looks something like this:

  name: garage-door

  board: nodemcuv2

  - platform: status_led
    id: status
      number: D4
      inverted: true

  - platform: ultrasonic
    trigger_pin: D1
    echo_pin: D2
    name: "Ultrasonic Sensor"
    id: distance
    update_interval: 1s
    timeout: 2m
      - lambda: if (isnan(x)) {  return 2.1; } return x;
      - quantile:

  - platform: gpio
    pin: D3
    id: relay
    name: "Garage remote trigger"
    icon: "mdi:garage"
    restore_mode: ALWAYS_OFF
      - light.turn_on: status
      - delay: 500ms
      - switch.turn_off: relay
      - light.turn_off: status

  - platform: analog_threshold
    name: "Garage open"
    sensor_id: distance
    id: door_open
    threshold: 0.20
    device_class: GARAGE_DOOR
    publish_initial_state: True
      - invert:

  - platform: template
    name: "Garage Door"
    id: garage_door
    device_class: garage
    lambda: |-
      if (id(door_open).state) {
        return COVER_OPEN;
      } else {
        return COVER_CLOSED;
        - switch.toggle: relay
        - switch.toggle: relay
mehr anzeigen

This configuration already includes error handling routines for the inaccuracies of the ultrasonic sensor and the definition that it is a garage door. This is important so that the device is not displayed in Home Assistant as just a switch and sensor.

Depending on the situation, the code may still need to be adjusted: Our garage door moves under the ceiling when opening and covers the ultrasonic sensor, so if it measures a distance of less than 20 cm, the door is open, anything above means it is closed.

After successful upload of the firmware, the new device can be added in Home Assistant.

The Result: PWA, Ready, Go!

Once added to Home Assistant dashboard, I now have the ability to open and close the door worldwide.

To provide easy access to everyone in the house, I also programmed a small web application. This can be installed as a PWA on common smartphones even without an AppStore.

Conclusion: Successful Home Automation with Minor Pitfalls

All in all, the described method is a very cost-effective way to make an existing device "smart". However, there were a few stumbling blocks, frankly speaking:

  • When the gate is half open, it is displayed as "closed"
  • The fine-tuning of the sensor was unexpectedly complex
  • Whether the electronics are weatherproof enough remains to be seen

For this use case, however, these are points we can live with. In any case, I learned a lot in this little project, especially that code that interacts with the real world, such as querying an ultrasonic sensor, requires a high level of tolerance and error handling routines, since dust or insects can falsify the result - this happens rather rarely in a computer.