--- title: "Conditions" description: "Documentation about all available conditions." redirect_from: /getting-started/scripts-conditions/ --- Conditions can be used within a script or automation to prevent further execution. When a condition does not return true, the script or automation will stop executing. A condition will look at the system right now. 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. ### AND condition Test multiple conditions in 1 condition statement. Passes if all embedded conditions are valid. ```yaml condition: 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 also just 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/). ### OR condition Test multiple conditions in 1 condition statement. Passes if any embedded condition is valid. ```yaml condition: condition: or conditions: - 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 1 condition statement. Passes if any embedded conditions is valid. 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 ``` ### Numeric state condition This type of condition attempts to parse the state of specified entity as a number and triggers if the value matches the thresholds. If both `below` and `above` are specified, both tests have to pass. You can optionally use a `value_template` to process the value of the state before testing it. ```yaml condition: condition: numeric_state entity_id: sensor.temperature above: 17 below: 25 # If your sensor value needs to be adjusted value_template: {% raw %}{{ float(state.state) + 2 }}{% endraw %} ``` ### State condition Tests if an entity is a specified state. ```yaml condition: condition: state entity_id: device_tracker.paulus state: 'not_home' # optional: trigger only if state was this for last X time. for: hours: 1 minutes: 10 seconds: 5 ``` ### Sun condition #### Sun state condition The sun state can be used to test if the sun has set or risen. ```yaml condition: condition: state # 'day' condition: from sunrise until sunset entity_id: sun.sun state: 'above_horizon' ``` ```yaml condition: 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 ```yaml condition: condition: and # 'twilight' condition: dusk and dawn, in typical locations conditions: - condition: template value_template: {% raw %}'{{ state_attr("sun.sun", "elevation") < 0 }}'{% endraw %} - condition: template value_template: {% raw %}'{{ state_attr("sun.sun", "elevation") > -6 }}'{% endraw %} ``` ```yaml condition: condition: template # 'night' condition: from dusk to dawn, in typical locations value_template: {% raw %}'{{ 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]. [sun_trigger]: /docs/automation/trigger/#sun-trigger