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:

  1. Go to Settings > Automations & scenes.
  2. Open an existing automation or script, or select Create automation > Create new automation.
  3. 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.
  4. In the Then do section, select Add action.
  5. From the search box, search for and select SQL: Query.
  6. Enter the Query, and optionally a Database URL.
  7. In the Response variable field, enter a name to store the result, for example, query_result.
  8. Select Save.

Options in the UI

Query (Required)

The SELECT query to run. Only SELECT statements are allowed.

Database URL (Optional)

The URL of the database to connect to. If not provided, the default Home Assistant recorder database is used.

Response variable (Optional)

The name of the variable where the result will be stored. If not provided, the result won´t be stored.

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:

ActionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called *sequence*. [Learn more]
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

query string Required

The SELECT query to run. Only SELECT statements are allowed.

db_url string

The URL of the database to connect to. If not provided, the default Home Assistant recorder database is used.

response_variable string

The name of the variable where the result will be stored. If not provided, the result won´t be stored.

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:

  • Decimal types are converted to floats.
  • date and datetime objects are converted to ISO 8601 formatted strings.
  • bytes and bytearray are converted to a hexadecimal string prefixed with 0x.
  • All other basic types (string, integer, float, and boolean) are returned as is.

For the example above, the response looks similar to this:

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

Tip

AI assistants like ChatGPT or Claude can also explain actions or suggest the right one when you describe what you want in plain language.