Node-red replacing speech marks with "&quot" in call service data

As of 0.117 you can now Cast plex to and google cast device.

I can make it work by entering details into the call service node.

Domain: media_player
Service: play_media
Data:
{
    "entity_id": "media_player.chromecast",
    "media_content_type": "movie",
    "media_content_id": "plex://{\"library_name\": \"Calibration\", \"title\": \"Fireplace 3hrs-1\"}"
}

But I would like to use the the power of node red by creating payloads for the different parts of Data.

So for example I have a change node that sets…

msg.entity_id to… media_player.chromecast
msg.media_content_type to… movie
msg.media_content_id to … plex://{“library_name”: “Calibration”, “title”: “Fireplace 3hrs-1”}

Then in my call service node I have …

Domain: Media_player
Service: play_media
Data:
{
    "entity_id": "{{entity_id}}",
    "media_content_type": "{{media_content_type}}",
    "media_content_id": "{{media_content_id}}"
}

But when trigger the flow it doesn’t work. If I enable the debug I can see it has changed the data.media_content_id to …

plex://{"library_name": "Calibration", "title": "Fireplace 3hrs-1"}

Any Ideas how to stop it changing the characters to text?

Here is some exsample node-red flows

[{"id":"d6e084eb.9f4328","type":"inject","z":"5e5759.72a178a8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":700,"y":480,"wires":[["32907b54.0940a4"]]},{"id":"eb499929.a70008","type":"api-call-service","z":"5e5759.72a178a8","name":"","server":"56e35aef.6eb604","version":1,"debugenabled":true,"service_domain":"media_player","service":"play_media","entityId":"","data":"{\"entity_id\":\"{{entity_id}}\",\"media_content_type\":\"{{media_content_type}}\",\"media_content_id\":\"{{media_content_id}}\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1210,"y":480,"wires":[[]]},{"id":"32907b54.0940a4","type":"change","z":"5e5759.72a178a8","name":"Set Cast media","rules":[{"t":"set","p":"msg.entity_id","pt":"msg","to":"media_player.chromecast","tot":"str"},{"t":"set","p":"msg.media_content_type","pt":"msg","to":"movie","tot":"str"},{"t":"set","p":"msg.media_content_id","pt":"msg","to":"plex://{\"library_name\": \"Calibration\", \"title\": \"Fireplace 3hrs-1\"}","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":940,"y":480,"wires":[["eb499929.a70008"]]},{"id":"b3a1065e.9e3e28","type":"api-call-service","z":"5e5759.72a178a8","name":"Cast Plex Fireplace on sony TV","server":"56e35aef.6eb604","version":1,"debugenabled":true,"service_domain":"media_player","service":"play_media","entityId":"","data":"{\"entity_id\":\"media_player.chromecast\",\"media_content_type\":\"movie\",\"media_content_id\":\"plex://{\\\"library_name\\\": \\\"Calibration\\\", \\\"title\\\": \\\"Fireplace 3hrs-1\\\"}\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1210,"y":420,"wires":[[]]},{"id":"abb88c7b.0f3f2","type":"inject","z":"5e5759.72a178a8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":700,"y":420,"wires":[["b3a1065e.9e3e28"]]},{"id":"56e35aef.6eb604","type":"server","name":"Home Assistant (working)","legacy":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open"}]

It’s tricky because the media_content_id is expected to be a properly formatted JSON payload prepended with plex://. Normally you can do this by single-quoting the whole payload without needing to escape the double-quotes like this:

    "media_content_id": 'plex://{"library_name": "Calibration", "title": "Fireplace 3hrs-1"}'

I don’t use Node-RED myself, but it looks like there’s a section specifically around handling JSON payloads: https://nodered.org/docs/user-guide/messages#working-with-json

By default, Mustache will replace certain characters with their HTML escape codes. To stop this happening, you can use triple braces: {{{payload}}} .

https://zachowj.github.io/node-red-contrib-home-assistant-websocket/guide/mustache-templates.html

4 Likes

Thank you both for your replies. I have made progress.

If I put it in an extra pair of brackets like a url {{{media_content_id}}} (thanks to Kermit) in the data section of the call service

{
    "entity_id": "{{entity_id}}",
    "media_content_type": "{{media_content_type}}",
    "media_content_id": "{{{media_content_id}}}"
}

I have to put in a backslash before each quote mark in the change node when I create the msg.media_content_id.

plex://{\"library_name\": \"Calibration\", \"title\": \"Fireplace 3hrs-1\"}

Which will work for my flow. Out of interest is there a way to get node red to add the back slashes for me? (If I don’t add them I get an array with every letter of the string on a different line.)

plex://{"library_name": "Calibration", "title": "Fireplace 3hrs-1"}

1 Like

I can’t tell if you tried this suggestion from before, but escaping the double-quotes shouldn’t be necessary if you enclose the whole payload in single-quotes:

'plex://{"library_name": "Calibration", "title": "Fireplace 3hrs-1"}'

Yeah I tried that. didn’t work for me. I have a working solution now though. Thank you