MQTT Statestream
MQTT Statestream 集成会将 Home Assistant 中的状态变化发布到各自独立的 MQTT 主题中。MQTT 集成 是 MQTT Statestream 正常工作的前提条件。
配置
要在 Home Assistant 中启用 MQTT Statestream,请将以下内容添加到 configuration.yaml 文件中。
:::tip
更改配置后需要重启 Home Assistant。
:::
配置过滤器
默认情况下,不会排除任何实体。要限制哪些实体暴露给 MQTT Statestream,可以使用 include 和 exclude 参数。
Filters are applied as follows:
- No filter
- All entities included
- Only includes
- Entity listed in entities include: include
- Otherwise, entity matches domain include: include
- Otherwise, entity matches glob include: include
- Otherwise: exclude
- Only excludes
- Entity listed in exclude: exclude
- Otherwise, entity matches domain exclude: exclude
- Otherwise, entity matches glob exclude: exclude
- Otherwise: include
- Domain and/or glob includes (may also have excludes)
- Entity listed in entities include: include
- Otherwise, entity listed in entities exclude: exclude
- Otherwise, entity matches glob include: include
- Otherwise, entity matches glob exclude: exclude
- Otherwise, entity matches domain include: include
- Otherwise: exclude
- Domain and/or glob excludes (no domain and/or glob includes)
- Entity listed in entities include: include
- Otherwise, entity listed in exclude: exclude
- Otherwise, entity matches glob exclude: exclude
- Otherwise, entity matches domain exclude: exclude
- Otherwise: include
- No Domain and/or glob includes or excludes
- Entity listed in entities include: include
- Otherwise: exclude
The following characters can be used in entity globs:
* - The asterisk represents zero, one, or multiple characters
? - The question mark represents zero or one character
常见过滤示例
在上面的示例中,除了 switch.x 和 sensor.nopublish 之外的所有实体都会发布到 MQTT。
在这个示例中,只有 sensor.x 和 lock.important 会被发布。
在这个示例中,除了 sensor.noshow 之外的所有传感器都会被发布。
工作原理
当任意 Home Assistant 实体发生变化时,此集成都会将该变化发布到 MQTT。
每个实体对应的主题都不同,因此您可以让其他系统只订阅自己感兴趣的实体。
主题格式为 base_topic/domain/entity/state。
例如,使用上述示例配置时,如果名为 light.master_bedroom_dimmer 的实体被打开,此集成会将 on 发布到 homeassistant/light/master_bedroom_dimmer/state。
如果该实体还有一个名为 brightness 的属性,集成还会将该属性值发布到 homeassistant/light/master_bedroom_dimmer/brightness。
所有状态和属性在发布前都会经过 JSON 序列化。请注意,这会导致字符串带上引号(例如字符串 on 会被发布为 "on")。在很多场景中,您可以使用 value_json 而不是 value 来访问 JSON 反序列化后的值(以及未加引号的字符串)。
实体的 last_updated 和 last_changed 值会分别发布到 homeassistant/light/master_bedroom_dimmer/last_updated 和 homeassistant/light/master_bedroom_dimmer/last_changed。时间戳采用 ISO 8601 格式,例如 2017-10-01T23:20:30.920969+00:00。

