--- title: "Conditions" description: "Documentation about all available conditions." toc: true no_toc: true --- Conditions can be used within a script or automation to prevent further execution. When a condition evaluates true, the script or automation will be executed. If any other value is returned, the script or automation stops executing. A condition will look at the system at that moment. For example, a condition can test if a switch is currently turned on or off. Unlike a trigger, which is always `or`, conditions are `and` by default - all conditions have to be true. All conditions support an optional `alias`. {{ page.content | markdownify | toc_only }} ## Logical conditions ### AND condition Test multiple conditions in one condition statement. Passes if all embedded conditions are true. ```yaml condition: alias: "Paulus home AND temperature below 20" condition: and conditions: - condition: state entity_id: "device_tracker.paulus" state: "home" - condition: numeric_state entity_id: "sensor.temperature" below: 20 ``` If you do not want to combine AND and OR conditions, you can list them sequentially. The following configuration works the same as the one listed above: ```yaml condition: - condition: state entity_id: "device_tracker.paulus" state: "home" - condition: numeric_state entity_id: "sensor.temperature" below: 20 ``` Currently you need to format your conditions like this to be able to edit them using the [automations editor](/docs/automation/editor/). The AND condition also has a shorthand form. The following configuration works the same as the ones listed above: ```yaml condition: alias: "Paulus home AND temperature below 20" and: - condition: state entity_id: "device_tracker.paulus" state: "home" - condition: numeric_state entity_id: "sensor.temperature" below: 20 ``` ### OR condition Test multiple conditions in one condition statement. Passes if any embedded condition is true. ```yaml condition: alias: "Paulus home OR temperature below 20" condition: or conditions: - condition: state entity_id: "device_tracker.paulus" state: "home" - condition: numeric_state entity_id: "sensor.temperature" below: 20 ``` The OR condition also has a shorthand form. The following configuration works the same as the one listed above: ```yaml condition: alias: "Paulus home OR temperature below 20" or: - condition: state entity_id: "device_tracker.paulus" state: "home" - condition: numeric_state entity_id: "sensor.temperature" below: 20 ``` ### Mixed AND and OR conditions Test multiple AND and OR conditions in one condition statement. Passes if any embedded condition is true. This allows you to mix several AND and OR conditions together. ```yaml condition: condition: and conditions: - condition: state entity_id: "device_tracker.paulus" state: "home" - condition: or conditions: - condition: state entity_id: sensor.weather_precip state: "rain" - condition: numeric_state entity_id: "sensor.temperature" below: 20 ``` Or in shorthand form: ```yaml condition: and: - condition: state entity_id: "device_tracker.paulus" state: "home" - or: - condition: state entity_id: sensor.weather_precip state: "rain" - condition: numeric_state entity_id: "sensor.temperature" below: 20 ``` ### NOT condition Test multiple conditions in one condition statement. Passes if all embedded conditions are **not** true. ```yaml condition: alias: "Paulus not home AND alarm not disarmed" condition: not conditions: - condition: state entity_id: device_tracker.paulus state: "home" - condition: state entity_id: alarm_control_panel.home_alarm state: disarmed ``` The NOT condition also has a shorthand form. The following configuration works the same as the one listed above: ```yaml condition: alias: "Paulus not home AND alarm not disarmed" not: - condition: state entity_id: device_tracker.paulus state: "home" - condition: state entity_id: alarm_control_panel.home_alarm state: disarmed ``` ## Numeric state condition This type of condition attempts to parse the state of the specified entity or the attribute of an entity as a number, and triggers if the value matches the thresholds. If both `below` and `above` are specified, both tests have to pass. ```yaml condition: alias: "Temperature between 17 and 25 degrees" condition: numeric_state entity_id: sensor.temperature above: 17 below: 25 ``` You can optionally use a `value_template` to process the value of the state before testing it. {% raw %} ```yaml condition: condition: numeric_state entity_id: sensor.temperature above: 17 below: 25 # If your sensor value needs to be adjusted value_template: "{{ float(state.state) + 2 }}" ``` {% endraw %} It is also possible to test the condition against multiple entities at once. The condition will pass if **all** entities match the thresholds. ```yaml condition: condition: numeric_state entity_id: - sensor.kitchen_temperature - sensor.living_room_temperature below: 18 ``` Alternatively, the condition can test against a state attribute. The condition will pass if the attribute value of the entity matches the thresholds. ```yaml condition: condition: numeric_state entity_id: climate.living_room_thermostat attribute: temperature above: 17 below: 25 ``` Number helpers (`input_number` entities), `number` and `sensor` entities that contain a numeric value, can be used in the `above` and `below` options to make the condition more dynamic. ```yaml condition: condition: numeric_state entity_id: climate.living_room_thermostat attribute: temperature above: input_number.temperature_threshold_low below: input_number.temperature_threshold_high ``` ## State condition Tests if an entity has a specified state. ```yaml condition: alias: "Paulus not home for an hour and a bit" condition: state entity_id: device_tracker.paulus state: "not_home" # optional: Evaluates to true only if state was this for last X time. for: hours: 1 minutes: 10 seconds: 5 ``` It is also possible to test the condition against multiple entities at once. The condition will pass if **all** entities match the state. ```yaml condition: condition: state entity_id: - light.kitchen - light.living_room state: "on" ``` Instead of matching all, it is also possible if one of the entities matches. In the following example the condition will pass if **any** entity matches the state. ```yaml condition: condition: state entity_id: - binary_sensor.motion_sensor_left - binary_sensor.motion_sensor_right match: any state: "on" ``` Testing if an entity is matching a set of possible conditions; The condition will pass if the entity matches one of the states given. ```yaml condition: condition: state entity_id: alarm_control_panel.home state: - "armed_away" - "armed_home" ``` Or, combine multiple entities with multiple states. In the following example, both media players need to be either paused or playing for the condition to pass. ```yaml condition: condition: state entity_id: - media_player.living_room - media_player.kitchen state: - "playing" - "paused" ``` Alternatively, the condition can test against a state attribute. The condition will pass if the attribute matches the given state. ```yaml condition: condition: state entity_id: climate.living_room_thermostat attribute: fan_mode state: "auto" ``` Finally, the `state` option accepts helper entities (also known as `input_*` entities). The condition will pass if the state of the entity matches the state of the given helper entity. ```yaml condition: condition: state entity_id: alarm_control_panel.home state: input_select.guest_mode ``` You can also use templates in the `for` option. {% raw %} ```yaml condition: condition: state entity_id: device_tracker.paulus state: "home" for: minutes: "{{ states('input_number.lock_min')|int }}" seconds: "{{ states('input_number.lock_sec')|int }}" ``` {% endraw %} The `for` template(s) will be evaluated when the condition is tested. ### Sun condition #### Sun state condition The sun state can be used to test if the sun has set or risen. ```yaml condition: alias: "Sun up" condition: state # 'day' condition: from sunrise until sunset entity_id: sun.sun state: "above_horizon" ``` ```yaml condition: alias: "Sun down" condition: state # from sunset until sunrise entity_id: sun.sun state: "below_horizon" ``` ### Sun elevation condition The sun elevation can be used to test if the sun has set or risen, it is dusk, it is night, etc. when a trigger occurs. For an in-depth explanation of sun elevation, see [sun elevation trigger][sun_elevation_trigger]. [sun_elevation_trigger]: /docs/automation/trigger/#sun-elevation-trigger {% raw %} ```yaml condition: condition: and # 'twilight' condition: dusk and dawn, in typical locations conditions: - condition: template value_template: "{{ state_attr('sun.sun', 'elevation') < 0 }}" - condition: template value_template: "{{ state_attr('sun.sun', 'elevation') > -6 }}" ``` {% endraw %} {% raw %} ```yaml condition: condition: template # 'night' condition: from dusk to dawn, in typical locations value_template: "{{ state_attr('sun.sun', 'elevation') < -6 }}" ``` {% endraw %} ### Sunset/sunrise condition The sun condition can also test if the sun has already set or risen when a trigger occurs. The `before` and `after` keys can only be set to `sunset` or `sunrise`. They have a corresponding optional offset value (`before_offset`, `after_offset`) that can be added, similar to the [sun trigger][sun_trigger]. Note that if only `before` key is used, the condition will be true _from midnight_ until sunrise/sunset. If only `after` key is used, the condition will be true from sunset/sunrise _until midnight_. If both `before: sunrise` and `after: sunset` keys are used, the condition will be true _from midnight_ until sunrise **and** from sunset _until midnight_. If both `after: sunrise` and `before: sunset` keys are used, the condition will be true from sunrise until sunset. [sun_trigger]: /docs/automation/trigger/#sun-trigger