mirror of
https://github.com/home-assistant/home-assistant.io.git
synced 2025-05-02 17:18:58 +00:00
57 lines
1.8 KiB
Markdown
57 lines
1.8 KiB
Markdown
---
|
|
title: "Automation Conditions"
|
|
description: "Automations can test conditions when invoked."
|
|
---
|
|
|
|
Conditions are an optional part of an automation rule. They can be used to prevent the automation's actions from being run. After a trigger occurred, all conditions will be checked. If any of them do not return true, the automation will stop executing. Conditions look very similar to triggers, but they are very different — a trigger will look at events happening in the system, while a condition only looks at how the system looks right now. A trigger can observe that a switch is being turned on. A condition can only see if a switch is currently on or off.
|
|
|
|
The available conditions for an automation are the same as for the script syntax so see that page for a [full list of available conditions](/docs/scripts/conditions/).
|
|
|
|
Example of using condition:
|
|
|
|
{% raw %}
|
|
|
|
```yaml
|
|
automation:
|
|
- alias: "Turn on office lights"
|
|
trigger:
|
|
- platform: state
|
|
entity_id: sensor.office_motion_sensor
|
|
to: "on"
|
|
condition:
|
|
- or:
|
|
- condition: numeric_state
|
|
entity_id: sun.sun
|
|
attribute: elevation
|
|
below: 4
|
|
- condition: numeric_state
|
|
entity_id: sensor.office_lux_sensor
|
|
below: 10
|
|
action:
|
|
- service: scene.turn_on
|
|
target:
|
|
entity_id: scene.office_lights
|
|
```
|
|
|
|
{% endraw %}
|
|
|
|
The `condition` option of an automation, also accepts a single condition template directly. For example:
|
|
|
|
{% raw %}
|
|
|
|
```yaml
|
|
automation:
|
|
- alias: "Turn on office lights"
|
|
trigger:
|
|
- platform: state
|
|
entity_id: sensor.office_motion_sensor
|
|
to: "on"
|
|
condition: "{{ state_attr('sun.sun', 'elevation') < 4 }}"
|
|
action:
|
|
- service: scene.turn_on
|
|
target:
|
|
entity_id: scene.office_lights
|
|
```
|
|
|
|
{% endraw %}
|