Window closed
The Window closed trigger fires when a targeted window closes. Use it to restore heating after airing out a room, confirm that windows are shut before bedtime, or start an automation only after a window has stayed closed for a while.
Using this trigger from the user interface
If you prefer building automations visually, Home Assistant walks you through this trigger step by step. You pick what to watch, tweak a few options, and save. No YAML knowledge required.
To use this trigger in an automation:
- Go to Settings > Automations & scenes.
- Open an existing automation, or select Create automation > Create new automation.
- In the When section, select Add trigger.
- Select what you want to monitor. Under By target (see Targets), pick the area the window is in, like your bedroom or kitchen. You can also select a floor, a device, a specific entity, or a label.
- From the triggers shown for that target, select Window closed.
- Under Trigger when (see Behavior), pick Each, First, or All to control how the trigger behaves when multiple windows are targeted.
- Under For at least, set how long the window must stay closed before the trigger fires. Leave it at zero to fire immediately.
- Select Save.
Options in the UI
When multiple windows are targeted, controls when the trigger fires. Pick Each to fire every time any targeted window closes, First to fire only when the first targeted window closes, or All to fire only after every targeted window is closed.
Using this trigger in YAML
If you work directly in YAML, or you want to know exactly what Home Assistant does under the hood, this section has the technical reference. It lists the field names you use in YAML, their types, and which ones are required. For how an automation is built in YAML, refer to Automations in YAML.
In YAML, put the settings that are specific to this trigger under options. Keep target, if the trigger uses one, and general settings such as id, alias, enabled, and variables at the top level, next to trigger.
When this trigger starts an automation, it passes data about what happened to the automation. You can use this trigger data in the templates of your conditions and actions. To see which data is available, refer to Trigger data in templates.
In YAML, refer to this trigger as window.closed. A basic example looks like this:
trigger: window.closed
target:
entity_id: binary_sensor.bedroom_window
This fires when binary_sensor.bedroom_window closes.
Options in YAML
When multiple windows are targeted, controls when the trigger fires. Accepts each, first, or all.
Duration the window must stay closed before the trigger fires. Accepts a duration string like 00:05:00 for five minutes. For a list of all accepted formats, refer to Durations in automations and scripts.
Targets of the trigger
This trigger requires a target. The target is the object that Home Assistant will watch. You can select a single entityAn entity represents a sensor, actor, or function in Home Assistant. Entities are used to monitor physical properties or to control other entities. An entity is usually part of a device or a service. [Learn more], a device, an area, a floor, or a label as a target, and Home Assistant will watch every matching window sensor or motorized window behind that target.
-
Entity: one specific window sensor or motorized window, such as
binary_sensor.living_room_window. - Device: every window sensor or motorized window that belongs to a device.
- Area: every window sensor or motorized window in a room or area.
- Floor: every window sensor or motorized window on a floor.
- Label: every window sensor or motorized window that shares a label.
You can also select different target types in one trigger. For example, you can add a specific entity and an area as targets in the same trigger to monitor both of them at once.
To learn which entities Home Assistant includes for a device, area, floor, or label, and how it handles several entities, refer to Automation targets.
Trigger variables
When the trigger reacts, the trigger variable identifies the entity that made it react. This is useful when a target contains multiple entities, such as a device, area, floor, or label.
The following trigger-specific variables are available:
-
trigger.entity_id: Entity ID of the entity that made the trigger react. -
trigger.from_state: Previous state object of that entity. -
trigger.to_state: New state object of that entity. -
trigger.for: Timedelta object of how long the trigger criteria were met, if applicable.
For example, use {{ trigger.to_state.name }} in an action to include the name of the entity that made the trigger react in a notification.
The trigger variable describes the entity that made the trigger react. It doesn’t contain all the entities of the target.
Behavior with multiple targets
When you target more than one entity (or select an area, floor, or label that contains several), the Trigger when option controls how the trigger responds:
- Each (default): the trigger fires every time any one of the targeted entities transitions. For example, if you monitor three motion sensors in the living room and someone walks past sensor 1, the automation fires. When they walk past sensor 2 a moment later, it fires again. Every individual event counts.
- First: the trigger fires only on the first transition in the targeted group, then waits until none of the targeted entities is in that state anymore before it fires again. For example, if you monitor the same three motion sensors, the automation fires when the first one picks up movement (someone entered the room). The other two firing afterward are ignored, so you get one notification per “someone walked in” event instead of three.
- All: the trigger fires only after the last targeted entity in the group has fired, meaning all of them are now in the expected state. For example, if you monitor the lights in the living room, bedroom, and hallway, the automation fires only once all three have turned off. This is useful for scenarios like “start the robot vacuum only after every light on the floor is off,” so you know the room is truly empty.
Good to know
- Use a binary sensor or cover that uses the window device class.
- The trigger only fires when a window transitions from a known, valid state. If a window comes back from being unavailable (
unavailable) or having an unknown state (unknown), the trigger does not fire for that recovery. - To react when a window opens instead, use Window opened.
Try it yourself
Ready to test this? Go to Settings > Automations & scenes, create a new automation, and add this trigger. Save the automation, and then make the trigger react. For example, change the state of the targeted entityAn entity represents a sensor, actor, or function in Home Assistant. Entities are used to monitor physical properties or to control other entities. An entity is usually part of a device or a service. [Learn more], turn on a light that the trigger monitors, or change its brightness. To check whether the automation ran, open its traces.
More examples
Real scenarios where this trigger fires in automations and scripts. Copy any example and adapt it to your setup. In the visual editor, select the UI options listed in the example when they are available, or paste the YAML code.
You don’t need to edit YAML to use these examples. Copy a YAML snippet from this page, open the visual automation editor in Home Assistant, and press Ctrl+V (or Cmd+V on Mac). Home Assistant automatically converts the pasted YAML into the visual editor format, whether it’s a full automation, a single trigger, a condition, or an action.
Automation: turn heating back on when bedroom window has been closed for 2 minutes
After you air out a room, it can help to wait until the window is fully closed before heating the room again.
- Trigger: Window closed
- Target: Bedroom window sensor
- Trigger when: Each
- For at least: 00:02:00
-
Action: Set thermostat HVAC mode
- Target: Bedroom thermostat
- HVAC mode: Heat
YAML example for restoring heating after a window closes
alias: "Resume bedroom heating when the window closes"
triggers:
- trigger: window.closed
target:
entity_id: binary_sensor.bedroom_window
options:
behavior: each
for: "00:02:00"
actions:
- action: climate.set_hvac_mode
target:
entity_id: climate.bedroom
data:
hvac_mode: heat
Automation: lock patio door after all ground floor windows are closed
When everyone finishes airing out the house, you can wait until every ground floor window is shut before locking the patio door.
- Trigger: Window closed
- Target: Ground-floor windows label
- Trigger when: All
- For at least: 00:01:00
- Action: Lock lock
YAML example for locking a door after all windows close
alias: "Lock the patio door when all ground-floor windows are closed"
triggers:
- trigger: window.closed
target:
label_id: ground_floor_windows
options:
behavior: all
for: "00:01:00"
actions:
- action: lock.lock
target:
entity_id: lock.patio_door
Still stuck?
To find out why the trigger didn’t react, make the change that it should react to, and then open the traces of the automation. If the automation didn’t run, there’s usually no trace, but some triggers record a Did not trigger trace. For details, refer to Testing automations. For common problems and their solutions, refer to Troubleshooting automations.
If that doesn’t help, the Home Assistant community is quick to help: join Discord for real-time chat, post on the community forum with the trigger you’re using and what you expected to happen, or share on our subreddit /r/homeassistant.
AI assistants like ChatGPT or Claude can also explain triggers or suggest the right one when you describe what you want in plain language.
Related triggers
Explore related triggers:
- Window opened: Triggers when one or more windows open.