Creates helper sensors whose state is backed by a Finite State Machine (FSM), along with Home Assistant actions to trigger transitions, override states, and run automatic state timeouts.
- YAML or JSON Configuration UI: Uses Home Assistant's syntax-highlighted
ObjectSelectoreditor (with automatic migration from v1schema_jsonentries). - Enum Sensor & State Attributes: Exposes all configured states as
SensorDeviceClass.ENUMoptions for dropdowns in the Automation UI, plusavailable_triggers,previous_state, andtimeout_atattributes. - Event-Loop Timeouts & Logbook Events: Supports per-state
timeoutrules scheduled directly on Home Assistant's event loop, emittingstate_machine_timeoutevents with context attribution. - State & Deadline Persistence: Preserves the current state,
previous_state, and activetimeout_atdeadline across Home Assistant restarts (resuming remaining time or transitioning immediately if a timeout expired while offline). - Actions:
state_machine.trigger: Executes a named trigger if valid from the current state (raises a validation error for undefined triggers; safely ignores triggers valid only from other states).state_machine.set_state: Explicitly overrides the current state to any valid state in the schema.
Configured via a schema close to Node-RED's Finite State Machine using either YAML or JSON in the integration config/options flow.
A special timeout rule is supported on any state for time-based automatic transitions (after in seconds, to destination state). Terminal states with no outgoing transitions can be specified as {} or implicitly referenced as a destination state.
Tracks the state of a dryer starting up, running, and stopping. The automation sends above, below, and middle triggers based on the dryer's power consumption. Automatic transitions happen from STARTING to RUNNING after 1 second, from STOPPING to DONE after 2 minutes, and from DONE to IDLE after 15 seconds.
state:
status: IDLE
transitions:
IDLE:
above: STARTING
STARTING:
timeout:
after: 1
to: RUNNING
below: IDLE
RUNNING:
below: STOPPING
STOPPING:
timeout:
after: 120
to: DONE
above: RUNNING
middle: RUNNING
DONE:
timeout:
after: 15
to: IDLEEquivalent JSON (also accepted by the editor and matching example.json):
{
"state": {
"status": "IDLE"
},
"transitions": {
"IDLE": {
"above": "STARTING"
},
"STARTING": {
"timeout": { "after": 1, "to": "RUNNING" },
"below": "IDLE"
},
"RUNNING": {
"below": "STOPPING"
},
"STOPPING": {
"timeout": { "after": 120, "to": "DONE" },
"above": "RUNNING",
"middle": "RUNNING"
},
"DONE": {
"timeout": { "after": 15, "to": "IDLE" }
}
}
}Uses the FSM above, configured as sensor.dryer. Triggers monitor a dryer power sensor (sensor.dryer_power) and react when the FSM transitions to DONE.
The automation uses state_machine.trigger to send triggers to sensor.dryer based on current power consumption, and sends a notification when the cycle reaches DONE.
alias: "!Laundry: Dryer Notifier"
description: ""
triggers:
- trigger: state
entity_id:
- sensor.dryer
to: DONE
id: DRYER_DONE
- trigger: numeric_state
entity_id: sensor.dryer_power
above: 200
id: FSM:above
- trigger: numeric_state
entity_id: sensor.dryer_power
below: 20
id: FSM:below
- trigger: numeric_state
entity_id: sensor.dryer_power
above: 20
below: 200
id: FSM:middle
conditions: []
actions:
- choose:
- conditions:
- condition: template
alias: FSM Event
value_template: "{{ trigger.id.startswith('FSM:') }}"
sequence:
- action: state_machine.trigger
data:
trigger: "{{ trigger.id[4:] }}"
target:
entity_id: sensor.dryer
- conditions:
- condition: trigger
id: DRYER_DONE
sequence:
- action: notify.iq_notify_parents
data:
message: >-
Dryer is Done{{ '<br>Washer Is Still Running' if
states('sensor.washer') not in ['DONE', 'IDLE'] else '' }}
title: Laundry
data:
mode: only_home_then_away
mode: queued
max: 10Hasmtools is a small python utility to visualize the state machine and convert the JSON configuration to/from YAML, started as a simple proof of concept. Code and examples are available on GitHub and the package can be found on PyPI.