home-assistant.io/source/_docs/scripts/conditions.markdown
Franck Nijhof c464056402
Making our website faster, cleaner and prettier (#9853)
* 🔥 Removes octopress.js

* 🔥 Removes use of root_url var

* 🔥 Removes Octopress generator reference from feed

* 🔥 Removes delicious support

* 🔥 Removes support for Pinboard

* 🔥 Removes support for Disqus

* 🔥 Removes support for Google Plus

* ↩️ Migrate custom after_footer to default template

* ↩️ Migrate custom footer to default template

* ↩️ Migrate custom header to default template

* 🔥 Removes unused template files

* 🚀 Places time to read directly in post template

* 🚀 Removes unneeded capture from archive_post.html template

* 🔥 🚀 Removes unused, but heaving sorting call in component page

* 🚀 Merged javascripts into a single file

* 🔥 Removes more uses of root_url

* 🚀 Removal of unneeded captures from head

* 🔥 🚀 Removal of expensive liquid HTML compressor

* 🔥 Removes unneeded templates

* 🚀 Replaces kramdown with GitHub's CommonMark 🚀

* 💄 Adds Prism code syntax highlighting

*  Adds support for redirect in Netlify

* ↩️ 🔥 Let Netlify handle all developer doc redirects

* ✏️ Fixes typo in redirects file: Netify -> Netlify

* 🔥 Removes unused .themes folder

* 🔥 Removes unused aside.html template

* 🔥 Removes Disqus config leftover

* 🔥 Removes rouge highlighter config

* 🔥 Removes Octopress 🎉

* 💄 Adjust code block font size and adds soft wraps

* 💄 Adds styling for inline code blocks

* 💄 Improve styling of note/warning/info boxes + div support

* 🔨 Rewrites all note/warning/info boxes
2019-07-15 22:17:54 +02:00

8.0 KiB

title description redirect_from
Conditions Documentation about all available conditions. /getting-started/scripts-conditions/

Conditions can be used within a script or automation to prevent further execution. 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.

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:

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.

OR condition

Test multiple conditions in 1 condition statement. Passes if any embedded condition is valid.

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.

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.

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.

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.

condition:
    condition: state  # 'day' condition: from sunrise until sunset
    entity_id: sun.sun
    state: 'above_horizon'
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.

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 %}
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.

The sunset/sunrise conditions do not work in locations inside the polar circles, and also not in locations with highly skewed local time zone.

It is advised to use conditions evaluating the solar elevation instead of the before/after sunset/sunrise conditions.

condition:
  condition: sun
  after: sunset
  # Optional offset value - in this case it must from -1 hours relative to sunset, or after
  after_offset: "-01:00:00"
condition:
    condition: or  # 'when dark' condition: either after sunset or before sunrise - equivalent to a state condition on `sun.sun` of `below_horizon`
    conditions:
      - condition: sun
        after: sunset
      - condition: sun
        before: sunrise
condition:
    condition: and  # 'when light' condition: before sunset and after sunrise - equivalent to a state condition on `sun.sun` of `above_horizon`
    conditions:
      - condition: sun
        before: sunset
      - condition: sun
        after: sunrise

A visual timeline is provided below showing an example of when these conditions will be true. In this chart, sunrise is at 6:00, and sunset is at 18:00 (6:00 PM). The green areas of the chart indicate when the specified conditions will be true.

Graphic showing an example of sun conditions

Template condition

The template condition will test if the given template renders a value equal to true. This is achieved by having the template result in a true boolean expression or by having the template render 'true'.

condition:
  condition: template
  value_template: "{% raw %}{{ (state_attr('device_tracker.iphone', 'battery')|int) > 50 }}{% endraw %}"

Within an automation, template conditions also have access to the trigger variable as described here.

Time condition

The time condition can test if it is after a specified time, before a specified time or if it is a certain day of the week

condition:
  condition: time
  # At least one of the following is required.
  after: '15:00:00'
  before: '02:00:00'
  weekday:
    - mon
    - wed
    - fri

Valid values for weekday are mon, tue, wed, thu, fri, sat, sun. Time condition windows can span across the midnight threshold. In the example above, the condition window is from 3pm to 2am.

A better weekday condition could be by using the Workday Binary Sensor.

Zone condition

Zone conditions test if an entity is in a certain zone. For zone automation to work, you need to have setup a device tracker platform that supports reporting GPS coordinates. Currently this is limited to the OwnTracks platform and the iCloud platform.

condition:
  condition: zone
  entity_id: device_tracker.paulus
  zone: zone.home

Examples

    condition:
      - condition: numeric_state
        entity_id: sun.sun
        value_template: '{{ state.attributes.elevation }}'
        below: 1
      - condition: state
        entity_id: light.living_room
        state: 'off'
      - condition: time
        before: '23:00:00'
        after: '14:00:00'
      - condition: state
        entity_id: script.light_turned_off_5min
        state: 'off'