MQTT and {,},",: marks

MQTT is correctly configured and I don’t have any problems with connection. I have problem with sending specific commands

I have device connected over MQTT where I need to send commands which contain {, }, " and : marks. Example of command (xx is variable value):

{"init":{"type":"notification","text":"It is xx degree"}}

But when I paste it into the rule, then logs show me this:

Configuration model 'yyyyy.rules' has errors, therefore ignoring it: [31,70]: mismatched input ':' expecting '}'
[31,71]: missing ')' at '{'
[31,78]: mismatched input ':' expecting '}'
[31,93]: mismatched input ',' expecting 'end'

It looks like openhab interprets these marks like something from its function or whatever. Is there any way to bypass it?

That format is called JSON, it is quite common.

1 Like

Thank you very much. That worked perfectly :grinning:

One more problem. Command works good but “xx” variable doesn’t change into number but it is sent as “xx”. How can I fix that?

'{\"init\":{\"type\":\"notification\",\"text\":\"It is xx degree\"}}'

Dunno. Is there some reason you’re not building up your JSON string like the example you have already looked at?

+ means join strings together into one string.
"first " + "second " + "third "
Strings don’t have to be literals
“before " + myItem.state.toString + " after”

Well, this value isn’t from openhab item, but I get it from other MQTT device in this way. I tried substitute xx with device_val, but nothing changed

rule "VA"
    Item VA_Intent received update
    var String device_val = transform("JSONPATH", "$.slots[0].value.value", VA_Intent.state.toString)
    logInfo("VA device value: ", device_val)
	var xx = device_val

	val mqttActions = getActions("mqtt","mqtt:broker:Display_Broker")
	var temperature = '{\"init\":{\"type\":\"notification\",\"text\":\"It is xx degree\"}}'
	mqttActions.publishMQTT("xxx/xxx/xxx", temperature)

Okay, here you are setting the variable temperature to a string.

Here’s another example of setting a variable to a string with some other variable in the middle.

var middle = "banana"
var everything = "apples " + middle + " oranges"

I’m not going to write this for you.


{\"init\":{\"type\":\"notification\",\"text\":\"It is ' + xx + ' degree\"}}'