Master Home Assistant Scripts: Quick Automation Tricks
Home Assistant (HA) is the Swiss Army knife of smart homes. It lets you orchestrate lights, locks, sensors, and even your coffee machine with the click of a button or a single line of YAML. If you’re looking to take your automation from “I wish it would” to “watch me, I’ve got this,” you’re in the right place.
Why Scripts Matter
A script in HA is a reusable block of actions. Think of it as a recipe you can call from anywhere—another script, an automation, or even your mobile app. Scripts keep your configuration DRY (Don’t Repeat Yourself) and make complex logic easier to read.
Here’s what you’ll get from mastering scripts:
- Simplicity: One script, many triggers.
- Flexibility: Pass variables on the fly.
- Debugging ease: Log output in a single place.
- Performance: Reduce automation churn by batching actions.
Getting Started: The Basic Skeleton
Let’s create a quick script that turns on the living room lights and sets the thermostat. Open scripts.yaml
(or use the UI) and paste this:
turn_on_living_room:
alias: "Turn On Living Room"
description: "Lights + Thermostat in one go."
fields:
brightness:
description: "Desired light brightness (0-255)."
example: 200
sequence:
- service: light.turn_on
target:
entity_id: light.living_room_main
data:
brightness: "{{ brightness default(200) }}"
- service: climate.set_temperature
target:
entity_id: climate.living_room_thermostat
data:
temperature: 22
Notice the fields section? That’s how you pass arguments when calling the script. The {{ brightness default(200) }}
Jinja expression lets you override the default.
Triggering Scripts from Automations
Automation is the engine; scripts are the pistons. Let’s wire up an automation that calls our script when motion is detected after sunset.
automation:
- alias: "Auto Lights on Motion"
trigger:
platform: state
entity_id: binary_sensor.motion_living_room
to: "on"
condition:
- condition: sun
after: sunset
action:
service: script.turn_on_living_room
data:
brightness: 180
Because we passed brightness: 180
, the script overrides the default. If you omit it, HA falls back to 200
.
Nested Scripts: The Power of Composition
You can call scripts from other scripts. This is handy when you have a common “goodnight” routine that turns off lights, locks doors, and sets the thermostat.
goodnight:
alias: "Good Night Routine"
sequence:
- service: script.turn_off_all_lights
- service: lock.lock
target:
entity_id: lock.front_door
- service: climate.set_temperature
data:
temperature: 18
Now, just add a single line to any automation:
- service: script.goodnight
Passing Variables Dynamically
Sometimes you need to feed runtime data into a script. For example, adjusting brightness based on the time of day.
dynamic_brightness:
alias: "Dynamic Brightness"
fields:
time_of_day:
description: "Sunrise or sunset."
example: sunrise
sequence:
- service_template: >
{% if time_of_day == 'sunrise' %}
light.turn_on
{% else %}
light.turn_off
{% endif %}
target:
entity_id: light.living_room_main
Here we use service_template
, a powerful feature that lets you choose the service at runtime based on Jinja logic.
Debugging Tips: Logging Inside Scripts
When scripts get complex, you’ll want to see what’s happening. Use logger.log
to dump values.
- service: logger.log
data:
level: debug
message: "Brightness set to {{ brightness }}"
Check the Logbook or Developer Tools → Logbook to see the output.
Optimizing Performance: Batching vs. Individual Calls
Each service call costs a tiny bit of processing time. If you’re turning on 10 lights, batching them into one call is faster.
Instead of:
- service: light.turn_on
entity_id: light.living_room_main
- service: light.turn_on
entity_id: light.living_room_side
Do this:
- service: light.turn_on
target:
entity_id:
- light.living_room_main
- light.living_room_side
data:
brightness: 200
Batching reduces network chatter and speeds up the overall execution.
Security Considerations
Scripts can control critical devices (locks, garage doors). Restrict who can run them:
- Use
allowlist_entities
in the script definition. - Enable Two-Factor Authentication on your HA instance.
- Audit the logbook regularly for unexpected script executions.
A Practical Example: Morning Routine
Let’s build a complete “Morning” script that:
- Turns on bedroom lights.
- Sets the thermostat to a cozy 21°C.
- Starts the coffee maker.
morning_routine:
alias: "Morning Routine"
sequence:
- service: light.turn_on
target:
entity_id: light.bedroom_main
data:
brightness: 150
- service: climate.set_temperature
target:
entity_id: climate.bedroom_thermostat
data:
temperature: 21
- service: switch.turn_on
target:
entity_id: switch.coffee_maker
Trigger it with a time-based automation:
- alias: "Start Morning Routine at 7 AM"
trigger:
platform: time
at: "07:00:00"
action:
service: script.morning_routine
Wrapping It Up: The Power of Scripts in HA
Scripts are the unsung heroes that let you:
- Create reusable logic.
- Pass dynamic data with ease.
- Keep your YAML clean and maintainable.
- Optimize performance by batching actions.
By mastering scripts, you turn your Home Assistant installation from a collection of isolated automations into a cohesive, intelligent system that reacts to context and time. Give it a try—your smart home will thank you.
Conclusion
Scripts in Home Assistant are like the duct tape of automation: they bind everything together, simplify complexity, and make your life easier. Start small—create a script to turn on a single light—and grow from there. Once you’re comfortable, experiment with dynamic variables, nested scripts, and performance optimizations.
Remember: Write once, reuse everywhere. Happy automating!
Leave a Reply