[教學] 房間進出狀態自動判斷與自動化控制

:clipboard: 概述

本方案透過多重感測器協同運作,實現房間進出狀態的自動判斷,並觸發相應的自動化控制

適用場景

  • 人員配置:單人使用環境
  • 空間配置:單一出入口房間

注意事項

本方案為參考設計,實際部署時請根據以下因素進行調整:

  • 感測器類型與感測器感測範圍
  • 特定的自動化需求
  • 現有智慧家庭系統的整合需求

:dart: 狀態定義

狀態圖


Online Diagram

狀態說明

狀態 描述 觸發條件
leaved 已離開房間 初始狀態或確認離開
entering 正在進入 門開啟(從leaved)
checking 檢查狀態 門關閉後等待確認
waiting 等待確認 檢查中門再次開啟
entered 已進入房間 檢測到室內活動
leaving 正在離開 門開啟(從entered)

:arrows_counterclockwise: 狀態轉換規則

主要轉換

  1. door_opened: leavedentering
  2. door_closed: enteringchecking
  3. motion_detected: checkingentered
  4. motion_cleared: checkingleaved (1分鐘無活動)
  5. checking_timeout: checkingleaved (2分鐘超時)
  6. door_opened: enteredleaving
  7. door_closed: leavingchecking
  8. door_opened: checkingwaiting (重新開門)
  9. door_closed: waitingchecking (回到檢查)

特殊轉換

  1. motion_detected: leavedentered (直接檢測)

:wrench: 感應器配置

必需感應器

實體ID 類型 用途 實際名稱
binary_sensor.entrance_door_contact 門窗感應器 檢測門開關狀態 Door Contact Contact
binary_sensor.all_room_motions 人體感應器群組 室內活動檢測(包含多個感應器) All Room Motions

感應器群組包含

  • binary_sensor.0x54ef44100138510c_occupancy - 床邊人體感應器 Occupancy
  • binary_sensor.0x54ef441000acabff_occupancy - 浴室人體感應器 Occupancy
  • binary_sensor.motion_sensor_7b7b_motion - 大門人體感應器 Motion

輸出實體

實體ID 類型 用途
input_select.room_entrance_state 選擇器 當前狀態(主要狀態實體)

主要自動化

自動化ID 別名 用途
entrance_motion_sensor_state_machine_control [臥室] 房間進出 - 有限狀態機 主狀態機控制邏輯

binary_sensor.yaml

binary_sensor:
- platform: group
  name: "All Room Motions"
  unique_id: all_room_motion
  device_class: motion
  entities:
        - binary_sensor.0x54ef44100138510c_occupancy
        - binary_sensor.0x54ef441000acabff_occupancy
        - binary_sensor.motion_sensor_7b7b_motion

input_select.yaml

input_select:
  room_entrance_state:
    name: 房間進出狀態
    options:
      - leaved      # 離開狀態
      - entering    # 進入中
      - checking    # 檢查中
      - waiting     # 等待中
      - entered     # 已進入
      - leaving     # 離開中
    icon: mdi:door

automations.yaml

