esphome:
name: door-lock
friendly_name: Door-Lock
esp32:
board: lolin_s2_mini
framework:
type: arduino
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: "HIDDEN FOR MY SANITY"
ota:
- platform: esphome
password: "HIDDEN FOR MY SANITY"
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Door-Lock Fallback Hotspot"
password: "HIDDEN FOR MY SANITY"
captive_portal:
# --- Storage ---
globals:
- id: last_tags
type: std::vector<std::string>
initial_value: '{}'
- id: alarm_active
type: bool
initial_value: 'false'
# --- Communication ---
i2c:
sda: 33
scl: 35
spi:
clk_pin: 7
mosi_pin: 11
miso_pin: 9
uart:
id: uart_bus
tx_pin: 39
rx_pin: 37
baud_rate: 256000
ld2410:
- uart_id: uart_bus
# --- Sentry Flash Script ---
script:
- id: police_flash
mode: restart
then:
- while:
condition:
lambda: 'return id(alarm_active);'
then:
- light.addressable_set:
id: status_led
red: 100%
blue: 0%
- delay: 150ms
- light.addressable_set:
id: status_led
red: 0%
blue: 100%
- delay: 150ms
- light.turn_off: status_led
# --- Hardware Components ---
light:
- platform: neopixelbus
type: GRB
variant: WS2812X
pin: 18
num_leds: 1
id: status_led
name: "Sentry LED"
restore_mode: ALWAYS_OFF
switch:
- platform: gpio
pin: 13
name: "Shaft Lock Relay"
id: relay_1
icon: "mdi:door-lock"
- platform: template
name: "RFID Maintenance Mode"
id: maintenance_mode
optimistic: true
restore_mode: ALWAYS_OFF
icon: "mdi:hammer-wrench"
sensor:
- platform: aht10
temperature:
name: "Cab Temperature"
humidity:
name: "Cab Humidity"
update_interval: 60s
- platform: ld2410
moving_distance:
name: "Sentry Distance"
on_value_range:
- below: 300
then:
- lambda: 'id(alarm_active) = true;'
- script.execute: police_flash
- above: 3.1
then:
- lambda: 'id(alarm_active) = false;'
binary_sensor:
- platform: ld2410
has_target:
name: "Presence in Cab"
id: presence_detected
# --- RFID System ---
rc522_spi:
cs_pin: 12
on_tag:
then:
- lambda: |-
// 1. Stop Sentry Flash immediately on scan
id(alarm_active) = false;
// 2. Log tag to history
id(last_tags).insert(id(last_tags).begin(), x);
if (id(last_tags).size() > 10) { id(last_tags).pop_back(); }
id(last_10_tags_view).publish_state("Tag Scanned");
// 3. Check access
bool is_master_tag = (x == "###"); // <--- REPLACE WITH YOUR TAG ID
bool is_maintenance = id(maintenance_mode).state;
if (is_master_tag || is_maintenance) {
auto call = id(status_led).turn_on();
call.set_rgb(0.0, 1.0, 0.0); // Green
call.perform();
id(relay_1).turn_on();
ESP_LOGD("custom", "Access Granted");
} else {
auto call = id(status_led).turn_on();
call.set_rgb(1.0, 0.0, 0.0); // Solid Red
call.perform();
ESP_LOGD("custom", "Access Denied");
}
- delay: 5s
- switch.turn_off: relay_1
- light.turn_off: status_led
text_sensor:
- platform: template
name: "Last 10 RFID Tags"
id: last_10_tags_view
lambda: |-
std::string output = "";
for (const auto &tag : id(last_tags)) {
output += tag + "\n";
}
return output;