The "scripts" of Home Assistant, what they are and how they are used

3 minutes of reading

An important bow arrow of users of Home Assistant - especially alongside the automations - are the "script". In reality, for the sake of clarity, it might have been better to call them "sequences" (but so much so), because this is what it is.Home Assistant Official Logo

In practice the entities of type "Script", when evoked, perform a series of sequential actions, which can also be conditioned by conditions (condition), execution delays (delay) and waits (wait).

Define a script

The definition of one or more entity of type "Script" is rather simple, especially for those who are familiar with the automations of Home Assistant.

It is in fact sufficient to add in configuration a similar block to the following:

script:
  ritorno_a_casa:
    sequence:
      - service: light.turn_on
        data:
          entity_id: group.ingresso
          brightness: 100
      - delay:
          minutes: 1
      - service: switch.turn_on
        entity_id: switch.acqua

The above configuration defines an entity called "script.ritorno_a_casa"Which, if evoked by the service"script.turn_on” prosees the lighting of a group of lights ("group.ingresso"), Wait a minute and then open the water in the house by activating it switch.acqua (imagining it as an actuator connected to a solenoid valve integrated a Home Assistant).

As the most attentive will have noticed, the definition of sequentiality is in all respects similar to that definable within a common block “action” of an automation.

Obviously inside the block "script:"You can define how many scripts you want, for example:

script:
  uscita_di_casa:
    sequence:
      - service: light.turn_off
        entity_id: group.ingresso
      - service: switch.turn_off
        entity_id: switch.acqua
  ritorno_a_casa:
    sequence:
      - service: light.turn_on
        data:
          entity_id: group.ingresso
          brightness: 100
      - delay:
          minutes: 1
      - service: switch.turn_on
        entity_id: switch.acqua

Variables

The adoption of the scripts allows, at the time of their execution, the passage of variables. It is a trivial as well as versatile concept: this allows you to define scripts that vary their behavior based on the variables that are passed to them.

Let's say for example that you have defined a script that provveda to the volume adjustment of all Alexa devices integrated as "Media Player” present in the house:

script:
  volume_alexa:
    sequence:
      - service: media_player.volume_set
        data_template:
          entity_id: media_player.alexa_sala, media_player.alexa_camera
          volume_level: '{{ volumelevel }}'

This script ("script.volume_alexa") prosees to evoke the service "media_player.volume_set"Requiring the two entities indicated to regulate the profirst volume through the parameter “volume_level", To which the variable is passed"VolumeLevel"

Evoking this script via automation is quite simple:

automation:
  alias: "Regolazione volume"
  trigger: [] # qualcosa che inneschi l'automazione
  condition: []
  action:
    - service: script.turn_on
      entity_id: script.volume_alexa
      data:
        variables:
          volumelevel: 0.5

Condition (condition)

The scripts are executed sequentially, from top to bottom. In the sequence it is possible introduce one or more conditions (also nested) in order to allow the script to profollow or not in the execution based on the occurrence or not of the defined condition.

Let's take the following example:

script:
  accensione_luce_condizionata:
    sequence:
      - condition: numeric_state
        entity_id: sensor.broadlink_sensor_light
        below: 2
      - service: light.turn_on
        entity_id: light.sala

In this example "script.accensione_luce_condizionata"When executed, before turning on the light"light.sala"Verifies the light conditions starting from the sensor"sensor.broadlink_sensor_light”Deriving from the integration of a sensor Broadlink A1 e-Air. If the sensor detects a degree of luminosity greater than or equal to 2, the sequence stops and the light is not switched on.

Delay (delay)

The particle "delay"Allows you to temporarily interrupt - for a defined time - the execution of a script.
The configuration possibilities are varied. Some examples:

# Attende un'ora
- delay: '01:00'
# Attende un minuto e 30 secondi
- delay: '00:01:30'
# Attende un minuto
- delay:
    # Supporta millisecondi, secondi, minuti, ore, giorni
    minutes: 1
# Attende un numero di secondi preimpostato tramite l'entità input_number.second_delay
- delay:
    # Supporta millisecondi, secondi, minuti, ore, giorni
    seconds: "{{ states('input_number.second_delay') }}"
# Attende un numero di secondi preimpostato tramite l'entità input_number.minut_delay
# Formati validi includono HH:MM e HH:MM:SS
- delay: "{{ states('input_number.minute_delay') | multiply(60) | timestamp_custom('%H:%M:%S',False) }}"

Wait

Unlike "delay", the particle "wait_template”Suspends execution pending the occurrence of a given condition, typically a (specific) change of state of an entity. Obviously it is possible to configure the "wait" in order to unblock the situation in the face of the passage of a specific period of time, as well as telling them what to do, then, in this case.

Let's look at an example:

# Attende che il sensore binary_sensor.ingresso rilevi l'apertura di un varco
- wait_template: "{{ is_state('binary_sensor.entrance', 'on') }}"
  timeout: '00:01:00'
  continue_on_timeout: 'false'

The sequence does not proyields beyond this block until the sensor status becomes "on"(Within one minute). If the sensor status no becomes "on"Within one minute, the sequence stops.

trigger

Starting a script can be done in various ways; the service to be used is always the same, or script.turn_on. Typically used in automations of Home Assistant, it can also be useful to manually trigger the scripts using of virtual switches.


More information on syntax scripts are available on the dedicated page at the site of Home Assistant.

Define virtual switches to command sequences (scripts) Home Assistant


Home Assistant Official LogoATTENZIONE: remember that there is on our FORUM community an ad hoc section dedicated to Home Assistant, for any doubt, question, information on the specific merit of these components.


Please comment below