With the Sonoff Basic relay, magnetic door sensor and ESPHome, I'm got a full integration of automatic gate for Home Assistant, HomeKit and Google Home, with actual state displaying.
Unfortunately, due to the war with the Russian fascists, which caused a humanitarian catastrophe in my town, I cannot shoot a actual video. That's why I'm referring on the video taken a few years ago.
Watch on youtube:
Automatic gates, opened by a radio key, usually have the GPIO contacts on control board for alternative opening control. Short close/open of contacts leads to opening of gate if they were in the closed state, closing of gate if they were open, and stop if the gate are opening or closing.
I'm modified Sonoff Basic to a relay with "dry" contacts, which allowed me to use GPIO on the gate control board. Video about modifying Sonoff Basic to a relay with "dry" contacts. And also, I'm was connect 'RX' contact of Sonoff Basic to the magnetic opening sensor, to obtain the state of the gate.
Below is a photo of a relay with a magnetic door sensor on a three-meter long cable, before mounting on the gate. And the door sensor mounted on the gate.
Config file | Description |
---|---|
gate.yaml | ESPHome config file |
If you don't know how to flash Sonoff, read this article at esphome.io
If you want to buy the ready for installation relay, flashed with a gate ESPHome config file, and with magnetic sensor, please email me. I will be very grateful for you support.
Wi-Fi connection is implemented as follows:
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
The corresponding parameters are set in the secrets.yaml
file of the working directory (where the configuratuion.yaml located)
In order to connect controller to your network, you need to create the secrets.yaml
file in the working directory and set the following parameters:
wifi_ssid: you_wifi_ssid
wifi_password: you_wifi_password
Then on the ESPHome page in the upper right corner you need to open the SECRETS window and enter the next line:
<<: !include ../secrets.yaml
Except the software gate control, configuration file describes the physical button of the Sonoff relay. A short press on it will result opening, closing or stopping the gate, depending on the initial state. The state of the gate is determined by binary_sensor.gate_open_sensor
, to which the magnetic door sensor is connected. It has a 2 second delay to be set to 'on' state. This delay is introduced to filter out accidental triggers from gusts of wind.
binary_sensor:
- platform: gpio
pin:
number: GPIO0
mode:
input: true
pullup: true
inverted: true
name: "Gate Button1"
internal: true
on_press:
then:
- switch.turn_on: relay
- delay: 1s
- switch.turn_off: relay
- logger.log: Gate Button1 Pressed
- platform: gpio
pin:
number: GPIO3
mode:
input: true
pullup: true
name: "Gate Open Sensor"
id: "gate_open_sensor"
device_class: door
filters:
- delayed_on: 2s
The gate entity cover.gate
is implemented using the template button gate_button2
, hidden from the Home Assistant interface. The template button in the on_press
script, close relay with "dry" contacts for one second. In turn, relay connected to the GPIO gate control board. The cover.gate
template gets its state from the binary_sensor.gate_open_sensor
, that wath described above.
button:
- platform: template
name: "Gate Button2"
id: "gate_button2"
internal: true
on_press:
then:
- switch.turn_on: relay
- delay: 1s
- switch.turn_off: relay
- logger.log: Gate Button2 Pressed
cover:
- platform: template
name: "Gate"
device_class: gate
lambda: |-
if (id(gate_open_sensor).state) {
return COVER_OPEN;
} else {
return COVER_CLOSED;
}
open_action:
- button.press: gate_button2
close_action:
- button.press: gate_button2
stop_action:
- button.press: gate_button2
After flashing Sonoff Basic with the gate.yaml
config file, ESPHome will automatically add to the Home Assistant the Gate
entity of the cover
type, with all services and properties. All you have to do is to add a card or button to Lovelace, or throw Gate
entity through the bridge in HomeKit. Below is a configuration.yaml
fragment, where gate_opening_sensor
and Gate
entity are added to HomeKit bridge.
homekit:
- filter:
include_domains:
- light
- media_player
include_entities:
- binary_sensor.gate_open_sensor
- cover.gate
Parts | Description |
---|---|
![]() |
Sonoff Basic R2 |
![]() |
Wired magnetic door sensor |
If my work has been useful to you, I would be grateful for your support: