在手机上接收 Home Assistant 通知
当门被打开、传感器检测到漏水或温度下降时,Home Assistant 可以通过 NotIf 通知你。你可以在 Android 应用、浏览器或终端中收到消息,不需要 Nabu Casa 账户,也不需要配套应用。
创建命令
在 configuration.yaml 中添加一个 REST 命令,然后重启 Home Assistant。把 my-home 换成一个难以猜到的频道名称。
rest_command:
notif:
url: "https://usenotif.com"
method: POST
content_type: "application/json"
payload: >
{{ {
"topic": "my-home",
"title": title | default("Home Assistant"),
"message": message,
"priority": priority | default(3),
"tags": tags | default([])
} | to_json }}
to_json 过滤器能保护消息中的中文、特殊字符和引号。
从自动化发送消息
调用 rest_command.notif 动作,并传入标题和消息。
automation:
- alias: "车库门开着"
triggers:
- trigger: state
entity_id: cover.garage_door
to: "open"
for: "00:10:00"
actions:
- action: rest_command.notif
data:
title: "车库"
message: "车库门已经开了 10 分钟"
priority: 4
tags: ["warning"]
在可视化编辑器中,选择 Add action → RESTful Command: notif,然后切换到 YAML 模式填写 data。界面上的名称可能因版本和界面语言而不同。
优先级从 1 到 5。漏水或警报请使用 5:Android 应用会长时间振动。参见优先级。
包含传感器的状态
Home Assistant 的模板可以在 message 中使用。
- action: rest_command.notif
data:
title: "冰柜"
message: "温度:{{ states('sensor.freezer_temperature') }} °C"
priority: 5
tags: ["thermometer"]
预留频道
任何知道公开频道名称的人都能读取它。要让频道只有你能读取,请预留它并创建一个令牌。把令牌保存在 secrets.yaml 中:
notif_auth: "Bearer tk_votrejeton"
然后在命令中加上请求头:
rest_command:
notif:
url: "https://usenotif.com"
method: POST
content_type: "application/json"
headers:
Authorization: !secret notif_auth
payload: >
{{ {"topic": "my-home", "title": title | default("Home Assistant"), "message": message} | to_json }}
在通知中操作
按钮可以调用 Home Assistant 的 webhook,例如关闭车库门。请求从手机发出:手机必须能访问你的 Home Assistant。默认情况下,Home Assistant 只接受来自本地网络的这类 webhook。
- action: rest_command.notif
data:
title: "车库"
message: "车库门开着"
actions:
- action: http
label: "关闭"
url: "https://ha.exemple.com/api/webhook/fermer-garage-8f3k2"
clear: true
在命令的 payload 中加上 "actions": actions | default([]),才能传递按钮。参见操作按钮。