自动化条件

条件是自动化规则的可选部分。它们可以用来防止自动化的动作被执行。在发生 trigger触发器是定义的一组平台值或条件,用于触发自动化运行。 [Learn more] 之后,所有条件将被检查。如果所有条件都返回真,则会执行自动化;否则,只要其中任何一个条件不返回真,自动化就会停止执行。

条件看起来与触发器非常相似,但它们是非常不同的——触发器会观察系统中发生的事件,而条件仅查看系统当前的状态。触发器可以观察到开关正在被打开。条件只能看到开关当前是开还是关。

自动化可用的条件与脚本语法中的条件相同,因此请查看该页面以获取可用条件的完整列表

使用条件的示例:

automation:
  - alias: "打开办公室灯"
    triggers:
      - trigger: state
        entity_id: sensor.office_motion_sensor
        to: "on"
    conditions:
      - or:
        - condition: numeric_state
          entity_id: sun.sun
          attribute: elevation
          below: 4
        - condition: numeric_state
          entity_id: sensor.office_lux_sensor
          below: 10
    actions:
      - action: scene.turn_on
        target:
          entity_id: scene.office_lights

自动化的 condition 选项也可以直接接受单个条件模板。例如:

automation:
  - alias: "打开办公室灯"
    triggers:
      - trigger: state
        entity_id: sensor.office_motion_sensor
        to: "on"
    conditions: "{{ state_attr('sun.sun', 'elevation') < 4 }}"
    actions:
      - action: scene.turn_on
        target:
          entity_id: scene.office_lights