Switch on by publishing a command and a json string

  • Platform information:
    • Hardware: CPUArchitecture/RAM/storage i5/16GB/256GB
    • OS: _what OS is used and which version_Ubuntu 20.04
    • Java Runtime Environment: _which java platform is used and what version_openJDK 11
    • openHAB version:3.0.0
  • Issue of the topic: Ligth switch that needs to publish a json string at on or off

So I have a Zigbee Blizwolf plugg, a Tasmota 9.2 Coordinator and what to control this by MQTT.

On the “on” action I need to publish a command and a jason string.
so basacly to publish “{“device”:“Plug1”,“send”:{“Power”:“On”}}” to /cmnd/ZBBridge/ZbSend

This did not work:
Thing:
Channells:
Type switch : Plug1 [stateTopic=“stat/ZBBridge/RESULT”, commandTopic=“cmnd/ZBBridge/ZbSend”, on="{“device”:“Plug1”,“send”:{“Power”:“On”}}", off="…"]

Item:
Switch Plug1 “Plug1” {channel=“mqtt:topic:tele:ZBBridge:Plug1”}

Should I define switch as a string??

Thanks

Firstly, you have a typo:

I’m on phone, so can’t provide a complete solution, but for this I would probably use the formatBeforePublish option for your Channel

That would make your Channel something like:

stateTopic="stat/ZBBridge/RESULT",
commandTopic="cmnd/ZBBridge/ZbSend",
formatBeforePublish="{\"device\":\"Plug1\",\"send\":{\"Power\":%s}}"
on="On",
off="Off"

Thank you, this worked!

Thing and channel:
Thing mqtt:topic:tele:ZBBridge “ZBBridge” (mqtt:broker:MosquittoMqttBroker) {
Channels:
// Sonoff Zb Bridge
Type switch : Plug1 [
stateTopic=“stat/ZBBridge/RESULT”,
commandTopic=“cmnd/ZBBridge/ZbSend”,
formatBeforePublish="{“device”:“Plug1”,“send”:{“Power”:"%s"}}",
on=“On”,
off=“Off”]

That’s great, well done!

Merry Christmas!

The formatBeforePublish string needs to be escaped. Even the %. The escape character disappeared in my last post.

How do I mark this as solved?

Just click the tick box at the bottom left of the post which solved your issue.