Silverix
(Silverix)
August 25, 2019, 2:10pm
1
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?
rossko57
(Rossko57)
August 25, 2019, 2:14pm
2
That format is called JSON, it is quite common.
Hi all!!!
I have my OH2 in rasb.
So i want to send this payload —>
Timer1 {“Arm”:1,“Mode”:2,“Time”:"-2:23",“Window”:0,“Days”:“11TW11S”,“Repeat”:1,“Output”:1,“Action”:1}
via mqtt to my device(arendst/Sonoff-Tasmota Software installed)
Item:
String heater_command { mqtt=">[broker:cmnd/relay_heater/timer:command:*:default]"}
Switch progr
rule "heater time adjust Mode 2"
when
Item progr received command
then
//I want to change the payload values like Arm 1 or 0, Mode 1 or 2 etc
//but how …
1 Like
Silverix
(Silverix)
August 25, 2019, 2:27pm
3
Thank you very much. That worked perfectly
Silverix
(Silverix)
August 25, 2019, 2:49pm
4
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\"}}'
rossko57
(Rossko57)
August 25, 2019, 3:44pm
5
Dunno. Is there some reason you’re not building up your JSON string like the example you have already looked at?
Easy:
rule "heater time adjust Mode 2"
when
Item progr received command
then
//I want to change the payload values like Arm 1 or 0, Mode 1 or 2 etc
//but how can i create/compose the json string to send it? because i can't send it with this form above like a simple string
var jsonString = "{\"Arm\":" + ArmItem.state.toString
jsonString = jsonString + ",\"Mode\":" + modeItem.state.toString
jsonString = jsonString + ",\"Time\":\"-2:23\",\"Window\":0,\"Days\":\"11TW11S\",\"Repeat\":1,\"Output\":…
+
means join strings together into one string.
"first " + "second " + "third "
Strings don’t have to be literals
“before " + myItem.state.toString + " after”
Silverix
(Silverix)
August 25, 2019, 4:17pm
6
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"
when
Item VA_Intent received update
then
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)
end
rossko57
(Rossko57)
August 25, 2019, 4:36pm
7
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.
2 Likes
Silverix
(Silverix)
August 25, 2019, 4:48pm
8
YES, THAT WORKED
’{\"init\":{\"type\":\"notification\",\"text\":\"It is ' + xx + ' degree\"}}'