Automations are the heart of ESPHome — they let your device respond to its environment entirely on-device, without depending on a network connection or a cloud service. Every automation is a pair: a trigger (the event that fires it) and one or more actions (what the device does in response). This page walks through the full automation system, from simple button presses to reusable scripts, recurring intervals, shared global variables, and Lambda C++ templates.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/esphome/esphome.io/llms.txt
Use this file to discover all available pages before exploring further.
How Automations Work
Every automation follows the same pattern: a trigger fires, then a list of actions executes sequentially.on_press attribute is the trigger. The then: block holds the actions. Actions run in order — the next action does not start until the previous one finishes (or, for async actions like delay, until the delay elapses).
You can chain as many actions as you like:
Common Triggers
on_boot and on_shutdown
Run actions once when the device boots up or cleanly shuts down. Useful for initialising outputs or publishing a startup message.
on_boot fires every time the ESP restarts — including after an OTA update or a watchdog reset. Use priority (higher value = runs earlier; common range –100 to 800) to order multiple on_boot blocks relative to each other. Default priority is 600.on_loop
Runs on every main-loop iteration (~millisecond cadence). Avoid heavy work here; prefer interval: for periodic tasks.
Sensor Triggers
Sensors expose several triggers you can hook into:| Trigger | Fires when… |
|---|---|
on_value | Sensor publishes any new reading |
on_value_range | Reading enters an above/below window |
on_raw_value | Raw (unfiltered) value is received |
Button / Binary Sensor Triggers
Common Actions
delay
Pauses the action list for a fixed or computed duration. The rest of the device continues running normally during the delay.
if
Branches based on a condition. Both then: and else: are optional.
while
Loops until a condition becomes false:
repeat
Executes a block a fixed number of times. The loop counter is available as iteration inside lambdas.
wait_until
Suspends the action list until a condition becomes true, with an optional timeout:
lambda
Execute arbitrary C++ code inline:
Scripts
Scripts let you define a named, reusable block of actions and call it from multiple places. They also support execution modes that control what happens when the script is called again while already running.| Mode | Behavior |
|---|---|
single | Ignore new calls while already running; issue a warning (default) |
restart | Stop current run and restart from the top |
queued | Queue new calls; run them after the current one finishes (max 5 total instances by default) |
parallel | Run new instances alongside existing ones (unlimited by default) |
Interval Component
Theinterval: component runs a block of actions repeatedly on a fixed schedule. It is lighter-weight than time.on_time and needs no real-time clock.
Global Variables
Globals store values that persist across automation runs and can be read or written from any lambda. They are declared at the top level and referenced by ID.globals.set action (no C++ needed):
Unlike sensors, globals do not use
.state — you access them directly as id(my_global). Wrapping initial_value in quotes is required because ESPHome passes it verbatim into C++.Lambda Templates (C++ Inside YAML)
Lambdas let you write C++ expressions directly in your YAML configuration. They are introduced with the!lambda tag (or implied in lambda: keys).
| Expression | What it returns |
|---|---|
id(my_sensor).state | Current float value of a sensor |
id(my_binary).state | true / false for a binary sensor |
id(my_switch).state | true / false for a switch |
id(my_text).state | std::string of a text sensor |
static:
Automations Without a Network
All automations run locally on the microcontroller. Your device will keep responding to buttons, sensors, and timers even with no Wi-Fi connection, no MQTT broker, and no Home Assistant instance reachable.ESPHome will reboot automatically if it cannot reach its API endpoint for an extended period (default: 15 minutes). You can adjust this with the
reboot_timeout option on the api:, wifi:, or mqtt: component if you need offline-only operation.