Skip to content

Instantly share code, notes, and snippets.

@muxa
Created September 18, 2021 06:25
Show Gist options
  • Save muxa/f6aeda90eec739e79a645146364d563d to your computer and use it in GitHub Desktop.
Save muxa/f6aeda90eec739e79a645146364d563d to your computer and use it in GitHub Desktop.
ESPHome Gate single button control with State Machine
substitutions:
device_name: Gate
esphome:
name: gate
platform: ESP8266
board: d1_mini
on_boot:
priority: 600
then:
- cover.template.publish:
id: gate
state: CLOSED
current_operation: IDLE
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
fast_connect: true
logger:
level: DEBUG
api:
password: !secret api_password
ota:
password: !secret ota_password
external_components:
- source:
type: git
url: https://github.com/muxa/esphome-state-machine
refresh: 1h
binary_sensor:
- platform: gpio
pin:
number: D6
mode: INPUT_PULLUP
name: "${device_name} Relay"
filters:
- delayed_on: 100ms
on_press:
- if:
condition:
- switch.is_off: gate_relay
then:
- state_machine.transition: PRESS
switch:
- platform: gpio
name: "${device_name} Toggle"
internal: true
pin: D5
id: gate_relay
icon: "mdi:gate"
on_turn_on:
- delay: 1s
- switch.turn_off: gate_relay
- platform: template
name: "${device_name} Keep Open"
id: keep_open
icon: "mdi:gate"
on_turn_on:
- state_machine.transition: HOLD
on_turn_off:
- state_machine.transition: CLOSE
status_led:
pin:
number: D4
inverted: yes
sensor:
- platform: wifi_signal
name: "${device_name} WiFi Signal"
update_interval: 60s
- platform: uptime
name: "${device_name} Uptime"
filters:
- lambda: return x / 86400.0;
unit_of_measurement: "days"
accuracy_decimals: 1
text_sensor:
- platform: state_machine
name: "${device_name} State"
state_machine:
- name: "${device_name}"
initial_state: CLOSED
states:
- name: CLOSED
on_enter:
- cover.template.publish:
id: gate
state: CLOSED
current_operation: IDLE
- name: OPENING
on_enter:
- cover.template.publish:
id: gate
state: OPEN
current_operation: OPENING
- delay: 23s
- state_machine.transition: TIMEOUT
- name: OPEN
on_enter:
- cover.template.publish:
id: gate
state: OPEN
current_operation: IDLE
- if:
condition:
state_machine.transition:
from: OPENING
then:
- logger.log: "Will be able to HOLD in 6s"
- delay: 6s
- state_machine.transition: TIMEOUT
else:
- switch.turn_off: keep_open # keep open window is over, so keep off should be off
- name: CLOSING
on_enter:
- cover.template.publish:
id: gate
state: OPEN
current_operation: CLOSING
- delay: 23s
- state_machine.transition: TIMEOUT
- name: WAITING_HOLD
on_enter:
- if:
condition:
switch.is_on: keep_open
then:
- logger.log: "HOLD open"
- switch.turn_on: gate_relay
- state_machine.transition: PRESS
else:
- logger.log: "Waiting for manual HOLD"
- delay: 4s
- state_machine.transition: TIMEOUT
- name: WAITING_CLOSE
on_enter:
- logger.log: "Waiting for automatic CLOSE"
- delay: 27s # 37s total delay - 6s (before WAITING_HOLD) - 4s (after WAITING_HOLD)
- state_machine.transition: TIMEOUT
- name: OPEN_HOLD
on_enter:
- logger.log: "Keeping OPEN"
inputs:
- name: PRESS
transitions:
- CLOSED -> OPENING
- OPENING -> CLOSING
- CLOSING -> OPENING
- OPEN -> CLOSING
- WAITING_HOLD -> OPEN_HOLD
- OPEN_HOLD -> CLOSING
- WAITING_CLOSE -> CLOSING
- name: TIMEOUT
transitions:
- OPENING -> OPEN
- CLOSING -> CLOSED
- OPEN -> WAITING_HOLD
- WAITING_HOLD -> WAITING_CLOSE
- WAITING_CLOSE -> CLOSING
action:
- logger.log: "Timeout reached"
- name: OPEN
transitions:
- CLOSED -> OPENING
- CLOSING -> OPENING
action:
- switch.turn_on: gate_relay
- name: HOLD
transitions:
- WAITING_HOLD -> OPEN_HOLD
action:
- switch.turn_on: gate_relay
- name: CLOSE
transitions:
- OPEN -> CLOSING
- OPENING -> CLOSING
- OPEN_HOLD -> CLOSING
- WAITING_CLOSE -> CLOSING
action:
- switch.turn_on: gate_relay
cover:
- platform: template
name: "${device_name}"
id: gate
device_class: garage
assumed_state: false
open_action:
- state_machine.transition: OPEN
close_action:
- state_machine.transition: CLOSE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment