Home Assistant Scripting Essentials
Introduction to Home Assistant Scripts
What Is a Script?
In Home Assistant, a script is a sequence of actions you can run on command. Think of it like a recipe. A recipe lists steps to follow in order: first, preheat the oven; second, mix the ingredients; third, bake for 30 minutes. A script is just like that, but for your smart home devices.
You create a script to bundle a series of actions into a single, reusable command. For example, instead of manually turning off three different lights, locking the front door, and adjusting the thermostat every night, you can create a "Good Night" script that does all of that for you with one tap.
A script is a defined sequence of actions that Home Assistant can execute.
This makes complex routines simple and ensures they run the same way every time. You can trigger a script manually from your dashboard, call it from within an automation, or even have another script run it.
Scripts vs. Automations vs. Scenes
It's easy to confuse scripts with automations and scenes. They all manage your smart home devices, but they serve different purposes. The key difference lies in how and why they run.
| Feature | Automation | Scene | Script |
|---|---|---|---|
| What it is | A rule that runs automatically | A snapshot of device states | A sequence of actions |
| How it starts | An event happens (trigger) | You activate it | You call it (manually or from an automation) |
| Main Job | Reacts to changes in your home | Sets a group of devices to a specific state | Performs a series of steps in order |
| Example | When motion is detected, then turn on the light. | "Movie Mode" sets lights to 10% and turns on the TV. | "Leave Home" turns off all lights, then locks the door. |
An automation has a trigger. It watches for something to happen, like a door opening or the sun setting, and then performs an action. It's reactive.
A scene doesn't perform actions in a sequence; it just sets a group of devices to a predefined state all at once. It's about achieving a specific look or feel instantly.
A script is the action-oriented tool. It has no trigger of its own. It's a procedure waiting to be called.
The Anatomy of a Script
Scripts are written in YAML, the same language used for most Home Assistant configurations. At its core, a script is a named list of actions filed under a sequence.
script:
good_morning:
alias: Good Morning
sequence:
- service: light.turn_on
target:
entity_id: light.bedroom_lamp
data:
brightness_pct: 50
- delay: '00:00:05'
- service: media_player.play_media
target:
entity_id: media_player.kitchen_speaker
data:
media_content_id: 'spotify:playlist:your_playlist_uri'
media_content_type: 'playlist'
Let's break that down:
good_morning: This is the unique ID for the script. You'll use this to call it.alias: Good Morning: This is the friendly name that appears in the Home Assistant interface.sequence:: This is the heart of the script. It's a list of all the actions to perform, in order.- service:: Each item in the sequence is an action. Here, we call thelight.turn_onservice and themedia_player.play_mediaservice.delay:: This is a special action that pauses the script. In this example, the light turns on, the script waits for five seconds, and then the music starts playing.
Each action follows the same pattern: you specify a service to call, a target device or entity, and any data the service needs, like brightness or which song to play.
When to Use a Script
So, when should you reach for a script instead of an automation or a scene?
1. When you want to run the same actions from multiple places. Imagine you have a routine to turn off all lights and lock the doors. You might want to trigger this with a button on your dashboard, but also have an automation that runs it automatically at midnight. Instead of defining the actions in both places, you create one script and have the button and the automation both call it. This keeps your setup clean and easy to maintain. If you need to add a step, you only change it in one place.
2. When you need to perform a sequence with delays. Scripts excel at ordered tasks with pauses. For example, a script to water the garden could turn on the sprinkler, wait 10 minutes, turn it off, wait an hour, and then turn on a different sprinkler. This kind of timed sequence is exactly what scripts are built for.
3. When an automation's action block becomes too complex.
Sometimes, an automation needs to do a lot of things. If you find your action section growing into a long, complicated list, it's often better to move that logic into a script. The automation simply triggers the script, keeping your logic organized and easier to read.
Use a script to define a reusable routine. Use an automation to decide when that routine should run.
Now that you understand the what, why, and how of scripts, you're ready to start building your own.