Every night, my wife asks me if I locked the basement door. And every night, I can’t remember. So I walk down to the basement to check. And almost always, it’s locked.

So I wanted something that could answer that question for me. A way to see in Home Assistant if the door is locked or not, without going down the stairs.

In this article, I’ll show you how I did it with a small switch inside the lock and an ESP32-H2 board.

The Idea

A smart lock would solve this, but I didn’t need the door to lock itself. I only needed to know if it was locked.

When you lock a door, a bolt moves inside the lock case. If a switch could sense that bolt, I’d know the state of the lock. Then an ESP32-H2 could send that state over Zigbee to Home Assistant.

Finding a Spot Inside the Lock

I took the lock case out of the door and opened it. Inside, I found a perfect spot for a limit switch, where it opens and closes as the door is locked and unlocked. I glued the switch in place.

A limit switch glued inside an opened door lock case

Then I soldered two wires to the switch and led them out of the lock case.

Testing the Idea With an LED

Before doing the ESP32-H2 part, I wanted to check that the idea worked. So I connected the two wires from the lock case to a simple test circuit on a breadboard: a battery, a resistor and an LED.

The door lock case with two wires from the switch connected to a breadboard with a resistor, an LED and a 9 V battery

The switch is in series with the LED. When the switch closes, current flows and the LED lights up. When it opens, the LED goes dark. So by locking and unlocking, I could see right away if the switch reacted to the lock.

Schematic of the test circuit: a 9 V battery, the switch, a 1000 ohm resistor and a red LED in series

With that working, I mounted the lock case back in the door.

The Circuit

The circuit is very simple. One wire from the switch goes to GND, and the other goes to GPIO10 on the ESP32-H2. The ESP32-H2 has a built-in pull-up resistor, so the pin reads HIGH when the switch is open and LOW when it’s closed.

Schematic of the smart door lock: a microswitch between GPIO10 and GND, and a 3.6 V lithium AA battery with a 100 µF capacitor on the 3V3 pin of an ESP32-H2-DevKitM-1
✎ Edit this circuit Open Build Instructions

Change values or swap parts. Free in your browser, no signup needed.

To power it, I used a 3.6 V lithium AA battery connected to the 3V3 pin. That’s right at the top of what the 3V3 pin can take. If you build this yourself, a 3.2 V LiFePO4 battery of the same size is the safer choice.

I also added a 100 µF capacitor across the battery. When the ESP32-H2 transmits, it needs short bursts of current, and this type of battery can have a hard time delivering that. The capacitor helps out during those bursts.

Wiring of the smart door lock: AA battery and capacitor on 3V3 and GND, and a microswitch wired to GPIO10 and GND through a small stripboard
✎ Edit this circuit

Move parts around or plan your own build. Free in your browser, no signup needed.

Setting Up the ESP32-H2

The ESP32-H2 runs the same code as my Zigbee door sensor. It sleeps almost all the time and wakes up when the switch changes. Then it sends “open” or “closed” to Home Assistant and goes back to sleep.

I’ve written a separate tutorial with all the details: the Arduino IDE settings, the code, and how to pair the board with Home Assistant. Follow it, and then come back here:

How to Make a Zigbee Door Sensor With an ESP32-H2

This is the code running on my lock. It’s the same as in the door sensor tutorial:

#ifndef ZIGBEE_MODE_ED
#error "Select Tools -> Zigbee mode: Zigbee ED (end device)"
#endif

#include "Zigbee.h"
#include <Preferences.h>

#define SENSOR_PIN     10   // Must be GPIO 7-14 to wake the ESP32-H2 from deep sleep
#define ENDPOINT       1    // Same endpoint as the example you paired with
#define CHECKIN_HOURS  6    // Wake up anyway every few hours so Home Assistant knows we're alive

ZigbeeContactSwitch zbContact = ZigbeeContactSwitch(ENDPOINT);
Preferences preferences;    // Stores the "enrolled" flag in flash

bool readIsOpen() {
  return digitalRead(SENSOR_PIN) == HIGH;
}

void sendState(bool isOpen) {
  if (isOpen) {
    zbContact.setOpen();
    Serial.println("Sent: OPEN");
  } else {
    zbContact.setClosed();
    Serial.println("Sent: CLOSED");
  }
}

