Publish MQTT message with JSON payload

Tags: #<Tag:0x00007f2fb6386908>

Dear all,

I would like to publish an MQTT message with JSON payload:

Switch button "Update" {mqtt=">[rabbit:owntracks/peter/phone/cmd:command:ON:\"{abcd\:2}\"]"}

The problem is that I do not know how to escape the colon sign.

Background:
OwnTracks allows to remotely trigger the current location by sending MQTT message with the payload:

{"_type":"cmd","action":"reportLocation"}

This MQTT message I want to publish after button click in OpenHAB.

Thank you for your help
Peter

I found an workaround through a rule:

Items:

Switch button
String locationRequest "[%s]" {mqtt=">[rabbit:owntracks/peter/phone/cmd:command:*:default]"}

Sitemaps:

Switch item=button label="Update" mappings=[ON="Now"]

Rules:

rule "mqtt publish"
when
    Item button received command
then
    locationRequest.sendCommand("{\"_type\":\"cmd\",\"action\":\"reportLocation\"}")
end

Hope it helps

Peter

I have similar problem.
Did you find solution?
Is this Button Item correct?

Switch item=button label="Update" mappings=[ON="Now"]

Edit: It’s correct. 100% working for me. Thanks

Hey there are a lot of questions about how to send a JSON paload via MQTT within the item file and no rule. I Just figured it out.
Just use \ to escape the colon ( : ) like example:

    //ITEM
    Switch Awtrix_Power "Awtrix Power" { mqtt=">[broker:awtrix/basics:command:ON:{\"power\"\\: true}]"}

It will send the JSON payload: {“power”: true}
No Rules or maps needet. i used MQTT 1x biding
Maybe it will help someone in the future.