Presence Detection 位置追踪与到家/离家自动化
什么是 Presence Detection
Presence Detection(位置检测)是 Home Assistant 里最常用的功能之一——Home Assistant 通过各种手段判断"谁在家、谁不在家",然后自动化可以据此触发:
- 你到家 → 自动开客厅灯 + 开空调 + 播放喜欢的音乐
- 你离家 → 自动关灯 + 锁门 + 调低恒温器 + 设防报警
- 所有人都睡了 → 自动关所有灯 + 锁所有门
方法一:Companion App GPS
最准确但最耗电的方式:
| 优点 | 缺点 |
|---|---|
| 室外精度高(~5m) | 室内 GPS 不准 |
| 跨地域都能用 | 手机耗电 |
| 免费 | 需要手机一直开 App |
优化:不要用持续 GPS 追踪。在 Companion App 设置里选 Zone-based(只在进出某个区域时上报)——这是默认也是最省电的方式。
方法二:WiFi SSID / BSSID 识别
最省电、最适合室内:
- Companion App 检测到手机连接了家里的 WiFi SSID → 判定为 home
- Companion App 检测到手机断开家里的 WiFi → 判定为 away
在 Home Assistant Settings → People → 你的 Person → Edit → 可以把 sensor.wifi_ssid 或 binary_sensor.wifi_connected 加为 Presence source。
注意:WiFi SSID 识别需要 Companion App 一直在后台跑(iOS 上后台运行受限,但 Home Assistant 的 App 优化得不错)。
方法三:路由器设备列表
路由器的 DHCP 租约表里能看到当前连接的设备 MAC 地址。Home Assistant 的 Router 集成可以读取这个列表:
- ASUS Router 集成 → 读 ASUS 路由器的设备列表
- TP-Link Kasa 集成 → 含设备在线状态
- UniFi Network 集成 → UniFi 控制器的设备列表(最专业的方案)
- OpenWRT → 通过 LuCI API 读设备列表
原理:你手机在家时会连上家里的 WiFi → 路由器列表里出现你的 MAC → Home Assistant 判定你在家。
优点:
- 不需要手机运行任何 App
- 路由器数据非常可靠
- 精度高(室内 100% 准确)
缺点:
- 需要路由器支持对应集成
- 只能知道设备在/不在,不知道具体位置
方法四:蓝牙 / iBeacon
室内高精度方案:
- 在家里布置几个 iBeacon(或蓝牙发射器)
- 手机/ESPHome 扫描到 iBeacon 信号 → 判定在哪
Home Assistant 支持通过 Bluetooth LE 集成直接做区域识别:
- 在 Home Assistant Settings → Devices & services → Bluetooth → Add integration
- 创建一个 Bluetooth Region(定义某个 BLE 信号的进入/离开区域)
- 手机/ESPHome 扫到这个信号 → Home Assistant 自动更新 Person 状态
ESPHome BLE 扫描器可以覆盖更远的距离(用 ESP32 做固定位置的 BLE 扫描器,扫描家里的蓝牙设备)。
方法五:房间级别的精细定位
如果你需要"知道你在客厅还是卧室"这种粒度:
| 方案 | 精度 | 成本 |
|---|---|---|
| 蓝牙 RSSI 三角定位 | 3~5m | 3+ 个 ESPHome 扫描器 + iBeacon |
| Zigbee 存在传感器(Aqara FP2 等) | 房间级 | ¥200~300 / 个 |
| 毫米波雷达 | 房间级 + 检测呼吸 | ~¥300 |
方法六:综合判定(推荐)
不要只用一种方案——组合起来更可靠:
Person 状态 = GPS(室外) OR WiFi SSID(室内) OR 路由器列表(兜底)
Home Assistant 的 Person 实体支持多个 Presence Source,任一 source 判定为 home 就是 home。
Person 实体配置
Settings → People → + Add person 或点已有 Person → Edit
| 字段 | 填什么 |
|---|---|
| Name | 你的名字 |
| Device tracker | 选 Companion App 的 GPS 实体(如 device_tracker.my_iphone) |
| Person picture | 头像 URL 或本地文件 |
之后可以在区域里关联 Person:Settings → Areas & Zones → Zone.home → Edit → Tracked persons → 勾选。
到家 / 离家自动化示例
到家
alias: 到家自动开启"回家模式"
trigger:
- platform: zone
entity_id: person.me
zone: zone.home
event: enter
condition:
- condition: time
before: "23:00:00"
after: "16:00:00"
action:
- service: scene.turn_on
target:
entity_id: scene.arrive_home
- service: notify.mobile_app_me
data:
title: "欢迎回家 🎉"
message: >
当前家中温度:{{ states('sensor.living_room_temp') }}°C
已开启回家模式
离家
alias: 离家自动开启"离家模式"
trigger:
- platform: zone
entity_id: person.me
zone: zone.home
event: leave
condition:
- condition: state
entity_id: person.family_member_1
state: "not_home" # 其他人也不在家
- condition: state
entity_id: person.family_member_2
state: "not_home"
action:
- service: scene.turn_on
target:
entity_id: scene.leave_home
- service: lock.lock
target:
entity_id: lock.front_door
- service: climate.set_preset_mode
target:
entity_id: climate.living_room
data:
preset_mode: away
有人在家时保持舒适
alias: 有人在家时保持温度
trigger:
- platform: state
entity_id: person.me
- platform: state
entity_id: person.family_member_1
- platform: state
entity_id: person.family_member_2
action:
- if:
condition: state
entity_id: group.all_persons
state: home
then:
- service: climate.set_temperature
target:
entity_id: climate.living_room
data:
temperature: 24
- else:
- service: climate.set_temperature
target:
entity_id: climate.living_room
data:
temperature: 18