Automations in YAML

Automations are created in Home Assistant via the UI, but are stored in a YAMLYAML is a human-readable data serialization language. It is used to store and transmit data in a structured format. In Home Assistant, YAML is used for configuration, for example in the configuration.yaml or automations.yaml files. [Learn more] format. If you want to edit the YAMLYAML is a human-readable data serialization language. It is used to store and transmit data in a structured format. In Home Assistant, YAML is used for configuration, for example in the configuration.yaml or automations.yaml files. [Learn more] of an automationAutomations in Home Assistant allow you to automatically respond to things that happen in and around your home. [Learn more], select the automation, select the menu button in the top right then on Edit in YAML.

The UI will write your automations to automations.yaml. This file is managed by the UI and should not be edited manually.

It is also possible to write your automations directly inside configuration.yamlThe configuration.yaml file is the main configuration file for Home Assistant. It lists the integrations to be loaded and their specific configurations. In some cases, the configuration needs to be edited manually directly in the configuration.yaml file. Most integrations can be configured in the UI. [Learn more] or other YAMLYAML is a human-readable data serialization language. It is used to store and transmit data in a structured format. In Home Assistant, YAML is used for configuration, for example in the configuration.yaml or automations.yaml files. [Learn more] files. You can do this by adding a labeled automation block to your configuration.yaml:

# The configuration required for the UI to work
automation: !include automations.yaml

# Labeled automation block
automation kitchen:
  - triggers:
      - trigger: ...

You can add as many labeled automation blocks as you want.

After you change automations in a YAML file, reload them. For the steps, refer to Reloading automations.

Configuration Variables

alias string (Optional)

Name of the automation.

id string (Optional)

A unique ID for your automation. With an ID, you can change the name and entity ID in the UI, and the automation stores debug traces. The ID must be text. To use a number, put it in quotes, for example id: "1700000000000".

description string (Optional)

A description of the automation.

initial_state boolean (Optional)

Used to define the state of your automation at startup. When not set, the state will be restored from the last run. If there is no state to restore, the automation is turned on. See Automation initial state.

Default:

Restored from last run, otherwise true

trace map (Optional, default: {})

Configuration values for the traces stored, currently only stored_traces can be configured.

stored_traces integer (Optional, default: 5)

The number of traces which will be stored. See Number of debug traces stored.

variables map (Optional, default: {})

Variables that will be available inside your templates, both in conditions and actions.

PARAMETER_NAME any

The value of the variable. Any YAML is valid. Templates can also be used to pass a value to the variable.

trigger_variables map (Optional, default: {})

Variables that you can use in the templates of all triggers, for example, in a template trigger, in the enabled option of a trigger, in the event_type of an event trigger, in the webhook_id of a webhook trigger, or in the topic of an MQTT trigger. Home Assistant sets them when it sets up the triggers, for example, when it loads the automation or when you turn the automation on.

PARAMETER_NAME any

The value of the variable. Any YAML is valid. Only limited templates can be used.

mode string (Optional, default: single)

Controls what happens when the automation is invoked while it is still running from one or more previous invocations. See Automation modes.

max integer (Optional, default: 10)

Controls maximum number of runs executing and/or queued up to run at a time. Only valid with modes queued and parallel. The minimum is 2.

max_exceeded string (Optional, default: warning)

When max is exceeded (which is effectively 1 for single mode) a log message will be emitted to indicate this has happened. This option controls the severity level of that log message. See Log Levels for a list of valid options. Or silent may be specified to suppress the message from being emitted.

triggers list Required

The trigger(s) which will start the automation. Multiple triggers can be added and the automation will start when any of these triggers trigger.

alias string (Optional)

A name for the trigger. It’s shown in the automation editor and in the trace.

id string (Optional)

An ID that can be used in the automation to determine which trigger caused the automation to start. The ID must be text. To use a number, put it in quotes.

enabled boolean | template (Optional, default: true)

Set to false to disable the trigger. You can also use a limited template. For details, refer to Disabling a trigger.

variables map (Optional, default: {})

Variables that will be available in the conditions and action sequence.

PARAMETER_NAME any

The value of the variable. Any YAML is valid. Templates can also be used to pass a value to the variable.

conditions list (Optional)

Conditions that have to be true for the automation to run its actions. When a trigger starts the automation, Home Assistant checks the conditions. If they are not met, the automation stops. By default, all conditions listed have to be true. To change this, use the And, Or, and Not conditions.

actions list Required

The sequence of actions to be performed in the automation.

YAML example

Example of a YAMLYAML is a human-readable data serialization language. It is used to store and transmit data in a structured format. In Home Assistant, YAML is used for configuration, for example in the configuration.yaml or automations.yaml files. [Learn more] based automation that you can add to configuration.yamlThe configuration.yaml file is the main configuration file for Home Assistant. It lists the integrations to be loaded and their specific configurations. In some cases, the configuration needs to be edited manually directly in the configuration.yaml file. Most integrations can be configured in the UI. [Learn more].

