Malaiesun Logo
Dekstop

HA Smart Door Lock

Advanced door lock system with Home Assistant Integration. (M.E.L.O.D.Y)

Difficulty:Advanced
Est. Time:2 hours
Author:Malaiesun
Published:December 15, 2024

YouTube Demo:

Project Overview

Melody is a secure smart door lock built with an ESP32, RFID, an LCD numpad, and MQTT integration with Home Assistant.

It supports PIN and RFID entry, phone-based unlock via Home Assistant, a Do-Not-Disturb (DND) mode to ignore specific tags, and reports all states (locked/unlocked, door open/closed, battery/power) to Home Assistant for full monitoring and automation.

Materials & Components

You’ll need the following hardware and tools:

  • ESP32 (development board)
  • RFID reader (e.g., RC522) and RFID tags/cards
  • 16x2 or 20x4 LCD with I2C backpack (or SPI)
  • 4x4 matrix keypad (numpad) or dedicated numeric keypad
  • Electronic strike / solenoid lock or motor-driven deadbolt
  • Magnetic door sensor (reed switch) for open/closed state
  • Battery pack / 12V power supply and optional voltage regulator
  • MOSFET / relay and flyback diode for driving the lock
  • Push button(s) for manual lock/unlock and DND toggle (optional)
  • Enclosure / 3D printed case, mounting hardware, and wiring supplies
  • Optional: buzzer, RGB LED for status, RTC module for timed DND

Key Features

Melody delivers the following capabilities:

  • RFID unlock using authorized tags or cards.
  • PIN entry via the onboard keypad with on-screen feedback on the LCD.
  • Phone-based unlock by calling a Home Assistant service (mobile app / automations).
  • DND Mode: blacklist specific RFID tags or temporarily disable selected tags (useful for guests or lost tags).
  • State reporting to Home Assistant: lock state, door open/closed, last unlock method, battery level, tamper alerts.
  • Secure comms: MQTT over TLS between ESP32 and Home Assistant broker (recommended).
  • Audit log: Home Assistant stores events — who unlocked, by which method, and timestamp.
  • Auto-lock options: configurable delay to auto-lock after closing.
  • Local fallback: keypad and RFID work without network (ESP32 handles local auth).

System Architecture & Integration

How Melody connects and communicates:

  • ESP32 runs firmware that:
    • Reads keypad and RFID inputs,
    • Controls the lock actuator and reads the door sensor,
    • Publishes state and events to MQTT topics,
    • Subscribes to control topics (e.g., home/door/lock/set) for phone or HA commands.
  • Home Assistant:
    • Acts as the MQTT broker or connects to one,
    • Exposes lock entity, binary sensor (door), sensor entities (battery, last_action),
    • Provides mobile/voice unlock via automations and scripts,
    • Hosts dashboards and history logs for Melody.
  • Security:
    • Use MQTT with TLS and username/password or client certificates,
    • Store PINs and RFID IDs hashed or in secure storage on Home Assistant, not in plain text on the device.

Step-by-Step Build Guide

Basic implementation steps:

  1. Hardware setup
    • Mount the ESP32, RFID reader, keypad, LCD, door sensor, and lock in the enclosure.
    • Wire the lock via MOSFET/relay; ensure power handling and flyback protection.
  2. Local firmware
    • Implement keypad scanning, RFID reading, LCD UI, and lock control.
    • Add safe debounce, lock activation timing, and emergency unlock behavior.
  3. MQTT integration
    • Configure MQTT client on ESP32 (set TLS if possible).
    • Publish topics: melody/lock/state, melody/door/state, melody/event (payloads: JSON with method, id, timestamp).
    • Subscribe to: melody/lock/command for remote lock/unlock and melody/dnd/set for toggling DND.
  4. Home Assistant config
    • Create lock and sensors using MQTT integration or device via MQTT discovery.
    • Build automations for phone unlock, notifications on unauthorized access, and auto-lock.
  5. DND and access control
    • Maintain an authorized list; implement blacklist/whitelist logic.
    • Provide an HA service or UI to add/remove tags and toggle DND.
    • Ensure DND persists across reboots (store in non-volatile storage or manage from HA).
  6. Security hardening
    • Use unique MQTT credentials per device.
    • Rate-limit keypad attempts and introduce cooldowns on repeated wrong PINs.
    • Log and alert on tamper or repeated failed attempts.
  7. Testing & deployment
    • Test all unlock flows: RFID, keypad PIN, phone unlock, manual button.
    • Verify state reporting in HA and that automations trigger correctly.
    • Monitor for edge cases (power loss, network downtime) and add safe defaults.

Example MQTT Topic Structure

Recommended minimal topic layout:

  • melody/lock/stateLOCKED / UNLOCKED
  • melody/door/stateOPEN / CLOSED
  • melody/event → JSON { "method":"PIN|RFID|PHONE", "id":"tagId|user", "result":"OK|FAIL", "time":"ISO8601" }
  • melody/lock/command → payload LOCK or UNLOCK
  • melody/dnd/set → payload {"mode":"ON","blacklist":["id1","id2"]}

Security & Best Practices

Keep Melody secure and reliable:

  • Use TLS for MQTT and strong credentials.
  • Keep sensitive lists (authorized tags, PINs) in Home Assistant and push updates to the device securely.
  • Implement brute-force protection (lockout after multiple failed PINs).
  • Log events in HA for auditing and add notifications for suspicious activity.
  • Provide manual override and safe fail states for emergency access.

Final Notes

Intended: Melody is designed to be a secure, connected, and user-friendly smart lock that blends local reliability (keypad & RFID) with the flexibility of Home Assistant automations.
Customize UI messages, auto-lock timings, and DND behavior to fit your home or lab security needs.