Skip to main content

Home Assistant States

An IoT device or data from a service is represented as one or more entities in Home Assistant. An entity in the core is represented as a state. Each state has an identifier for the entity in the format of <domain>.<object_id>, a state and attributes that further describe the state. An example of this would be with the state on and attributes describing the color and the brightness.

The <domain> part of an entity identifier is equal to the Home Assistant component that is maintaining the state. This domain can be used to figure out what kind of state attributes to expect. See the entity documentation for more information about the different entities and their data.


All states are stored in the database in a table named states.

The difference between last_changed and last_updated is that last_changed only updates when the state value was changed while last_updated is updated on any change to the state, even if that included just attributes. Example: if a light turns on, the state changes from off to on, so both last_updated and last_changed will update. If a light changes color from red to blue, only the state attributes change. In this case only last_updated will change. By distinguishing between these two values, we can easily identify how long a light has been on and how long it has been on the current color/brightness.

The last_changed field is not stored in the database when it is the same as the last_updated field. See Fetching the last_changed when it is NULL for queries to populate the value when it is NULL.

state_idColumn(Integer, primary_key=True)
event_idColumn(Integer, ForeignKey('events.event_id'), index=True)
last_updatedColumn(DateTime(timezone=True), default=datetime.utcnow, index=True)
old_state_idColumn(Integer, ForeignKey("states.state_id"), index=True)
attributes_idColumn(Integer, ForeignKey("state_attributes.attributes_id"), index=True)
context_idColumn(String(36), index=True)

The created field is no longer stored in the states table to avoid duplicating data in the database as it was always the same as last_updated and the matching state_change event's time_fired.

As many attributes are the same, attributes are stored in the state_attributes table with many to one relationship:

attributes_idColumn(Integer, primary_key=True)
hashColumn(BigInteger, index=True)
shared_attrsColumn(Text().with_variant(mysql.LONGTEXT, "mysql"))

Below is an example query to find attributes that were recordered after the change over.

states Indices

ix_states_entity_id_last_updatedentity_id, last_updated

Example queries

Significant states

Users are usually not so interested in state updates that only changed the attributes. Attribute only changes can be triggered by a light changing color or a media player changing song (which happens every ~3 minutes!). Since we maintain both last_changed and last_updated, it's easy to filter for just the states where the state was changed:

SELECT * FROM states WHERE last_changed = last_updated

Linking a new state to an old state

After startup, once a state is changed, the id of the old state is stored as old_state_id, making it easy to find the previous state.

SELECT * FROM states LEFT JOIN states as old_states ON states.old_state_id = old_states.state_id

Fetching attributes

Attributes are stored in the state_attributes table.

SELECT * FROM states LEFT JOIN state_attributes ON states.attributes_id = state_attributes.attributes_id

Attributes can be found in the following order state_attributes.shared_attrs or states.attributes. As new states are recorded states.attributes will be phased out.

Fetching the last_changed when it is NULL


select entity_id,state,last_updated,iif(last_changed is NULL,last_updated,last_changed) as last_changed from states;

MySQL & MariaDB

select entity_id,state,last_updated,if(last_changed is NULL,last_updated,last_changed) as last_changed from states;


select entity_id,state,last_updated,(case when last_changed is NULL then last_updated else last_changed end) from states;