Define variables

The Define variables building block sets variables that templates in the next steps can use. Use it when you want to give a value a clear name, or calculate a value once and use it in several steps. For variables that the whole script can use, refer to script variables.

Using this building block from the user interface

  1. Go to Settings > Automations & scenes and select an automation. To edit a script, go to the Scripts tab and select the script.
  2. In the Then do section of an automation, or the Sequence section of a script, select Add action.
  3. Select Blocks, then select Define variables.
    • Result: The building block opens in YAML.
  4. Under variables, enter each variable as a name and a value, for example, brightness: 100.
  5. Select Save.

Options in the UI

This building block has no options in the UI. You enter the variables in YAML.

Using this building block in YAML

In YAML, use variables. This example sets two variables, and uses them in the next step:

ActionAn action makes Home Assistant do something, for example, turn on a light, send a notification, or activate a scene. Automations and scripts run a list of actions. [Learn more]
- alias: "Set variables"
  variables:
    entities:
      - light.kitchen
      - light.living_room
    brightness: 100
- alias: "Control lights"
  action: light.turn_on
  target:
    entity_id: "{{ entities }}"
  data:
    brightness: "{{ brightness }}"

The value of a variable can be a template:

ActionAn action makes Home Assistant do something, for example, turn on a light, send a notification, or activate a scene. Automations and scripts run a list of actions. [Learn more]
- alias: "Set a templated variable"
  variables:
    blind_state_message: "The blind is {{ states('cover.blind') }}."
- alias: "Notify about the state of the blind"
  action: notify.send_message
  target:
    entity_id: notify.my_device
  data:
    message: "{{ blind_state_message }}"

Options in YAML

variables map Required

The variables to set, as names and values. Values can be templates.

Good to know

  • If a variable with the same name was defined earlier, Define variables changes its value. This also works from inside another building block, such as If-then.
  • If no variable with that name was defined earlier, the variable is available in the rest of the automation or script run.

Examples

Script: Count the people who are home

This script starts with people set to 0. If Paulus is home, a Define variables step inside If-then adds 1. After the If-then, people still has the new value.

YAML example
ScriptA script is a saved list of steps that runs when you start it, for example, from a dashboard, with Assist, or from an automation. Unlike an automation, a script has no triggers, so it doesn't start by itself. [Learn more]
sequence:
  # Set the people variable to a default value
  - variables:
      people: 0
  # Try to increment people if Paulus is home
  - if:
      - condition: state
        entity_id: device_tracker.paulus
        state: "home"
    then:
      - variables:
          people: "{{ people + 1 }}"
          paulus_home: true
      # "People at home: 1"
      - action: notify.notify
        data:
          message: "People at home: {{ people }}"
  # Variable value is now updated
  # "People at home: 1 (including Paulus)"
  - action: notify.notify
    data:
      message: >
        People at home: {{ people }}
        {% if paulus_home is defined %}(including Paulus){% endif %}