Skip to main content
The state machine is the heart of Home Assistant. It tracks the current state of all entities and fires events when states change. Understanding state management is crucial for building reliable integrations.

The State Machine

The StateMachine class manages all entity states in Home Assistant:
Reference: homeassistant/core.py:2058

State Objects

State Structure

The State object contains complete entity information:
Reference: homeassistant/core.py:1718

State vs Attributes

  • State: The primary status (on/off, open/closed, numeric value)
  • Attributes: Additional context (brightness, color, temperature)

Reading State

Get Single State

Reference: homeassistant/core.py:2142

Check State Value

Reference: homeassistant/core.py:2151

Get All States

Reference: homeassistant/core.py:2124

Get Entity IDs

Reference: homeassistant/core.py:2081

Setting State

Basic State Update

Reference: homeassistant/core.py:2247

State Update with Context

Context tracks what triggered the state change:
Reference: homeassistant/core.py:1213

Force Update

Force a state change event even if state hasn’t changed:
Reference: homeassistant/core.py:2247

State Change Events

EVENT_STATE_CHANGED

Fired when state or attributes change:
Reference: homeassistant/core.py:137

EVENT_STATE_REPORTED

Fired when state is updated but unchanged:
Reference: homeassistant/core.py:147

State Reservations

Reserve Entity ID

Reserve an entity ID before creating the entity:
Reference: homeassistant/core.py:2222

Check Availability

Reference: homeassistant/core.py:2239

Removing State

Remove Entity State

This fires EVENT_STATE_CHANGED with new_state=None. Reference: homeassistant/core.py:2169

State Persistence

Internal State Format

States are internally compressed for efficiency:
Reference: homeassistant/core.py:1708

Advanced State Patterns

State Validation

Reference: homeassistant/core.py:200

Entity ID Validation

Reference: homeassistant/core.py:192, homeassistant/core.py:171

Tracking Multiple Entities

Reference: homeassistant/helpers/event.py:309

State Attribute Updates

Update only attributes without changing state:

State Machine Indexes

The state machine maintains indexes for performance:

Domain Index

Reference: homeassistant/core.py:2016

Best Practices

1. Use Async Methods

Always use async methods in the event loop:

2. Check for None

Always handle missing states:

3. Use Appropriate Tracking

Use helpers instead of manual event listening:

4. Minimize State Updates

Only update when necessary:

5. Use Context

Provide context for state changes:

Performance Considerations

  1. State reads are fast - Direct dictionary lookup
  2. State writes fire events - Consider batch updates
  3. Domain filtering is optimized - Uses internal index
  4. Attributes are immutable - Uses ReadOnlyDict for safety

Common Pitfalls

  • Don’t modify state.attributes directly (it’s read-only)
  • Don’t assume entities exist (always check for None)
  • Don’t create excessive state updates (batch when possible)
  • Don’t store large data in attributes (use data registry)
  • Clean up entity states when removing entities

State Machine Lifecycle