MQTT send JSON command to Plug form Switch

Hello,
I have OH2.4 running with MQTT and a Mosquito Broker (separate instance, not on OH instance).
Today I tried to add INNR Plugs to my setup.

I have included the plugs to my MQTT network and using MQTT.fx I can switch them. As the plugs needs to get the command as JSON string, I tried different ways to set this up but all failed.

Important to mention is that I do not use a Things file, instead I have my configuration running using Paper UI.

I have created a new Switch item and set the Command Topic to “zigbee2mqtt/WZ/PLUG/CAMERA/set”.
Then i created a JS file with the following content and add this to Outgoing value format JS:onoff.js.

(function(x) { var result = new Object();
if (x==ON) {result.state = “state:ON”}
else if (x==OFF) {result.state = “state.OFF”}
return JSON.stringify(result);
})(input).

That didn’t work, so I tried to add the information directly in Paper UI:

ON={“state”:“ON”}, OFF={“state”:“OFF”}.

Finally I tried using a rule to run the command:

val mqttAction = getActions(“mqtt”,“mqtt:systemBroker:MQTTBroker”)
mqttAction.publishMQTT(“zigbee2mqtt/WZ/PLUG/CAMERA/set”,“{"state":"ON"}”)

This returns an error:

Rule ‘testbutton’: Instance is not an MQTTActions class.

So I have no further idea how to proceed. Different aspects seems just to work using things files instead of working through the UI.

I’m happy of any kind of support.
Regards Martin

It’d be helpful to see what you send, what you’re trying to acheive.

You’ve got mqtt.fx to monitor what happened here. May we know what happened too?

You would normally get actions associated with a broker, not with a systembroker. You don’t need any systembroker Thing unless you are using the embedded broker.

In addition to @rossko57 requests:

I have something similar, and use formatBeforePublish. I use files, but I presume there is a similarly named option with the UI:

formatBeforePublish="{\"state\":\"%s\"}"

The %s gets filled in with either on or off depending on the switch position.

in MQTT.fx I use the following command to switch:
For on {“state”:“ON”}, for off {“state”:“off”}.

Regarding the script, you mean

val mqttAction = getActions(“mqtt”,“mqtt:broker:MQTTBroker”)

instead of

val mqttAction = getActions(“mqtt”,“mqtt:systemBroker:MQTTBroker”)

Should work?
Thanks Martin

I couldn’t say; I don’t know what you chose to name your broker Thing.

It looks like you are having fun.

Try this rule

rule "Test Item"
when
  Item TEST222a received command
then
   
  val value = '{\"state\":\"' + receivedCommand + '\"}'
  val actions = getActions("mqtt","mqtt:broker:myMQTTBroker") // myMQTTBroker is the name of my broker yours may be different
  actions.publishMQTT("zigbee2mqtt/WZ/PLUG/CAMERA/set",value)    

end

The name of your broker is found in paper ui

Screenshot 2020-09-21 155624

2 Likes

Thanks!!!
This was exactly the issue, using your hint it works perfectly!

1 Like

Note, given that the command isn’t being transformed, you probably don’t need the Rule. Use the “Outgoing Value Format” in the Thing definition.

{ "state": "%s" }

That’s what it’s there for.

1 Like

This wasn’t from a thing this is from an item direct to MQTT with a rule. Yes it is easier to do all this with a thing