Query
The Query action runs a read-only SELECT query against a database and returns the rows it finds.
This is useful when you want an automation or script to pull data on demand, for example to look up recent history or summarize values, without creating a dedicated sensor for it. Only SELECT statements are allowed.
This action does not support targets. In the UI, you are not prompted to choose an area, device, entity, or label. Instead, you enter the query to run, and optionally the database to run it against.
Using this action from the user interface
If you prefer building automations and scripts visually, Home Assistant walks you through this action step by step. You pick what to target, tweak a few options, and save. No YAML knowledge required.
To run a query from an automation or a script:
- Go to Settings > Automations & scenes.
- Open an existing automation or script, or select Create automation > Create new automation.
- If you’re setting up a new automation, add a trigger in the When section. Scripts don’t need a trigger. They run when something else calls them.
- In the Then do section, select Add action.
- From the search box, search for and select SQL: Query.
- Enter the Query, and optionally a Database URL.
- In the Response variable field, enter a name to store the result, for example,
query_result. - Select Save.
Options in the UI
The URL of the database to connect to. If not provided, the default Home Assistant recorder database is used.
Using this action in YAML
If you work directly in YAML, or you want to know exactly what Home Assistant does under the hood, this section has the technical reference. It lists the field names you use in YAML, their types, and which ones are required.
In YAML, refer to this action as sql.query. Because this action returns data, use response_variable to capture the result. A basic example looks like this:
action: sql.query
data:
query: |-
SELECT
states.state,
last_updated_ts
FROM
states
INNER JOIN states_meta ON
states.metadata_id = states_meta.metadata_id
WHERE
states_meta.entity_id = 'sun.sun'
ORDER BY
last_updated_ts DESC
LIMIT
3;
response_variable: sun_history
This runs the query and stores the result in the sun_history variable.
Options in YAML
The URL of the database to connect to. If not provided, the default Home Assistant recorder database is used.
Response data
The action returns a result, which is a list of rows. Each row is a mapping of column names to their values.
The data returned by the database is converted to be compatible with the action response. The following conversions are applied:
-
Decimaltypes are converted to floats. -
dateanddatetimeobjects are converted to ISO 8601 formatted strings. -
bytesandbytearrayare converted to a hexadecimal string prefixed with0x. - All other basic types (string, integer, float, and boolean) are returned as is.
For the example above, the response looks similar to this:
result:
- state: below_horizon
last_updated_ts: 1760634101.8498254
- state: below_horizon
last_updated_ts: 1760633981.849044
- state: below_horizon
last_updated_ts: 1760633861.848531
Try it yourself
Ready to test this? Open Developer tools > Actions, search for this action, fill in the fields, and select Perform action. You see what happens on your actual entitiesAn entity represents a sensor, actor, or function in Home Assistant. Entities are used to monitor physical properties or to control other entities. An entity is usually part of a device or a service. [Learn more] without writing a line of YAML.
Still stuck?
The Home Assistant community is quick to help: join Discord for real-time chat, post on the community forum with the action you’re calling and what you expected to happen, or share on our subreddit /r/homeassistant.
AI assistants like ChatGPT or Claude can also explain actions or suggest the right one when you describe what you want in plain language.