Home Assistant Scripting: Automate Tomorrow, Today
Welcome, fellow smart‑home enthusiasts! If you’ve ever dreamed of waking up to the aroma of freshly brewed coffee, having your lights dance in sync with your favorite playlist, or simply letting Home Assistant (HA) handle the boring bits while you binge‑watch your shows, you’re in the right place. In this post we’ll walk through the fundamentals of HA scripting and automation rules, sprinkle in some best‑practice tips, and end with a practical example that you can copy‑paste right into your automations.yaml
. Let’s get automating!
What Are Scripts and Automations?
Scripts are reusable blocks of actions you can trigger manually, via services, or from other automations. Think of them as a recipe you can call whenever you want.
Automations are event‑driven. They consist of a trigger, optional condition(s), and one or more actions. Whenever the trigger fires, HA evaluates the conditions; if they pass, the actions execute.
In short:
- Scripts: “Do this when I say so.”
- Automations: “Do this when something happens.”
Getting Started: File Structure
Home Assistant stores configurations in config/
. Two files are key for this guide:
scripts.yaml
– holds all your script definitions.automations.yaml
– holds all your automation definitions.
If you haven’t yet, add them to configuration.yaml
:
scripting: !include scripts.yaml
automation: !include automations.yaml
Now you’re ready to roll!
Building a Simple Script
Let’s create a script that turns on the living‑room lights, plays your favorite playlist, and sets the thermostat to 22°C. Open scripts.yaml
and add:
turn_on_living_room:
alias: "Living Room Warm‑Up"
description: Turns on lights, plays music, and sets temperature.
mode: single
sequence:
- service: light.turn_on
target:
entity_id: light.living_room_main, light.living_room_ambient
data:
brightness_pct: 70
- service: media_player.play_media
target:
entity_id: media_player.spotify_living_room
data:
media_content_type: music
media_content_id: "spotify:user:your_spotify_user:playlist:37i9dQZF1DXcBWIGoYBM5M"
- service: climate.set_temperature
target:
entity_id: climate.living_room_thermostat
data:
temperature: 22
Key points:
- alias gives a human‑readable name.
- mode: single prevents overlapping runs.
- Use
target:
instead of the olderentity_id:
for cleaner syntax.
Crafting an Automation
Now let’s automate that script so it runs at sunset and when you say “Hey Google, start my evening.” Create the following in automations.yaml
:
- alias: "Evening Routine"
description: Starts the living‑room warm‑up at sunset and on voice command.
trigger:
- platform: sun
event: sunset
- platform: voice_command
command_type: text
command: "start my evening"
condition:
- condition: state
entity_id: input_boolean.evening_mode
state: "on"
action:
- service: script.turn_on_living_room
Why the input_boolean.evening_mode
?
- It lets you toggle the routine on or off from the UI.
- Adding a condition keeps your house from over‑automating during the day.
Best Practices for Automations
- Use unique IDs: Add
id: evening_routine
to avoid duplicates after reloads. - Keep triggers simple: Complex logic goes in conditions or actions.
- Leverage templates: Use
{{ now().strftime("%H:%M") }}
for time‑based conditions. - Test incrementally: Create a single trigger, see it work, then add more.
Advanced Scripting: Conditional Logic
Suppose you want the lights to dim if it’s already dark. Add a choose
action:
- service: script.turn_on_living_room
data:
brightness_pct: <?= state_attr('light.living_room_main', 'brightness') default(255) / 2 ?>
But that looks messy. HA’s choose
block is cleaner:
sequence:
- choose:
- conditions:
- condition: numeric_state
entity_id: sensor.lux_living_room
below: 50
sequence:
- service: light.turn_on
target:
entity_id: light.living_room_main
data:
brightness_pct: 30
default:
- service: light.turn_on
target:
entity_id: light.living_room_main
data:
brightness_pct: 70
Now the lights automatically adapt to ambient light.
Using Templates for Dynamic Actions
Templates let you pull in sensor data or calculate values on the fly. For example, to set the thermostat based on outside temperature:
- service: climate.set_temperature
target:
entity_id: climate.living_room_thermostat
data:
temperature: <?= (states('sensor.outdoor_temperature') float) + 2 ?>
Tip: Wrap your template in {{ }}
for readability.
Debugging: How to See What’s Happening
Tool | Description |
---|---|
Developer Tools → Logs |
Shows runtime errors and action failures. |
Developer Tools → Events |
Subscribe to automation.triggered to watch triggers. |
History |
Visual timeline of entity states. |
When an automation fails, the log will often include a stack trace pointing to the offending line.
Performance Tips
- Avoid loops: Scripts that call themselves recursively can hang HA.
- Use
delay
sparingly: Over‑using delays can block the event loop. - Prefer
service_call
overscript.turn_on
for simple one‑liners. - Keep your YAML tidy: Use
yamllint
or the HA editor’s linting.
Putting It All Together: A Real‑World Example
Below is a full automation that:
- Triggers at sunset or when you say “Good night.”
- Checks if the bedroom door is closed.
- Turns off all lights, locks doors, and sets the thermostat to eco mode.
- id: good_night_routine
alias: "Good Night Routine"
description: Secure the house and save energy at night.
trigger:
- platform: sun
event: sunset
- platform: voice_command
command_type: text
command: "good night"
condition:
-
Leave a Reply