In our basement we have a baby gate, which surprisingly keeps our cat out of the gym and golf sim areas.

Sometimes we forget to close the gate, so I needed a sensor to monitor its state. I still had the breadboard from the air quality monitor project, so it was quick to add a magnetic door switch and test things out with the D1 Mini clone.

I have extra sensors, so those were kept in the project and allowed me to get rid of the shitty DHT22 I added to the golf remote. Everything worked, but I want to save my last two D1 minis and use them for something with the screens I have for them. So I swapped in an Adafruit Feather HUZZAH ESP8266, which I got with AdaBox 3 or 4 in 2017 and made minor changes to the code.

Parts:
- Baby gate
- Adafruit Feather HUZZAH ESP8266
- Magnetic door switch
- ENS160 multi-gas sensor
- Si7021 temperature and humidity sensor
- Adafruit Perma-Proto Half-sized Breadboard
- Project box
References:
- Adafruit Feather HUZZAH ESP8266 Pinout
- ESPHome: GPIO Binary Sensor
- ESPHome: I²C Bus
- ESPHome: ENS160 Sensor
- ESPHome: AHT10 Sensor
- Home Assistant Forums: ENS160 Stability issues
- ESPHome: HTU21D | Si7021 | SHT21 Temperature & Humidity Sensor
ESPHome YAML code:
substitutions:
slug: gate
friendly: Gate
esphome:
name: ${slug}
friendly_name: ${friendly}
esp8266:
board: huzzah
logger:
level: WARN
api:
encryption:
key: 'xxx'
ota:
- platform: esphome
password: "xxx"
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
manual_ip:
static_ip: x.x.x.x
gateway: x.x.x.x
subnet: 255.255.255.0
i2c:
binary_sensor:
- platform: gpio
pin:
number: GPIO14
mode:
input: true
pullup: true
name: ${friendly}
device_class: door
sensor:
- platform: htu21d
model: SI7021
temperature:
name: Temperature
id: ${slug}_temp
humidity:
name: Humidity
id: ${slug}_humid
- platform: aht10
variant: AHT20
temperature:
name: AHT21 Temperature
id: ${slug}_aht21_temp
humidity:
name: AHT21 Humidity
id: ${slug}_aht21_humid
- platform: ens160_i2c
address: 0x53
eco2:
name: CO²
tvoc:
name: VOC
aqi:
id: demo_aqi
name: AQI
compensation:
temperature: ${slug}_aht21_temp
humidity: ${slug}_aht21_humid
text_sensor:
- platform: template
name: AQI Rating
lambda: |-
switch ( (int) ( id( ${slug}_aqi ).state ) ) {
case 1: return {"Excellent"};
case 2: return {"Good"};
case 3: return {"Moderate"};
case 4: return {"Poor"};
case 5: return {"Unhealthy"};
default: return {"N/A"};
}
I also added this to my configuration.yaml because I wanted a gate icon instead of the door, due to the device class of the binary sensor:
template:
- binary_sensor:
- name: Gate
unique_id: gate_template
device_class: door
state: "{{ is_state( 'binary_sensor.basementgate_gate', 'on' ) }}"
icon: |
{% if is_state( 'binary_sensor.basementgate_gate', 'on' ) %}
mdi:gate-open
{% else %}
mdi:gate
{% endif %}
I figured I might as well use one of the fancy Adafruit Perma-Proto boards I had, which makes soldering all of the connections much easier. As a bonus it was nearly a perfect fit for the case.





The magnetic switch and Si7021 will live outside the box, so those couldn’t get soldered yet. After connecting power I checked the ESPHome logs to make sure everything was working.

I cut holes in a project box, finished soldering, and used hot glue to secure the board..




I reversed the swing of the gate, placed my device, and attached the two sides of the magnetic switch to the gate.

In Home Assistant an automation runs whenever the stairs light is turned off to check the state of the gate. If it’s open, a notification is sent to our phones.


I’m enjoying these little electronics projects, and it feels good to finally put various parts to use.




















































