Home Assistant 中文文档 下载 App

自动化条件与逻辑门控

条件的作用

触发器命中后,Home Assistant 先检查条件——所有(AND)条件必须同时满足,自动化才继续执行动作。如果条件不满足,本次触发直接忽略。

条件不会排队——条件不满足就跳过这次,下次再触发时重新检查。

内置条件类型

State(实体状态条件)

condition: state
entity_id: person.me
state: home

也可以检查属性:

condition: state
entity_id: climate.living_room
attribute: preset_mode
state: heat

Numeric State(数值阈值条件)

condition: numeric_state
entity_id: sensor.temperature_living_room
above: 25
below: 30

Sun(太阳位置条件)

condition: sun
before: sunset
after: sunrise
before_offset: "-00:30:00"

Time(时间段条件)

condition: time
after: "22:00:00"
before: "06:30:00"
weekday:
  - mon
  - tue
  - wed
  - thu
  - fri

Zone(区域条件)

condition: zone
entity_id: person.me
zone: zone.home

Template(模板条件)—— 最灵活

用 Jinja2 模板写任意逻辑,返回 true 或 false:

condition: template
value_template: >
  {{ state_attr('sensor.living_room_climate', 'temperature') | float > 26
     and is_state('person.me', 'home') }}

Trigger(触发器 ID 条件)

根据哪个触发器命中了来决定后续执行(2026.10 新增):

condition: trigger
id: motion_detected

多个条件的逻辑组合

条件之间的逻辑默认是 AND(所有都满足才执行):

condition:
  - condition: state
    entity_id: person.me
    state: home
  - condition: time
    after: "18:00:00"
    before: "23:00:00"
# 上面两个条件必须同时满足

OR 逻辑组

至少一个满足即可:

condition:
  - condition: or
    conditions:
      - condition: state
        entity_id: person.me
        state: home
      - condition: state
        entity_id: person.wife
        state: home
# 我或老婆在家就行

AND + OR 嵌套

可以多层嵌套,构建任意复杂度的逻辑:

condition:
  - condition: and
    conditions:
      - condition: time
        after: "18:00:00"
      - condition: or
        conditions:
          - condition: state
            entity_id: person.me
            state: home
          - condition: state
            entity_id: person.wife
            state: home
          - condition: state
            entity_id: person.kid
            state: home
# 18 点之后,家里任意一个人在家就算满足

NOT(否定)条件

condition: not
conditions:
  - condition: state
    entity_id: alarm_control_panel.home_alarm
    state: armed_away
# 报警布防在外时,此条件不满足

在动作里也可以用条件

动作内部也可以用 if / choose / while 做条件分支:

action:
  - service: light.turn_on
    target:
      entity_id: light.living_room
  - if:
      condition: state
      entity_id: sensor.motion_kitchen
      state: "on"
    then:
      - service: light.turn_on
        target:
          entity_id: light.kitchen
    else:
      - service: notify.mobile_app_me
        data:
          message: 厨房没人
  - choose:
      - conditions:
          - condition: state
            entity_id: sensor.temperature_living_room
            above: 28
        sequence:
          - service: climate.set_temperature
            target:
              entity_id: climate.living_room
            data:
              temperature: 24
      - conditions:
          - condition: state
            entity_id: sensor.temperature_living_room
            below: 18
        sequence:
          - service: climate.set_temperature
            target:
              entity_id: climate.living_room
            data:
              temperature: 22
    default:
      - service: notify.mobile_app_me
        data:
          message: 温度正常,不用调整

条件 vs. Trigger 里的 for

很多人混淆这两个:

一个例子: