Home Assistant Guide

Simple tutorials for powerful automations

Automations vs. Scripts: When to Use Each

In Home Assistant, automations and scripts are both ways to make things happen in your smart home. They often look similar, but they serve different purposes and are used in different situations. This article breaks down the differences in simple terms, so you'll know when to use each - and includes practical examples.

What Is an Automation?

An automation is a set of instructions that runs when something specific happens. It's like saying, "If this happens, then do that." Automations always start with a trigger - an event that causes the automation to run.

  • Trigger: What starts the automation (e.g. sunset, motion detected, a button pressed).
  • Condition (optional): Checks if certain requirements are met before continuing (e.g. only if someone is home).
  • Actions: What should happen when triggered (e.g. turn on a light, send a notification).

What Is a Script?

A script is a series of actions that you can run manually, from another automation, or using a button in the dashboard. Think of it as a reusable set of steps, but unlike automations, scripts don't have triggers - they only run when you tell them to.

  • Scripts are great for reusing logic you want in several places.
  • They let you define custom fields - like variables you can pass in when you run them - adding flexibility. For example, you can allow a "message" field for custom notifications, or an "entity_id" field to target different devices - without changing the script's logic.

When Should I Use an Automation?

  • Whenever you want something to happen automatically in response to an event (like a sensor changing state, a certain time, etc.).
  • Examples:
    • Turn on your porch light when the sun sets.
    • Send a notification if a window is opened while you're away.
    • Start a morning routine when your alarm goes off.

When Should I Use a Script?

  • When you want to manually start a set of actions, or reuse them in multiple automations.
  • Examples:
    • "Good Night" script: Turns off all lights, locks the doors, sets the alarm.
    • A script to flash certain light(s), or gradually brighten or dim them.

Practical

Example: Automation Turning On the Hall Light at Sunset

Goal: Turn on the hall light automatically every day at sunset.

automation:
  - alias: "Turn on Hall Light at Sunset"
    trigger:
      - platform: sun
        event: sunset
    action:
      - type: turn_on
        entity_id: light.hall

Example: Script to Set an Android Phone Alarm

This script lets you set an alarm on any Android phone in your household (that uses the Home Assistant companion app, Google Clock, and has the right permissions). By using fields and selectors, you can pick both the phone and the time in the UI - no need to edit code each time.

set_android_alarm:
  alias: Set Android Alarm
  description: Set an alarm on the Android phone
  fields:
    alarm_time:
      name: Alarm Time
      description: Time to set the alarm for
      example: "08:00:00"
      selector:
        time: null
      required: true
    phone:
      name: Phone
      description: >
        The phone to set the alarm on (must be an Android phone, with HA companion
        app & Google clock installed, and correct permissions set)
      selector:
        device:
          integration: mobile_app
  sequence:
    - variables:
        hour: "{{ alarm_time.split(':')[0] | int }}"
        minute: "{{ alarm_time.split(':')[1] | int }}"
    - service: notify.mobile_app_clk_nx1
      data:
        message: command_activity
        data:
          ttl: 0
          priority: high
          intent_package_name: com.google.android.deskclock
          intent_action: android.intent.action.SET_ALARM
          intent_extras: >
            android.intent.extra.alarm.HOUR:{{ hour }},
            android.intent.extra.alarm.MINUTES:{{ minute }},
            android.intent.extra.alarm.SKIP_UI:true
  mode: single
  • Fields & Selectors: Users can pick a time with a time picker, and select their phone from a list of available devices, making this script easy to use in dashboards or automations.
  • Reusable & Adaptable: You can call this script from anywhere - voice, dashboard, or another automation - and it works for any phone, any time, no code changes needed.
  • Behind the Scenes: The script extracts the hour and minute from the chosen time and sends an Android intent via a notification to set the alarm silently.

Key Differences at a Glance

  • Automations: Always have a trigger. Used for "when X happens, do Y."
  • Scripts: No triggers. Used for reusable or complex actions. Must be started by something else (a button, automation, or voice command).

Summary Table

Automation Script
Has triggers? Yes No
Runs automatically? Yes No (must be called)
Reusable in other automations? Not usually Yes
Manual control? No Yes
Use for complex steps? Possible, but scripts are better Best choice

Tip: Scripts Make Great Dashboard Buttons

Want a button on your Home Assistant dashboard that runs a specific set of actions? Scripts are perfect for this! You can add a script to your dashboard as a button, and tapping it will run all the actions you've defined - like locking all doors, sending a custom notification, or even starting your favorite "Good Morning" routine.

  • Simple scripts: Add a button that performs a single action, or a sequence (e.g. turn off all lights, then set the alarm).
  • Scripts with fields: Some dashboard cards allow you to input variables (fields) before running the script, making them interactive.
  • Visual customization: You can style the button, add icons, and even use conditions to show/hide it.

To add a script as a button, use the "Button" or "Entity" card and select your script. Scripts will appear as script.name_of_your_script in the entity picker.

Tip for Beginners

If you're not sure which to use, start with an automation. If you find yourself repeating the same steps in several automations, or you need a sequence you want to trigger in different ways (manually, by schedule, or by another automation), move that logic into a script.