[SOLVED] Send json with mqtt command

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 can i create/compose the json string to send it? because i can't send it with this form above like a simple string

sendCommand(heater_command,jsonstring)

end

i thing with .map or transformation but i dont know how to write the code
thnx in advance

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\":1,\"Action\":1}"

heater_command.sendCommand(jsonString)

end

I think that I am gonna have problem with the " . I’ll try it

If you notice, if you want to put a " inside a string you need to escape it using \
So a String containing one " would be declared as : var String myString = "\""

1 Like

side question @vzorglub: is it case sensitive or not?

1 Like

Typos… Typos…

1 Like

ok did it but…

rule "heater time adjust Mode 2"
when
Item progr received command
then
var jsonstring= '{Timer1:{\"Arm\":\"0\",\"Mode\":\"0\",\"Time\":\"13:34\",\"Window\":\"0\",\"Days\":\"1111111\",\"Repeat\":\"0\",\"Output\":\"1\",\"Action\":\"1\"}}'
logInfo("heater",jsonstring.toString)
heater_command.sendCommand(jsonstring)
end

mqtt spy result on topic -->

{Timer1:{“Arm”:“0”,“Mode”:“0”,“Time”:“13:34”,“Window”:“0”,“Days”:“1111111”,“Repeat”:“0”,“Output”:“1”,“Action”:“1”}}

log file openhab result —>

2018-11-20 01:11:54.498 [INFO ] [clipse.smarthome.model.script.heater] - {Timer1:{“Arm”:“0”,“Mode”:“0”,“Time”:“13:34”,“Window”:“0”,“Days”:“1111111”,“Repeat”:“0”,“Output”:“1”,“Action”:“1”}}

on device my arsend software the result on consol is -->

01:19:54 MQT: stat/relay_heater/RESULT = {“Timer1”:{“Arm”:1,“Mode”:0,“Time”:“02:23”,“Window”:0,“Days”:“0011001”,“Repeat”:1,“Output”:1,“Action”:1}}

What goes wrong? whatever I put to my string ,timer1 or timer2,arm 0 or 1 i take the same result for timer1

Don’t put quote for numbers

1 Like

with this–>

var jsonstring=’{“Timer1”:{“Arm”:0,“Mode”:0,“Time”:13":"34,“Window”:0,“Days”:1111111,“Repeat”:0,“Output”:1,“Action”:1}}’

in console i have this result->> {“Timer1”:“Invalid JSON”}

this bit: “Time”:13":"34
Need to be "Time":"13:34", it’s a string
You’re close

1 Like

rule “heater time adjust Mode 2”
when
Item progr received command
then
var String jsonstring= ‘{“Timer2”:{“Arm”:0,“Mode”:0,“Time”:“13:34”,“Window”:0,“Days”:1111111,“Repeat”:0,“Output”:1,“Action”:1}}’
logInfo(“heater”,jsonstring.toString)
heater_command.sendCommand(jsonstring.toString)
end

the same result in console

23:05:55 MQT: stat/relay_heater/RESULT = {“Timer1”:{“Arm”:1,“Mode”:0,“Time”:“02:23”,“Window”:0,“Days”:“0011001”,“Repeat”:1,“Output”:1,“Action”:1}}

instructions from here:

the solution was very simple
i had wrong topic …/timer instead of …/time1
so
var String jsonstring= "{\"Arm\":0,\"Time\":\"13:35\",\"Window\":0,\"Days\":\"11TW11S\",\"Repeat\":0,\"Output\":1,\"Action\":1}"
it works now.