ESPHome State Machine component
A flexible Finite-State Machine platform for ESPHome. It lets you model complex behaviours with limited inputs, such as:
light with a single button.cover with a single button.display with a button (e.g. flip through pages on click, and go into editing mode on hold).external_components:
- source:
type: git
url: https://github.com/muxa/esphome-state-machineThe basic state machine configuration involves providing:
statesinputstransitions for each input.Example for a simple on/off toggle state machine:
state_machine:
- name: On/Off Toggle State Machine
states:
- "OFF"
- "ON"
inputs:
- name: TOGGLE
transitions:
- ON -> OFF
- OFF -> ONAnd to transition between states it you'll need to trigger the machine by providing input, e.g:
binary_sensor:
- platform: gpio
pin: D6
name: "Button"
filters:
- delayed_on: 100ms
on_press:
- state_machine.transition: TOGGLEinitial_state (Optional, string): The intial state of the state machine. Defaults to first defined state.
states (Required, list): The list of states that the state machine has.
on_transition automation and before after_transition. before_transition automation and before on_transition automation. set action or via initial_state. Will not trigger if new state is the same as current state.inputs (Required, list): The list of inputs that the state machine supports with allowed state transitions.
FROM_STATE -> TO_STATE, or advanced configuration:on_input automation and before on_transition automation. on_leave automation and before on_enter automation. on_enter automation.before_transition will be called. This automation will not be called if transition is invalid.diagram (Optional, boolean): If true, then a diagram of the state machine will be output to the console during validation/compilation of YAML. See Diagrams section below for more details. Defaults to false.
Note:
Any running state machine automations (state, input and transition) will be stopped before running next automations. This is useful when there's a delayed transition in one of the automation and it needs to be cancelled because a new input was provided which results in a different transition.
When State Machine receives input (via transition action) it will call the automation specified in the input, transition, the "from" state and the "to" state in the following order:
on_inputbefore_transitionon_leaveon_transitionon_enterafter_transitionSee test.yaml for a demonstration of running order of these automations within a context of simple ON-OFF state machine.
state_machine.transition ActionYou can provide input to the state machine from elsewhere in your WAML file with the state_machine.transition action.
# in some trigger
on_...:
# Basic:
- state_machine.transition: TOGGLE
# Advanced (if you have multiple state machines in one YAML)
- state_machine.transition:
id: sm1
input: TOGGLEConfiguration options:
state_machine.set ActionThis action allows resetting the state machine current state, without going through transitions. This can be useful when initial state is not really known until some sensor data is available.
Note that only the target state
on_setautomation will be triggered, and all other state machine automations (on_enter,on_leaveandactionof the inputs and transitions) will be skipped.
# in some trigger
on_...:
# Basic:
- state_machine.set: OPEN
# Advanced (if you have multiple state machines in one YAML)
- state_machine.set:
id: sm1
state: OPENConfiguration options:
state_machine.state ConditionThis condition lets you check what state the machine is currently in.
# in some trigger
on_...:
# Basic
if:
condition:
state_machine.state: "ON"
then:
- logger.log: Is ON
# Advanced
if:
condition:
state_machine.state:
id: sm1
value: "ON"
then:
- logger.log: Is ONstate_machine.transition ConditionThis condition lets you check what transition last occurred.
# in some trigger
on_...:
# Basic
if:
condition:
state_machine.transition:
trigger: TOGGLE
then:
- logger.log: Toggled
# Advanced
if:
condition:
state_machine.transition:
id: sm1
from: "OFF"
trigger: TOGGLE
to: "ON"
then:
- logger.log: Turned on by toggletext_sensor.state_machine SensorThis is a simple text sensor to expose the current state of the state machine:
text_sensor:
- platform: state_machine
name: On/Off Toggle StateYou have an option to generate a diagram of the state machine. You can enable this by adding diagram: to the state machine yaml declaration (disabled by default). Currently supported formats are mermaid and dot
When compiling or validating your YAML a state machine diagram will be output to the console.
Mermaid is a JavaScript based diagramming and charting tool that takes Markdown-inspired text definitions and creates diagrams dynamically in the browser.
Github supports Mermaid diagrams emdded into Martkdown as code snippets via ```mermaid ... ```.
When using mermaid diagram the following will be output to console:
Here's an example:
stateDiagram-v2
direction LR
[*] --> OFF
ON --> OFF: TOGGLE
OFF --> ON: TOGGLEstateDiagram-v2
direction LR
[*] --> OFF
ON --> OFF: TOGGLE
OFF --> ON: TOGGLEDOT DOT is a graph description language.
When using dot diagram the following will be output to console:
Here's an example:
INFO State Machine Diagram (for On/Off Toggle State Machine):
https://quickchart.io/graphviz?format=svg&graph=digraph%20%22On/Off%20Toggle%20State%20Machine%22%20%7B%0A%20%20node%20%5Bshape%3Dellipse%5D%3B%0A%20%20ON%20-%3E%20OFF%20%5Blabel%3DTOGGLE%5D%3B%0A%20%20OFF%20-%3E%20ON%20%5Blabel%3DTOGGLE%5D%3B%0A%7D
INFO DOT language graph:
digraph "On/Off Toggle State Machine" {
node [shape=ellipse];
ON -> OFF [label=TOGGLE];
OFF -> ON [label=TOGGLE];
}Which corresponds to:
To get just the url use this command:
esphome config 2>&1 | grep quickchart.ioTo open the diagram in Chrome use this command:
esphome config 2>&1 | grep quickchart.io | xargs open -n -a "Google Chrome" --args "-0"To get a PNG image instead of SVG change the
formatparameter in the image url fromsvgtopng.
stateDiagram-v2
direction LR
[*] --> OFF
ON --> OFF: TOGGLE
OFF --> ON: TOGGLEThis example illustrates toggling an LED using a button.
See toggle-example.yaml.
stateDiagram-v2
direction LR
[*] --> OFF
ON --> OFF: CLICK
OFF --> ON: CLICK
EDITING --> EDITING: CLICK
ON --> EDITING: HOLD
EDITING --> ON: HOLDThis example models a single button control for a dimmable light with the following functionality:
See dimmable-light-example.yaml.
…This example demonstrates using 2 switches to control a time-based cover:
No open issues yet, or sync has not completed.