Skip to content

Instantly share code, notes, and snippets.

@JohnMcLear
Last active January 8, 2025 15:26
Show Gist options
  • Select an option

  • Save JohnMcLear/865bd66df8e930dc2abfb4153f543c84 to your computer and use it in GitHub Desktop.

Select an option

Save JohnMcLear/865bd66df8e930dc2abfb4153f543c84 to your computer and use it in GitHub Desktop.
substitutions:
name: esphome-web-dee4ac
friendly_name: 'Derpy Test Door'
# Enable Home Assistant API
api:
reboot_timeout: 5min
on_client_connected:
- logger.log:
format: "Client %s connected to API with IP %s"
args: ["client_info.c_str()", "client_address.c_str()"]
- globals.set:
id: homeassistant_connected
value: 'true'
on_client_disconnected:
- logger.log:
format: "Client %s disconnected from API with IP %s"
args: ["client_info.c_str()", "client_address.c_str()"]
- globals.set:
id: homeassistant_connected
value: 'false'
actions:
# - action: play_rtttl
# variables:
# song_str: string
# then:
# - rtttl.play:
# rtttl: !lambda 'return song_str;'
- action: add_trusted_id
variables:
tagid: string
then:
- lambda: |-
std::string tag_id = tagid;
std::string trusted_ids = id(trustedids_str);
// Check if tagid is already in the trusted_ids list
if (trusted_ids.find(tag_id) == std::string::npos) {
if (trusted_ids.empty()) {
id(trustedids_str) = tag_id;
} else {
id(trustedids_str) += "," + tag_id;
}
ESP_LOGD("trustedids", "Added new tag ID: %s", tag_id.c_str());
} else {
ESP_LOGD("trustedids", "Tag ID %s already exists, not adding.", tag_id.c_str());
}
// Update the display with the current trusted_ids string
id(trustedids_display).publish_state(id(trustedids_str));
- action: remove_trusted_id
variables:
tagid: string
then:
- lambda: |-
std::string new_str = "";
std::string tag_id = tagid;
size_t pos = 0;
while ((pos = id(trustedids_str).find(tag_id)) != std::string::npos) {
if (pos > 0 && id(trustedids_str)[pos - 1] == ',') {
new_str = id(trustedids_str).substr(0, pos - 1) + id(trustedids_str).substr(pos + tag_id.size());
} else {
new_str = id(trustedids_str).substr(0, pos) + id(trustedids_str).substr(pos + tag_id.size());
}
id(trustedids_str) = new_str;
}
ESP_LOGD("trustedids", "Removed tag ID: %s", tag_id.c_str());
id(trustedids_display).publish_state(id(trustedids_str));
ota:
- platform: esphome
password: "INSERTPASSHERE"
improv_serial:
wifi:
ssid: "INSERT WIFI UUID HERE"
password: "INSERT WIFI OR WHATEVER SECRET HERE"
ap: {}
domain: ".lan"
captive_portal:
dashboard_import:
package_import_url: github://esphome/example-configs/esphome-web/esp32.yaml@main
import_full_config: true
esp32_improv:
authorizer: none
web_server:
port: 80
binary_sensor:
- platform: template
name: "NFC Reader Availability"
lambda: |-
return !(id(my_pn532).status_has_warning() || id(my_pn532).is_failed()) && id(my_pn532).is_ready();
- platform: template
name: "Home Assistant Availability"
lambda: 'return id(homeassistant_connected);'
output:
- platform: gpio
pin: GPIO16
id: 'relay1'
# - platform: ledc
# id: buzzer_output
# pin:
# number: GPIO27
# mode:
# output: true
# pulldown: true
lock:
- platform: output
name: "Door Lock"
output: 'relay1'
id: 'lockymclockface'
# on_unlock:
# then:
# - light.turn_on:
# id: led
# red: 0%
# green: 100%
# blue: 0%
# on_lock:
# then:
# - light.turn_on:
# id: led
# red: 0%
# green: 0%
# blue: 100%
pn532_spi:
cs_pin: GPIO18
data_rate: 200kHz
id: my_pn532
update_interval: 1s
on_tag:
then:
- delay: 0.15s #to fix slow component
- if:
condition:
lambda: |-
std::string tag_id = x.c_str();
std::string trusted_ids = id(trustedids_str);
// Check if tag_id is already in the trusted_ids list
if (trusted_ids.find(tag_id) != std::string::npos) {
return true; // Tag is found in the list
}
return false; // Tag is not found in the list
then:
# Remember that due to the way this logic works any Trusted ID / Tag
# Never shows up in Home Assistant > Settings > Tags. They do show up before though..
# What I wont be able to see is when a Trusted person went into the property..
- logger.log: "Unlocked due to trusted tagid"
# - rtttl.play: "success:d=24,o=5,b=100:c,g,b"
- lock.unlock:
id: lockymclockface
- delay: 3s
- logger.log: "Relocked due to trusted tagid"
- lock.lock:
id: lockymclockface
- light.turn_on:
id: led
red: 0%
green: 0%
blue: 100%
else:
- if:
condition:
lambda: 'return id(homeassistant_connected) == true;'
then:
- logger.log: "Connected to Home Assistant so running an automation there to check if user exists in the Settings > Tags of HA"
- homeassistant.tag_scanned: !lambda 'return x;'
else:
- logger.log: "Unlocked due to lack of Home Assistant connectivity"
# - rtttl.play: 'GoodBad:d=4,o=5,b=56:32p,32a#,32d#6,32a#,32d#6,8a#.,16f#.,16g#.,d#,32a#,32d#6,32a#,32d#6,8a#.,16f#.,16g#.,c#6,32a#,32d#6,32a#,32d#6,8a#.,16f#.,32f.,32d#.,c#,32a#,32d#6,32a#,32d#6,8a#.,16g#.,d#'
- lock.unlock:
id: lockymclockface
- delay: 3s
- lock.lock:
id: lockymclockface
- light.turn_on:
id: led
red: 0%
green: 0%
blue: 100%
text_sensor:
- platform: template
name: "NFC Tag"
id: rfid_tag
- platform: template
name: "Trusted IDs"
id: trustedids_display
lambda: |-
return id(trustedids_str);
spi:
clk_pin: GPIO21
miso_pin: GPIO22
mosi_pin: GPIO19
light:
- platform: neopixelbus
id: led
type: GRB
variant: ws2812x
pin: GPIO25
num_leds: 1
name: "LED"
default_transition_length: 0.5s
button:
- platform: restart
name: "Restart"
- platform: template
name: "Log Trusted IDs"
on_press:
then:
- lambda: |-
ESP_LOGD("trustedids", "Current trusted IDs: %s", id(trustedids_str).c_str());
- platform: template
name: "Clear Trusted IDs"
on_press:
then:
- lambda: |-
id(trustedids_str) = "";
id(trustedids_display).publish_state(id(trustedids_str));
ESP_LOGD("trustedids", "Trusted IDs cleared.");
sensor:
- platform: uptime
type: seconds
name: Uptime Sensor
globals:
- id: homeassistant_connected
type: bool
restore_value: yes
initial_value: 'false'
- id: trustedids_str
type: std::string
restore_value: True
initial_value: '""'
esphome:
name: ${name}
friendly_name: ${friendly_name}
min_version: 2024.6.0
name_add_mac_suffix: false
project:
name: esphome.web
version: dev
on_boot:
priority: -10
then:
- lock.lock:
id: lockymclockface
- logger.log: API is connected!
# - rtttl.play: "success:d=24,o=5,b=100:c,g,b"
- light.turn_on:
id: led
red: 0%
green: 0%
blue: 100%
#rtttl:
# output: buzzer_output
# on_finished_playback:
# - logger.log: 'Song ended!'
esp32:
board: esp32dev
framework:
type: arduino
# Enable logging
logger:
level: DEBUG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment