手机推送通知完整配置
Home Assistant 推送通知机制
当 Companion App 连接成功后,Home Assistant 自动创建一个推送服务:notify.mobile_app_<你的设备ID>。调用这个服务就能往手机上发通知。
iOS 和 Android 的推送机制不同:
- iOS:Home Assistant 通过 APNs(Apple Push Notification Service)推送
- Android:Home Assistant 通过 FCM(Firebase Cloud Messaging)推送
两者在 Home Assistant 里的调用方式完全一致——不需要区分平台。
最基础的通知
action:
- service: notify.mobile_app_me
data:
title: "注意!"
message: "车库门开了超过 10 分钟"
富媒体通知
iPhone 通知上的图片
action:
- service: notify.mobile_app_me
data:
title: "前门摄像头"
message: "有人在门口"
data:
attachment:
url: "http://homeassistant.local:8123/api/camera_proxy/camera.doorbell"
通知上的快捷按钮(iOS + Android)
action:
- service: notify.mobile_app_me
data:
title: "前门有人"
message: "门铃响了"
data:
actions:
- action: "OPEN_DOOR"
title: "开门"
icon: "mdi:door-open"
- action: "VIEW_CAMERA"
title: "查看摄像头"
icon: "mdi:video"
- action: "DECLINE"
title: "拒绝"
icon: "mdi:close"
用户点按钮后会触发 Home Assistant 的 mobile_app_notification_action 事件,自动化可以监听:
alias: 处理通知按钮点击
trigger:
- platform: event
event_type: mobile_app_notification_action
event_data:
action: "OPEN_DOOR"
action:
- service: lock.unlock
target:
entity_id: lock.front_door
通知跳转 App 内页面
action:
- service: notify.mobile_app_me
data:
title: "车库门"
message: "车库门开了"
data:
clickAction: "/lovelace/garage" # 跳转到车库仪表盘
push:
category: "garage_door"
Android 常驻通知
Android 可以显示常驻通知(不会自动消失):
action:
- service: notify.mobile_app_me
data:
title: "太阳能状态"
message: >
发电:{{ states('sensor.solar_production') }}W
今日:{{ states('sensor.solar_daily') }}kWh
data:
sticky: true
persistent: true
通知分组和收敛
分组相关通知
action:
- service: notify.mobile_app_me
data:
title: "晚上好"
message: "所有设备检查完毕"
data:
group: "evening_check" # 同一个 group 的通知在通知中心会合并
通知等级
action:
- service: notify.mobile_app_me
data:
title: "紧急!"
message: "检测到漏水!"
data:
ttl: 0 # 不自动过期
priority: high # 高优先级(iPhone 上会突破专注模式)
importance: high # Android 上的高优先级
Android 消息分组 + 摘要
action:
- service: notify.mobile_app_me
data:
title: "今日提醒"
message: "别忘了关灯"
data:
group: "daily"
tag: "reminder_1" # 同一个 tag 会覆盖之前的通知(而不是新增)
summary: "今日提醒:2 条" # 分组通知的摘要
发送到所有人
如果有多台设备(你和家人各一个手机),可以用 notify.all_devices(Home Assistant 会自动创建这个服务):
action:
- service: notify.all_devices
data:
title: "全家通知"
message: "今晚 7 点吃饭"
调试通知
快速测试
Developer Tools → Services → 找 notify.mobile_app_xxx → 填 title + message → Call Service
手机应该立刻收到。收不到就排查:
- Companion App 是否在运行
- Home Assistant → Settings → Devices & services → Mobile App 集成是否正常
- Companion App 是否有通知权限(iOS:设置 → Home Assistant → 通知;Android:设置 → 应用 → Home Assistant → 通知)
查看推送状态
Home Assistant 日志里会显示推送是否成功。Developer Tools → Logs → 搜索 Mobile App。
测试后清理
测试完通知后,Home Assistant 的事件日志里会留下痕迹。不影响系统,不用特意清理。
通知节流
不要在高频触发的自动化里直接发通知——比如 state_changed 触发器每次触发都发通知,会把你手机炸掉。用以下方法节流:
方法一:for 条件
trigger:
- platform: numeric_state
entity_id: sensor.temperature
above: 30
for:
minutes: 10 # 持续 10 分钟才发一次,避免频繁触发
方法二:input_boolean + 冷却
alias: 高温提醒(带冷却)
trigger:
- platform: numeric_state
entity_id: sensor.temperature
above: 30
condition:
- condition: state
entity_id: input_boolean.temp_notification_cooldown
state: "off"
action:
- service: notify.mobile_app_me
data:
title: "高温"
message: "温度超过 30°C"
- service: input_boolean.turn_on
target:
entity_id: input_boolean.temp_notification_cooldown
- delay:
hours: 1
- service: input_boolean.turn_off
target:
entity_id: input_boolean.temp_notification_cooldown