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.

Hi, I have a similar problem with formatting which I hope someone can help me with.

Also trying to get a Sonoff Zigbee switch to activate but getting the following error in the Zigbee Bridge: stat/sonoffzbbridge/RESULT = {“Command”:“Unknown”}. I can get the device power status, but can’t get MQTT message to turn device on or off.

I can send the control the device directly from the Tasmota console using : cmnd/sonoffzbbridge/ZbSend {“Device”:“0xE5DE”,“Send”:{“Power”:0}}

My Channel looks like this in the GUI:
State Topic: tele/sonoffzbbridge/SENSOR
MQTT command topic: cmnd/sonoffzbbridge/ZbSend
Custom On/Open value: 1
Custom Off/Closed value: 0
Incoming Value Transformations: JSONPATH:$.ZbReceived.0xE5DE.Power
Outgoing Value Transformation: “{ "Device" : "0xE5DE" , "Send" :{ "Power " : %d}}”
Outgoing Value Format: %d

Openhab logs show the following: [WARN ] [t.generic.ChannelStateTransformation] - Transformation service “{ "DEVICE" for pattern "0xE5DE" , "Send" :{ "Power " : %d}}” not found!

I’ve also tried transformation %s and tried the formatting with and without spaces.

What could I be missing???

You don’t want this: you want to use Format Before Publish.

Ok, I’ve moved the line to Outgoing Value Format (which is the FormatBeforePublish).
After double checking everything again, I was getting {“ZbSend”:“Invalid JSON”}. Long story short, I got it working and would you believe it that there is a difference between " and “
Thanks for the assistance and great work.