MQTT 警报器
mqtt
警报器平台允许您控制启用 MQTT 的警报器和基于文本的通知设备。
配置
在理想情况下,MQTT 设备将具有一个 state_topic
来发布状态变化。如果这些消息带有 RETAIN
标志发布,MQTT 警报器将在订阅后接收即时状态更新,并将以正确的状态开始。否则,警报器的初始状态将为 false
/ off
。
当 state_topic
不可用时,警报器将在乐观模式下工作。在此模式下,警报器将在每个命令后立即更改状态。否则,警报器将等待来自设备的状态确认(来自 state_topic
的消息)。
即使 state_topic
可用,也可以强制使用乐观模式。如果您遇到不正确的操作,请尝试启用它。
要在您的安装中启用此警报器,请将以下内容添加到您的 configuration.yaml
configuration.yaml 文件是 Home Assistant 的主要配置文件。它列出了要加载的集成及其特定配置。在某些情况下,需要直接在 configuration.yaml 文件中手动编辑配置。大多数集成可以在 UI 中配置。 [Learn more] 文件中:
# 示例 configuration.yaml 条目
mqtt:
- siren:
command_topic: "home/bedroom/siren/set"
Configuration Variables
订阅的 MQTT 主题列表,以接收可用性(在线/离线)更新。不得与 availability_topic
一起使用。
当配置了 availability
时,此参数控制将实体设置为 available
所需的条件。有效值为 all
,any
和 latest
。如果设置为 all
,则必须在标记实体为在线之前在所有配置的可用性主题上接收到 payload_available
。如果设置为 any
,则必须在至少一个配置的可用性主题上接收到 payload_available
才能标记实体为在线。如果设置为 latest
,则任何配置的可用性主题上最后接收到的 payload_available
或 payload_not_available
控制可用性。
Defines a template to extract device’s availability from the availability_topic
. To determine the devices’s availability result of this template will be compared to payload_available
and payload_not_available
.
Defines a template to generate a custom payload to send to command_topic
. The variable value
will be assigned with the configured payload_on
or payload_off
setting. The siren turn on action parameters tone
, volume_level
or duration
can be used as variables in the template. When operation in optimistic mode the corresponding state attributes will be set. Turn on parameters will be filtered if a device misses the support.
Defines a template to generate a custom payload to send to command_topic
when the siren turn off action is called. By default command_template
will be used as template for action turn off. The variable value
will be assigned with the configured payload_off
setting.
发表用于更改警报器状态的命令的 MQTT 主题。没有命令模板时,会发布默认 JSON 有效负载,如 {"state":"ON", "tone": "bell", "duration": 10, "volume_level": 0.5 }
。当执行警报器开启操作时,启动参数将被添加到 JSON 有效负载中。JSON 有效负载的 state
值将设置为配置的 payload_on
或 payload_off
有效负载。
设备与外界连接的列表,作为元组 [connection_type, connection_identifier]
的列表。例如网络接口的 MAC 地址:"connections": [["mac", "02:5b:26:a8:dc:12"]]
。
实体的 类别。
Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic
. Usage example can be found in MQTT sensor documentation.
订阅的 MQTT 主题,用于接收 JSON 字典有效负载,然后设置为传感器属性。用法示例可以在 MQTT 传感器 文档中找到。
表示 off
状态的有效负载。如果指定,将用于与 state_topic
中的值进行比较(详细信息见 value_template
和 state_off
)并发送为 off
命令到 command_topic
。
表示 on
状态的有效负载。如果指定,将用于与 state_topic
中的值进行比较(详细信息见 value_template
和 state_on
),并发送为 on
命令到 command_topic
。
必须为 siren
。仅在 MQTT 自动发现设备消息 中被允许和要求。
表示 off
状态的有效负载。当 state_topic
中表示 off
状态的值与应发送到 command_topic
的值不同。
payload_off
如果定义,则为 'OFF'
表示 on
状态的有效负载。当 state_topic
中表示 on
状态的值与应发送到 command_topic
的值不同。
payload_on
如果定义,则为 'ON'
订阅的 MQTT 主题,以接收状态更新。状态更新可以是 JSON 或简单字符串。当检测到 JSON 有效负载时,JSON 有效负载的 state
值应提供 payload_on
或 payload_off
定义的有效负载,以打开或关闭警报器。此外,状态属性 duration
、tone
和 volume_level
可以更新。使用 value_template
将接收到的状态更新转换为符合的 JSON 有效负载。仅在设备支持该功能并提供有效值时,属性才会被设置。当检测到非 JSON 有效负载时,它应为定义的 payload_on
或 payload_off
中的一个有效负载,或者为 None
以将警报器的状态重置为 unknown
。初始状态将为 unknown
。如果接收到 None
有效负载或 null
JSON 值作为状态更新,状态将重置为 unknown
。
Defines a template to extract device’s state from the state_topic
. To determine the siren’s state result of this template will be compared to state_on
and state_off
. Alternatively value_template
can be used to render to a valid JSON payload.
如果 MQTT 警报器支持 duration
开启操作参数并启用 duration
状态属性,则设置为 true
。
如果 MQTT 警报器支持 volume_set
开启操作参数并启用 volume_level
状态属性,则设置为 true
。
确保您的主题完全匹配。 some-topic/
和 some-topic
是不同的主题。
示例
在本节中,您将找到使用此警报器平台的示例。
完整配置
下面的示例显示了警报器的完整配置。
# 示例 configuration.yaml 条目
mqtt:
- siren:
unique_id: custom_siren
name: "入侵警报器"
state_topic: "home/alarm/siren1"
command_topic: "home/alarm/siren1/set"
available_tones:
- ping
- siren
availability:
- topic: "home/alarm/siren1/available"
payload_on: "ON"
payload_off: "OFF"
state_on: "ON"
state_off: "OFF"
optimistic: false
qos: 0
retain: true
仅打开/关闭的控制 Tasmota 继电器的警报器
下面的示例显示了一种不接受 JSON 命令的打开/关闭类型警报器的配置。
# 示例 configuration.yaml 条目
mqtt:
- siren:
unique_id: tasmota_siren
name: "车库"
state_topic: "stat/SIREN/RESULT"
command_topic: "cmnd/SIREN/POWER"
availability_topic: "tele/SIREN/LWT"
command_template: "{{ value }}"
state_value_template: "{{ value_json.POWER }}"
payload_on: "ON"
payload_off: "OFF"
payload_available: "在线"
payload_not_available: "离线"
为了检查,您可以使用与 mosquitto
一起提供的命令行工具 mosquitto_pub
发送 MQTT 消息。这允许您手动操作您的警报器:
mosquitto_pub -h 127.0.0.1 -t home/alarm/siren1 -m "ON"