automation:
- id: entrance_motion_sensor_state_machine_control
  alias: '[臥室] 房間進出 - 有限狀態機'
  description: 統一處理進出狀態的所有轉換邏輯
  triggers:
  - trigger: state
    entity_id: binary_sensor.entrance_door_contact
    to: 'on'
    id: door_opened
  - trigger: state
    entity_id: binary_sensor.entrance_door_contact
    to: 'off'
    id: door_closed
  - trigger: state
    entity_id:
    - binary_sensor.0x54ef44100138510c_occupancy
    - binary_sensor.0x54ef441000acabff_occupancy
    - binary_sensor.motion_sensor_7b7b_motion
    to: 'on'
    id: motion_detected
  - trigger: state # 感應器群組化等待所有感應器全是 off 狀態再觸發
    entity_id:
    - binary_sensor.all_room_motions
    to: 'off'
    for:
      minutes: 1
    id: motion_cleared
  - trigger: state
    entity_id: input_select.room_entrance_state
    to: checking
    for:
      minutes: 2
    id: checking_timeout
  conditions: []
  actions:
  - choose:
    - alias: Leaved + Door Open -> Entering
      conditions:
      - condition: trigger
        id: door_opened
      - condition: state
        entity_id: input_select.room_entrance_state
        state: leaved
      sequence:
      - action: input_select.select_option
        target:
          entity_id: input_select.room_entrance_state
        data:
          option: entering
    - alias: Entering + Door Close -> Checking
      conditions:
      - condition: trigger
        id: door_closed
      - condition: state
        entity_id: input_select.room_entrance_state
        state: entering
      sequence:
      - action: input_select.select_option
        target:
          entity_id: input_select.room_entrance_state
        data:
          option: checking
    - alias: Checking + Motion Detected -> Entered
      conditions:
      - condition: trigger
        id: motion_detected
      - condition: state
        entity_id: input_select.room_entrance_state
        state: checking
      sequence:
      - action: input_select.select_option
        target:
          entity_id: input_select.room_entrance_state
        data:
          option: entered
    - alias: Entered + Door Open -> Leaving
      conditions:
      - condition: trigger
        id: door_opened
      - condition: state
        entity_id: input_select.room_entrance_state
        state: entered
      sequence:
      - action: input_select.select_option
        target:
          entity_id: input_select.room_entrance_state
        data:
          option: leaving
    - alias: Leaving + Door Close -> Checking
      conditions:
      - condition: trigger
        id: door_closed
      - condition: state
        entity_id: input_select.room_entrance_state
        state: leaving
      sequence:
      - action: input_select.select_option
        target:
          entity_id: input_select.room_entrance_state
        data:
          option: checking
    - alias: Checking + Motion Cleared -> Leaved
      conditions:
      - condition: trigger
        id: motion_cleared
      - condition: state
        entity_id: input_select.room_entrance_state
        state: checking
      sequence:
      - action: input_select.select_option
        target:
          entity_id: input_select.room_entrance_state
        data:
          option: leaved
    - alias: Checking + Timeout -> Leaved
      conditions:
      - condition: trigger
        id: checking_timeout
      - condition: state
        entity_id: input_select.room_entrance_state
        state: checking
      sequence:
      - action: input_select.select_option
        target:
          entity_id: input_select.room_entrance_state
        data:
          option: leaved
    - alias: Leaved + Motion Detected -> Entered (Direct Entry)
      conditions:
      - condition: trigger
        id: motion_detected
      - condition: state
        entity_id: input_select.room_entrance_state
        state: leaved
      sequence:
      - action: input_select.select_option
        target:
          entity_id: input_select.room_entrance_state
        data:
          option: entered
    - alias: Checking + Door Reopen -> Waiting
      conditions:
      - condition: trigger
        id: door_opened
      - condition: state
        entity_id: input_select.room_entrance_state
        state: checking
      sequence:
      - action: input_select.select_option
        target:
          entity_id: input_select.room_entrance_state
        data:
          option: waiting
    - alias: Waiting + Door Close -> Checking
      conditions:
      - condition: trigger
        id: door_closed
      - condition: state
        entity_id: input_select.room_entrance_state
        state: waiting
      sequence:
      - action: input_select.select_option
        target:
          entity_id: input_select.room_entrance_state
        data:
          option: checking
  trace:
    stored_traces: 500
  mode: single

Note:

  1. 三組配置檔測試完畢後,可使用 package 來模組化管理,但在 Automation 編輯器不能編輯

:gear: 實施步驟

1. 部署配置

# 1. 狀態機自動化已整合到 automations.yaml
# 2. 確保所有必需的實體已創建
# 3. 重啟 Home Assistant 或重載配置

2. 驗證感應器

確保所有感應器實體存在且正常工作:

  • 檢查感應器狀態
  • 測試門開關檢測
  • 驗證人體感應器響應
  • 確認 binary_sensor.all_room_motions 群組已正確配置

3. 測試狀態轉換

按順序測試各種場景:

  1. 進入場景:門開啟 → 門關閉 → 檢測到室內活動
  2. 離開場景:門開啟 → 門關閉 → 等待活動清除(1分鐘)
  3. 超時場景:門開啟 → 門關閉 → 2分鐘後自動轉為已離開
  4. 直接檢測:在離開狀態下直接檢測到活動
  5. 重新開門場景:檢查狀態中門再次開啟

:mag: 調試和故障排除

調試工具

  1. Dashboard
  1. Card
  • Tile card
  • History graph card
  • Logbook card
  1. Automation Trace Log


:bar_chart: 狀態相關自動化

狀態動作自動化

自動化ID 觸發狀態 動作說明
entrance_state_machine_entering_action entering 開啟入口燈光,營造歡迎氛圍
entrance_state_machine_entered_action entered 根據需求控制設備
entrance_state_machine_leaving_action leaving 停止媒體播放、播報天氣預報
entrance_state_machine_leaved_action leaved 關閉所有燈光、冷氣、風扇等設備、電腦休眠、開始掃地機清掃

Caution:

  1. 多項設備控制,可以搭配 parallel 使用
  2. 避免使用長時間阻塞的 action (如 delay, wait_for_trigger 等),如有需要可移出到 script 或其它 automation 來非同步執行

automations.yaml

- id: entrance_state_machine_entering_action
  alias: '[臥室] 房間進出 - 進入中動作'
  description: ''
  triggers:
  - entity_id:
    - input_select.room_entrance_state
    to: entering
    trigger: state
  conditions: []
  actions:
  - parallel:
    - action: light.turn_on
      target:
        entity_id: light.da_deng
      data: {}
  - delay:
      seconds: 5 # 延遲開燈比較有互動氛圍
  mode: single

- id: entrance_state_machine_leaved_action
  alias: '[臥室] 房間進出 - 已離開動作'
  description: ''
  triggers:
  - entity_id:
    - input_select.room_entrance_state
    to: leaved
    trigger: state
  conditions:
  actions:
  - parallel:
    - action: light.turn_off
      target:
        entity_id: light.all_lights
      data: {}
    - action: fan.turn_off
      target:
        entity_id: fan.dmaker_p5_61b7_fan
      data: {}
  mode: single