void goToSleep() {
  // Wake up when the pin changes: if it's HIGH now, wake on LOW, and the other way around
  bool isOpen = readIsOpen();
  esp_sleep_enable_ext1_wakeup(1ULL << SENSOR_PIN,
                               isOpen ? ESP_EXT1_WAKEUP_ANY_LOW : ESP_EXT1_WAKEUP_ANY_HIGH);

  // Also wake up on a timer, as a "still alive" check-in
  esp_sleep_enable_timer_wakeup((uint64_t)CHECKIN_HOURS * 3600ULL * 1000000ULL);

  Serial.println("Going to sleep");
  Serial.flush();
  esp_deep_sleep_start();
}

void setup() {
  Serial.begin(115200);
  pinMode(SENSOR_PIN, INPUT_PULLUP);  // Built-in pull-up resistor

  // Has this board been enrolled before? (Same flag as the example uses)
  preferences.begin("Zigbee", false);
  bool enrolled = preferences.getBool("ENROLLED");
  preferences.end();

  // Set up the Zigbee device
  zbContact.setManufacturerAndModel("DIY", "ESP32H2_Door_Sensor");
  zbContact.setPowerSource(ZB_POWER_SOURCE_BATTERY);
  Zigbee.addEndpoint(&zbContact);

  // Settings for a sleepy end device (from Espressif's sleepy sensor example)
  esp_zb_cfg_t zigbeeConfig = ZIGBEE_DEFAULT_ED_CONFIG();
  zigbeeConfig.nwk_cfg.zed_cfg.keep_alive = 10000;
  Zigbee.setTimeout(10000);

  if (!Zigbee.begin(&zigbeeConfig, false)) {
    Serial.println("Zigbee failed to start");
    goToSleep();
  }

  // Wait for the network, but give up after 10 seconds to save the battery
  unsigned long start = millis();
  while (!Zigbee.connected()) {
    if (millis() - start > 10000) {
      Serial.println("No network, sleeping");
      goToSleep();
    }
    delay(100);
  }

  // Restore the enrollment from flash, or ask for a new one the first time
  if (enrolled) {
    zbContact.restoreIASZoneEnroll();
  } else {
    zbContact.requestIASZoneEnroll();
  }

  start = millis();
  while (!zbContact.enrolled()) {
    if (millis() - start > 10000) {
      Serial.println("Not enrolled, sleeping");
      goToSleep();
    }
    delay(100);
  }

  // Save the flag the first time, so we can restore it after every wake-up
  if (!enrolled) {
    preferences.begin("Zigbee", false);
    preferences.putBool("ENROLLED", true);
    preferences.end();
  }

  // Send the state, then send again if it changed while we were busy
  bool sentState = readIsOpen();
  sendState(sentState);
  delay(1000);  // Give the radio time to deliver the message

  if (readIsOpen() != sentState) {
    sendState(readIsOpen());
    delay(1000);
  }

  goToSleep();
}

void loop() {
  // Never reached - the board sleeps at the end of setup()
}

I put the finished device in a small box on the door, right above the lock.

The basement door with the finished ESP32-H2 device in a small white box above the lock

Making It Look Like a Lock in Home Assistant

When I powered it up and added it to Home Assistant, the lock showed up as an “alarm”, just like in the door sensor tutorial.

First, I opened the entity’s settings and set Show as to Lock. Then it showed “Locked” and “Unlocked” with a lock icon. But my real smart locks get a green icon when they’re locked, and this one stayed gray. That’s because it’s a binary sensor, not a real lock.

The fix is to wrap the sensor in a template lock. You can do that right in the Home Assistant interface:

  1. Go to Settings → Devices & services → Helpers, click Create helper, and choose Template → Lock.
  2. Give it a name, like “Basement lock”.
  3. In State, enter this line, with your own sensor’s entity ID:
    {{ is_state('binary_sensor.basement_lock_alarm_1', 'off') }}
    
  4. Under Device, pick the door sensor device, so the new lock ends up in the same area.
  5. Leave the lock and unlock actions empty, and save.

This tells Home Assistant that the door is locked when the sensor is off, which means the switch is closed.

Now I get a big lock icon that’s green when the door is locked and red when it’s unlocked:

The basement lock in the Home Assistant app: a green lock icon when locked and a red open lock icon when unlocked

The control in the middle can’t lock or unlock anything, since the device only senses the lock. But it shows the state at a glance.

Now, when the question comes in the evening, I can check Home Assistant instead of walking down to the basement.

More Arduino Tutorials