# Example of entry in configuration.yaml
automation my_lights:
  # Turns on lights 1 hour before sunset if people are home
  # and if people get home between 16:00-23:00
  - alias: "Rule 1 Light on in the evening"
    triggers:
      # Prefix the first line of each trigger configuration
      # with a '-' to enter multiple
      - trigger: sun
        event: sunset
        offset: "-01:00:00"
      - trigger: state
        entity_id:
          - person.paulus
          - person.anne_therese
        to: "home"
    conditions:
      # Prefix the first line of each condition configuration
      # with a '-' to enter multiple
      - condition: state
        entity_id:
          - person.paulus
          - person.anne_therese
        match: any
        state: "home"
      - condition: time
        after: "16:00:00"
        before: "23:00:00"
    actions:
      # With a single action entry, we don't need a '-' before action - though you can if you want to
      - action: homeassistant.turn_on
        target:
          entity_id: light.living_room

  # Turn off lights when everybody leaves the house
  - alias: "Rule 2 - Away Mode"
    triggers:
      - trigger: state
        entity_id: zone.home
        to: "0"
    actions:
      - action: light.turn_off
        target:
          entity_id: all

  # Notify when Paulus leaves the house in the evening
  - alias: "Leave Home notification"
    triggers:
      - trigger: zone
        event: leave
        zone: zone.home
        entity_id: device_tracker.paulus
    conditions:
      - condition: time
        after: "20:00"
    actions:
      - action: notify.notify
        data:
          message: "Paulus left the house"

  # Send a notification via Pushover with the event of a Xiaomi cube. Custom event from the Xiaomi integration.
  - alias: "Xiaomi Cube Action"
    initial_state: false
    triggers:
      - trigger: event
        event_type: cube_action
        event_data:
          entity_id: binary_sensor.cube_158d000103a3de
    actions:
      - action: notify.pushover
        data:
          title: "Cube event detected"
          message: "Cube has triggered this event: {{ trigger.event }}"

Older YAML syntax

Older examples, such as in forum posts, guides, or blueprints, can use keys that have been renamed since. Home Assistant still accepts the old keys, so these examples keep working. When you save an automation in the editor, Home Assistant changes the keys to the current names.

The renamed keys are:

  • service: in an action is now action:.
    • Renamed in Home Assistant 2024.8.
  • platform: in a trigger is now trigger:.
    • Renamed in Home Assistant 2024.10.
  • The top-level trigger:, condition:, and action: keys of an automation are now triggers:, conditions:, and actions:.
    • Renamed in Home Assistant 2024.10.

At the automation level, triggers:, conditions:, and actions: are plural. The key that identifies a trigger or condition within those lists stays singular (trigger: or condition:), and an action call uses singular action:.

Use either the old key or the new key, not both in the same place. For example, an automation can’t have both trigger: and triggers:. Before you copy part of an older example into your automation, change its keys to the current names.

Additional options

Some options are only available when writing automations directly in YAMLYAML is a human-readable data serialization language. It is used to store and transmit data in a structured format. In Home Assistant, YAML is used for configuration, for example in the configuration.yaml or automations.yaml files. [Learn more] and cannot be configured through the user interface.

Automation initial state

At startup, automations by default restore their last state of when Home Assistant ran. This can be controlled with the initial_state option. Set it to false or true to force initial state to be off or on.

automation:
  - alias: "Automation Name"
    initial_state: false
    triggers:
      - trigger: ...

Number of debug traces stored

When using YAMLYAML is a human-readable data serialization language. It is used to store and transmit data in a structured format. In Home Assistant, YAML is used for configuration, for example in the configuration.yaml or automations.yaml files. [Learn more] you can configure the number of debugging traces stored for an automation. This is controlled with the stored_traces option under trace. Set stored_traces to the number of traces you wish to store for the particular automation. If not specified the default value of 5 will be used.

automation:
  - alias: "Automation Name"
    trace:
      stored_traces: 10
    triggers:
      - trigger: ...

Migrating your YAML automations to automations.yaml

If you want to migrate your manual automations to use the editor, you’ll have to copy them to automations.yaml. Make sure that automations.yaml remains a list! For each automation that you copy over, you’ll have to add an id. This can be any string as long as it’s unique.

# Example automations.yaml entry. Note, automations.yaml is always a list!
- id: my_unique_id  # <-- Required for editor to work, for automations created with the editor the id will be automatically generated.
  alias: "Hello world"
  triggers:
    - trigger: state
      entity_id: sun.sun
      from: below_horizon
      to: above_horizon
  conditions:
    - condition: numeric_state
      entity_id: sensor.temperature
      above: 17
      below: 25
      value_template: "{{ float(state.state) + 2 }}"
  actions:
    - action: light.turn_on

Removing an automation from a YAML file

To delete an automation that you manage in the UI, use the automation editor. For the steps, refer to Duplicating, turning off, or deleting an automation.

Before you remove an automation, check whether another automation, a script, or a dashboard uses it.

To remove an automation that you wrote in YAMLYAML is a human-readable data serialization language. It is used to store and transmit data in a structured format. In Home Assistant, YAML is used for configuration, for example in the configuration.yaml or automations.yaml files. [Learn more]:

  1. Open the YAML file that contains the automation, such as configuration.yaml. For how to set up file access and an editor, refer to Editing configuration.yaml.
    • If the automation is in automations.yaml and has an id, delete it in the automation editor instead.
  2. Delete the entire automation. It starts with a line that begins with -, such as - id:, - alias:, or - triggers:. It ends before the next line that begins with - at the same indentation.
    • If you split your automations into one file per automation with !include_dir_list, the file contains only this automation, without a - at the start. Delete the entire file instead.
  3. Check the configuration, and then reload your automations.
    • Result: The automation is removed. If it had an id, its entity stays until you delete it.
  4. If the automation had an id, delete its entity:
    1. Go to Settings > Devices & services > Entities.
    2. Select Enter selection mode, and then select the automation.
    3. Select the three dots menu, and then select Delete selected. Confirm the deletion.

Reloading automations

After you change automations in a YAML file, reload them to apply the changes. Reloading only affects the automations that you changed:

  • If one of them is running, the run stops.
  • If one of them has a For at least duration in progress, the duration is lost.

For details, refer to When a run stops before it finishes.

  1. Go to Settings > Tools > YAML.
  2. Under YAML configuration reloading, select Automations.

To reload automations from an automation or a script, use the Reload automations action.