substitutions: name: sthome-ut6 friendly_name: "sthome-ut6" esphome: name: "${name}" friendly_name: "${friendly_name}" on_boot: - priority: 600 then: - lambda: |- id(restore_remote_lock).execute(); globals: - id: remote_lock_status type: bool restore_value: yes initial_value: 'false' esp8266: board: esp01_1m # Enable logging logger: # Enable Home Assistant API api: encryption: key: "K3yXQthpXVNknD4RCLZX66gglNgDEFtj3H0r85VLBNs=" ota: - platform: esphome password: "5956a60f6cf40cf4b6b172e23f236572" wifi: #ssid: !secret wifi_ssid #password: !secret wifi_password # we will use local dns server for local dns resolution domain: ".sthome.org" networks: - ssid: !secret wifi_ssid1 password: !secret wifi_password1 - ssid: !secret wifi_ssid2 password: !secret wifi_password2 - ssid: !secret wifi_ssid3 password: !secret wifi_password3 - ssid: !secret wifi_ssid4 password: !secret wifi_password4 - ssid: !secret wifi_ssid5 password: !secret wifi_password5 manual_ip: # For faster connection startup, set a static IP address # Set this to the IP of the ESP static_ip: 10.0.2.6 gateway: 10.0.0.2 subnet: 255.255.240.0 dns1: 10.0.0.1 dns2: 10.0.0.2 # Enable fallback hotspot (captive portal) in case wifi connection fails ap: ssid: "${name} Fallback Hotspot" password: "HIjEc5P2BJhz" captive_portal: ##################################################################################### # Minimal flash writes, once per hour preferences: flash_write_interval: 60min sun: id: sun_sensor latitude: !secret latitude longitude: !secret longitude # Sync time with Home Assistant time: - platform: homeassistant id: homeassistant_time # Blink LED for status, and also expose to HA as switch #light: # - platform: status_led # name: "${friendly_name} status light" # id: blueled # pin: # number: GPIO2 # inverted: yes # restore_mode: RESTORE_DEFAULT_OFF # #script: # - id: heartbeat # mode: single # then: # - light.toggle: blueled # - delay: 20 ms # - light.toggle: blueled # # Heartbeat while connected to HA #interval: # - interval: 5s # then: # if: # condition: # api.connected: # then: # - script.execute: heartbeat # switch: # Switch to restart the ESP - platform: restart name: ${friendly_name} Restart # depending on current state of gate, it will open, close or stop closing / opening - platform: gpio pin: GPIO0 name: "Gate Remote Trigger" inverted: false id: gate_remote_trigger restore_mode: RESTORE_DEFAULT_OFF on_turn_on: - delay: 1000ms - switch.turn_off: gate_remote_trigger # this is a latching relay, so each pulse toggles the state of the relay - platform: gpio pin: GPIO1 name: "Gate Remote Lock Toggle" inverted: false id: gate_remote_lock_toggle restore_mode: RESTORE_DEFAULT_OFF on_turn_on: # - lambda: |- # ESP_LOGD("info", "1: Remote lock status: %d [%d]", id(remote_lock_status), id(gate_remote_lock_sensor).state); - delay: 200ms - switch.turn_off: gate_remote_lock_toggle - delay: 100ms - lambda: |- id(remote_lock_status) = id(gate_remote_lock_sensor).state; // ESP_LOGD("info", "2: Remote lock status: %d [%d]", id(remote_lock_status), id(gate_remote_lock_sensor).state); binary_sensor: - platform: status # Status platform provides a connectivity sensor name: "${friendly_name} Status" device_class: connectivity - platform: gpio pin: number: GPIO3 inverted: false id: gate_remote_lock_sensor name: Gate Remote Lock Sensor device_class: lock sensor: # Report wifi signal strength every 5 min if changed - platform: wifi_signal name: ${friendly_name} WiFi Signal update_interval: 300s filters: - delta: 10% # human readable uptime sensor output to the text sensor above - platform: uptime name: ${friendly_name} Uptime in Days id: uptime_sensor_days update_interval: 60s on_raw_value: then: - text_sensor.template.publish: id: uptime_human state: !lambda |- int seconds = round(id(uptime_sensor_days).raw_state); int days = seconds / (24 * 3600); seconds = seconds % (24 * 3600); int hours = seconds / 3600; seconds = seconds % 3600; int minutes = seconds / 60; seconds = seconds % 60; return ( (days ? String(days) + "d " : "") + (hours ? String(hours) + "h " : "") + (minutes ? String(minutes) + "m " : "") + (String(seconds) + "s") ).c_str(); text_sensor: # Expose WiFi information as sensors - platform: wifi_info ip_address: name: ${friendly_name} IP mac_address: name: ${friendly_name} Mac Address # human readable update text sensor from sensor:uptime - platform: template name: Uptime id: uptime_human icon: mdi:clock-start script: - id: restore_remote_lock then: - lambda: |- bool restore_value = id(remote_lock_status); id(set_remote_lock).execute(restore_value); - delay: 250ms # make double sure after delay - lambda: |- bool restore_value = id(remote_lock_status); id(set_remote_lock).execute(restore_value); - id: set_remote_lock parameters: on_state: bool then: - lambda: |- bool lockstate = id(gate_remote_lock_sensor).state; // next statement is XOR; causes a skip if states are the same if((on_state && !lockstate) || (!on_state && lockstate)) { // toggle id(gate_remote_lock_toggle).turn_on(); } # Pin assignments for ESP-01 # https://randomnerdtutorials.com/esp8266-pinout-reference-gpios/ # 3v3 | | RX/GPIO3 - high at boot # RST | | GPIO0 - pulled up, flash if low on boot # EN | | GPIO2 - pulled up, blue led on if pulled down, must be high at boot # TX | | GND # ^ TX/GPIO1 - high at boot