Skip to content

About

A State Machine integration for Home Assistant

Topics

Resources

Stars

16 stars

Watchers

1 watching

Forks

Repository files navigation

Finite State Machine Sensor

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.

Installation

Open your Home Assistant instance and open a repository inside the Home Assistant Community Store.

Features

  • YAML or JSON Configuration UI: Uses Home Assistant's syntax-highlighted ObjectSelector editor (with automatic migration from v1 schema_json entries).
  • Enum Sensor & State Attributes: Exposes all configured states as SensorDeviceClass.ENUM options for dropdowns in the Automation UI, plus available_triggers, previous_state, and timeout_at attributes.
  • Event-Loop Timeouts & Logbook Events: Supports per-state timeout rules scheduled directly on Home Assistant's event loop, emitting state_machine_timeout events with context attribution.
  • State & Deadline Persistence: Preserves the current state, previous_state, and active timeout_at deadline 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.

Configuration

Creating & Editing a State Machine

  1. Go to Settings -> Devices & Services -> Helpers and click Create Helper -> State Machine.
  2. Enter a State Machine Name (for example, Dryer, which creates sensor.dryer).
  3. 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.
  4. Home Assistant stores the configuration in the helper's config entry options. You can update it anytime from the helper's Configure button.
  5. To test transitions manually, open Developer Tools -> Actions, select state_machine.trigger (or state_machine.set_state), target your sensor.<name> entity, and enter a trigger name.

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.

Trigger Behavior

  • Normal transition (trigger: TARGET): Transitions to TARGET and starts TARGET's timeout if one is configured.
  • Self-transition (trigger: CURRENT_STATE): Remains in the current state and resets any active timeout timer.
  • Explicitly ignored trigger (trigger: "" or trigger: 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.

Wildcard Transitions ("*")

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 ("" or null) suppress a "*" transition in that state (without resetting active timeouts).
  • timeout rules are not supported under "*", and "*" cannot be used as state.status or 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: RUNNING

Example Config

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: IDLE

Equivalent 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" }
    }
  }
}

Example Automation

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: 10

Debug Logging

Because 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: debug

Or enable it temporarily without restarting via Developer Tools -> Actions using logger.set_level:

action: logger.set_level
data:
  custom_components.state_machine: debug

Related Software

hasmtools

Hasmtools 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.

About

A State Machine integration for Home Assistant

Topics

Resources

Stars

16 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages