← 全部集成

从 Home Assistant 发送短信

警报响起、冰柜升温或夜里大门被打开时收到短信,即使接收短信的手机没有网络也能收到。Home Assistant 通过 rest_command 调用 NS SMS API,你的 Android 手机用自己的 SIM 卡发出短信。

适合谁

  • 想给家人发送短信提醒、又不想安装配套应用的 Home Assistant 用户。
  • 推送通知不够用的度假屋、车库和工作间。
  • 喜欢把配置写在 YAML 里、不想安装自定义集成的人。

开始之前

  • 一部 Android 手机(8.0 或更高版本),装有可发送短信的 SIM 卡。
  • 从 Google Play 安装 NS SMS 应用,设为默认短信应用,并在 API 标签页开启“接受 API 请求”。
  • API 标签页中的手机 ID(device_id)和 API 密钥(nss_...)。下文以 YOUR_DEVICE_ID 和 nss_YOUR_API_KEY 表示。
  • 能访问 Home Assistant 的配置目录(File editor 或 Studio Code Server 加载项、Samba 或 SSH)。

无需账号、邮箱或密码。安装应用,开启 API,复制手机 ID 和 API 密钥即可。

分步设置

  1. 第 1 步.把 API 密钥存为 secret

    打开 secrets.yaml(与 configuration.yaml 在同一目录),添加 Authorization 请求头的值:单词 Bearer、一个空格和你的密钥。

    secrets.yaml
    ns_sms_auth: "Bearer nss_YOUR_API_KEY"
  2. 第 2 步.添加 rest_command

    把以下内容加入 configuration.yaml,并替换 YOUR_DEVICE_ID。to_json 过滤器会把号码和消息转成合法的 JSON 字符串,消息中的引号和换行不会破坏请求。

    configuration.yaml
    rest_command:
      send_sms:
        url: "https://sms-api.n-soft.pl/v1/sms/send"
        method: post
        headers:
          Authorization: !secret ns_sms_auth
        content_type: "application/json"
        payload: >-
          {"device_id": "YOUR_DEVICE_ID",
          "to": {{ to | to_json }},
          "text": {{ message | to_json }}}

    如果 configuration.yaml 里已经有 rest_command: 段,请把 send_sms 放在它下面,不要再添加第二个 rest_command: 键。

  3. 第 3 步.重启并发送测试短信

    检查配置(Developer tools > YAML),重启 Home Assistant,然后在 Developer tools > Actions 的 YAML 模式下运行该动作。手机会在几秒内发出短信。

    Developer tools > Actions (YAML mode)
    action: rest_command.send_sms
    data:
      to: "+14155550123"
      message: "Hello from Home Assistant"
  4. 第 4 步.在自动化中使用

    新建一个自动化,打开右上角三点菜单,选择 Edit in YAML 并粘贴示例。把 binary_sensor.front_door 换成你自己的实体。消息中可以使用 now() 等模板。

    Automation editor > Edit in YAML
    alias: SMS when the front door opens at night
    triggers:
      - trigger: state
        entity_id: binary_sensor.front_door
        to: "on"
    conditions:
      - condition: time
        after: "23:00:00"
        before: "06:00:00"
    actions:
      - action: rest_command.send_sms
        data:
          to: "+14155550123"
          message: "Front door opened at {{ now().strftime('%H:%M') }}"
    mode: single
  5. 第 5 步.可选:短信 notify 服务

    有些 blueprint 和脚本需要 notify 动作。RESTful notify 平台可以发送同样的请求:把消息放进 text,把目标放进 to,并加上 device_id。添加后再次重启,然后调用 notify.sms。

    configuration.yaml
    notify:
      - name: sms
        platform: rest
        resource: "https://sms-api.n-soft.pl/v1/sms/send"
        method: POST_JSON
        headers:
          Authorization: !secret ns_sms_auth
        message_param_name: text
        target_param_name: to
        data:
          device_id: "YOUR_DEVICE_ID"
    automation action
    action: notify.sms
    data:
      message: "The washing machine has finished."
      target: "+14155550123"

查看状态并接收 Webhook

发送请求会立即返回 202 和消息 ID。之后状态依次变为 received(手机已收到)、sent 和 delivered,或带错误码的 failed。你可以用 GET /v1/sms/{id} 查询,也可以让服务器把每次变化 POST 到你在应用(API 标签页)中设置的 Webhook 地址。所有套餐都支持 Webhook,还会推送 usage.warning、usage.limit_reached、device.offline 和 device.online 事件。

要在自动化中查看结果,请在 rest_command 动作中加上 response_variable: sms。sms.status 是 HTTP 状态码(已接受时为 202),sms.content.id 是消息 ID。

要响应 Webhook 事件,请创建一个使用 webhook 触发器的自动化,并在 NS SMS 应用中把 Webhook 地址设为 https://YOUR-HOME-ASSISTANT/api/webhook/<webhook_id>。Home Assistant 必须能通过 HTTPS 从互联网访问(例如通过 Home Assistant Cloud 或你自己的反向代理)。下面的示例会在短信手机停止上报时提醒你:

Automation editor > Edit in YAML
alias: NS SMS phone went offline
triggers:
  - trigger: webhook
    webhook_id: ns-sms-REPLACE-WITH-A-LONG-RANDOM-STRING
    allowed_methods:
      - POST
    local_only: false
conditions:
  - condition: template
    value_template: "{{ trigger.json.event == 'device.offline' }}"
actions:
  - action: persistent_notification.create
    data:
      title: NS SMS
      message: "The SMS phone stopped reporting at {{ trigger.json.sent_at }}."
mode: queued

webhook 触发器无法校验 X-NS-Signature 请求头,所以请把 webhook ID 当作密码:设得足够长且随机,不要出现在截图中。

常见问题

  • 找不到动作 rest_command.send_sms:YAML 没有加载。检查配置,查看 Settings > System > Logs 并重启。
  • 2024 年底之前的版本使用 service: 而不是 action:,使用 trigger:/platform: 而不是 triggers:/trigger:。在可视化编辑器中创建自动化,会自动写出适合你版本的语法。
  • 每次传感器变化都触发的自动化很快就会用完每日额度。请加上条件、for: 持续时间或 mode: single。
  • 401 unauthorized:Authorization 请求头必须是 "Bearer " 加上密钥,且 device_id 必须属于该密钥。
  • 403 api_disabled:打开应用,在 API 标签页开启“接受 API 请求”。
  • 422 validation_failed:号码必须是国际 E.164 格式,带 + 和国家代码(例如 +14155550123),文本最多 1,600 个字符。
  • 429:每分钟超过 30 个请求,或超出套餐的每日或每月上限(Free 每天 50 条,Pro 500 条,Business 1,000 条)。响应会说明是哪一项。

短信API参考 → 短信错误码 → 套餐与每日额度 →

Home Assistant 的使用场景

其他集成