Formatting of JSON MQTT Publish

I’m trying to format an MQTT.Publish to run as part of an automation action…

I’m trying to convert this:-

{ "command": "notifu.exe", "args": "/m "Doorbell Rung" /p System", "user": "", "visible": true, "fallback": true }

which doesn’t work

into

{ "command": "notifu.exe", "args": "/m \"Doorbell Rung\" /p System", "user": "", "visible": true, "fallback": true }

which does.

Both are acceptable to the HA config file parser, but because I need an extra layer of escaping on “Doorbell Rung” I’m having difficulties. Any tips?!

This is my payload from the automation action:-

"{ \"command\": \"notifu.exe\", \"args\": \"/m \"Doorbell Rung\" /p System\", \"user\": \"\", \"visible\": true, \"fallback\": true }"

Thanks!!

If I try to use ascii code 08,

"{ \"command\": \"notifu.exe\", \"args\": \"/m \b\"Doorbell Rung\b\" /p System\", \"user\": \"\", \"visible\": true, \"fallback\": true }"

I get the following bizareness.

{ "command": "notifu.exe", "args": "/m "Doorbell Rung" /p System", "user": "", "visible": true, "fallback": true }

Got there in the end, this was the answer for anyone coming after me with the same problem!

payload: "{ \"command\": \"notifu.exe\", \"args\": \"/m \\\"Doorbell Rung\\\" /p System\", \"user\": \"\", \"visible\": true, \"fallback\": true }"

Did you find a solution for this? I’m in a similar boat and looking at using the to_json but can’t figure out how / if it will work with mqtt publish. So far all my attempts have failed.

Yep, this works

payload: "{ \"command\": \"notifu.exe\", \"args\": \"/m \\\"Arming alarm\\\" /p Alert\", \"user\": \"\", \"visible\": true, \"fallback\": true }"

Re-reading it seems your case is slightly different than mine. But for cleaner more readable code have you tried using single quotes on the outside to minimize (or maybe eliminate) the escaping slashes?

Something like this (would have to check syntax

payload: `{ "command": "notifu.exe", "args": "/m \\"Arming alarm\\" /p Alert", "user": "", "visible": true, "fallback": true }`

Thanks. Nope, not tried it. It works, and it took a LOT of trial and error getting there. Ain’t messing with it now!! :crazy_face:

Hope you find the answer you need. :slight_smile: