跳转到内容

MQTT 订阅文本传感器

mqtt_subscribe 文本传感器平台允许您将外部数据导入 ESPHome。 该传感器将订阅给定 MQTT 主题上的消息,并将最新值保存在其 id(mysensor).state 中。

# 示例配置条目
text_sensor:
- platform: mqtt_subscribe
name: "Data from topic"
id: mysensor
topic: the/topic
  • topic (必需, string): 要监听字符串数据的 MQTT 主题。
  • qos (可选, int): 订阅时使用的 MQTT QoS。默认为 0
  • 所有其他选项来自文本传感器

此组件对于显示器特别有用,可以在显示器上显示外部数据。 请注意,您必须在 .state 对象上使用 .c_str() 方法配合 %s 格式, 才能在 printf 表达式中使用它。

# 示例配置条目
text_sensor:
- platform: mqtt_subscribe
name: "Data from topic"
id: mysensor
topic: the/topic
display:
- platform: ...
# ...
lambda: |-
it.printf(0, 0, id(font), "The data is: %s", id(mysensor).state.c_str());