esphome: name: gate platform: ESP8266 board: d1_mini_pro on_boot: then: # Set/get initial states - binary_sensor.template.publish: id: automation_safety state: OFF - binary_sensor.template.publish: id: heading_comparison state: OFF - component.update: battery_level web_server: port: 80 logger: level: NONE wifi: networks: - ssid: SSID1 password: PSK # Mobile Hotspot for troubleshooting - ssid: SSID2 password: PSK ota: safe_mode: True password: esphome_recovery i2c: sda: D2 scl: D1 scan: False script: - id: gate_toggle then: if: condition: and: # Make sure we aren't triggering too rapidly - binary_sensor.is_off: automation_safety # Make sure gate is closed or else we won't trigger - binary_sensor.is_off: gate_status then: - binary_sensor.template.publish: id: automation_safety state: ON - switch.toggle: gate - delay: 120s - binary_sensor.template.publish: id: automation_safety state: OFF mqtt: broker: 104.131.62.194 username: gate password: gzFCjyjNXA log_topic: on_message: - topic: cmd/gate then: - switch.toggle: gate sensor: - platform: qmc5883l address: 0x0D heading: id: heading data_rate: 10Hz range: 200uT oversampling: 512x update_interval: 1s # Template sensor to keep average of last 15 heading # readings so we can detect significant change - platform: template id: heading_average update_interval: 1s unit_of_measurement: "°" lambda: return id(heading).state; filters: - sliding_window_moving_average: window_size: 15 send_every: 1 on_value: then: if: condition: and: - for: time: 3s condition: binary_sensor.is_on: heading_comparison # This is intentionally duplicated - binary_sensor.is_off: automation_safety then: - script.execute: gate_toggle - platform: adc pin: A0 id: battery_level name: "Gate Battery Voltage" update_interval: 600s accuracy_decimals: 4 filters: - multiply: 15.43 - platform: uptime name: "Uptime" binary_sensor: # Sensor to keep script from triggering in rapid succession - platform: template name: "Automation Safety" id: automation_safety # This is what is used to trigger the automation - platform: template name: Heading Comparison Trigger id: heading_comparison # Set our trigger to true if heading varies from avg more than 2.5deg lambda: |- if (id(heading).state > id(heading_average).state + 2.5) { return true; } if (id(heading).state < id(heading_average).state - 2.5) { return true; } else { return false; } # Magnet sensor to get real-world state - platform: gpio pin: number: D7 mode: INPUT_PULLUP name: Gate Status id: gate_status device_class: door filters: - delayed_on: 1s - delayed_off: 1s switch: - platform: restart name: "Reboot" # Actual GPIO output - platform: gpio id: relay pin: number: D8 inverted: False # Switch which toggles the relay on and off - platform: template id: gate name: "Gate Remote" icon: "mdi:gate" turn_on_action: - switch.turn_on: relay - delay: 500ms - switch.turn_off: relay text_sensor: - platform: wifi_info ip_address: name: "IP Address"