本帖最后由 ganguotu 于 2024-9-4 08:11 编辑
我测试了一下,可以直接用‘return x == "on";’判断,但是,在某些 mqtt 服务器中,同一个话题表示命令和状态,会导致进入循环,尽量别那样用。
可以试试这样:
mqtt:
broker: !secret mqtt_broker
port: !secret mqtt_port
username: !secret mqtt_username
password: !secret mqtt_password
topic_prefix: null
discovery: false
reboot_timeout: 0s
on_message:
- topic: led006
then:
- if:
condition:
lambda: 'return x == "on";'
then:
- light.turn_on: activity_led
- if:
condition:
lambda: 'return x == "off";'
then:
- light.turn_off: activity_led
再用不同的话题 ledstate 表示设备状态以免进入循环:
light:
- platform: esp32_rmt_led_strip
id: activity_led
name: "${friendly_name} LED"
rgb_order: GRB
pin: GPIO48
num_leds: 1
rmt_channel: 0
chipset: ws2812
on_turn_on:
- mqtt.publish:
topic: ledstate
payload: "on"
on_turn_off:
- mqtt.publish:
topic: ledstate
payload: "off"
踩坑点:在巴法云中,mqtt.on_message: 不可以重复订阅同一个topic,一直报错,客户端重复断连!只发现巴法云这样的,其他的 mqtt 服务器是可以的!
|