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.
- Go to Settings -> Devices & Services -> Helpers and click Create Helper -> State Machine.
- Enter a State Machine Name (for example,
Dryer, which createssensor.dryer). - Edit the State Machine Configuration directly in the UI—the editor starts pre-populated with a working YAML example and accepts either YAML or JSON.
- Home Assistant stores the configuration in the helper's config entry options. You can update it anytime from the helper's Configure button.
- To test transitions manually, open Developer Tools -> Actions, select
state_machine.trigger(orstate_machine.set_state), target yoursensor.<name>entity, and enter a trigger name.
In addition to the standard helper dialog, the integration registers a built-in State Machine Editor panel (accessible via Settings -> Devices & Services -> State Machine -> Configure or /state_machine) featuring:
- Live YAML and JSON editing with
fsm.schema.jsonvalidation squiggles, hover tooltips, and autocompletion. - A live read-only state diagram preview updating as you edit.
When you submit a configuration in Home Assistant's standard helper setup dialog, every validation issue found in the schema is also listed with its exact path (such as state.status or transitions.STARTING.timeout.after).
If you author or edit configurations in an external editor such as VS Code, you can get live autocompletion and schema validation using fsm.schema.json:
- JSON: Add
"$schema": "./fsm.schema.json"(or the raw GitHub URLhttps://raw.githubusercontent.com/edalquist/ha_state_machine/main/fsm.schema.json) to your JSON file, then remove"$schema"before pasting into Home Assistant if desired (Home Assistant ignores unknown top-level keys, whilefsm.schema.jsonexplicitly allows"$schema"). - YAML: Add a modeline at the top of your YAML file for the Red Hat YAML extension (
yaml-language-server):# yaml-language-server: $schema=https://raw.githubusercontent.com/edalquist/ha_state_machine/main/fsm.schema.json
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.
- Normal transition (
trigger: TARGET): Transitions toTARGETand startsTARGET'stimeoutif one is configured. - Self-transition (
trigger: CURRENT_STATE): Remains in the current state and resets any activetimeouttimer. - Explicitly ignored trigger (
trigger: ""ortrigger: null/ blank in YAML): Registers the trigger name as valid on the FSM while treating it as a no-op in that state. Firing it is silently ignored and does not reset active timeouts. - Omitted vs. undefined triggers: Triggers omitted from the current state (but defined elsewhere in the FSM) are also silently ignored without resetting timeouts, whereas triggers not defined anywhere in the FSM raise a validation error.
Use "*" as a source state key under transitions to define global transitions that apply from any state without repeating them under every state:
- State-specific transitions take precedence over
"*"for that state. - State-specific explicit ignores (
""ornull) suppress a"*"transition in that state (without resetting active timeouts). timeoutrules are not supported under"*", and"*"cannot be used asstate.statusor as a destination state.
state:
status: OFF
transitions:
"*":
power_off: OFF
OFF:
power_off: "" # already off; ignore without resetting any timer
start: RUNNING
RUNNING:
pause: PAUSED
PAUSED:
resume: RUNNINGTracks 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: 10Because State Machine helpers are created under Helpers, debug logging is configured via Home Assistant's logger integration rather than a per-device integration toggle.
Add to configuration.yaml:
logger:
default: info
logs:
custom_components.state_machine: debugOr enable it temporarily without restarting via Developer Tools -> Actions using logger.set_level:
action: logger.set_level
data:
custom_components.state_machine: debugHasmtools 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.
To rebuild the bundled frontend editor (custom_components/state_machine/frontend/state-machine-panel.js) after modifying files in frontend/src/:
cd frontend
npm ci
npm run typecheck
npm test
npm run build