Help with Amazon Polly via Node-Red Call Service TTS

Hello All, I was originally using Google TTS in Node-Red and everything worked great! I wanted to switch over to Polly for the SSML capabilities and have run into a snag, how to properly format the JSON string for the Call Service node to make this work.

I have updated my configuration.yaml file to include what’s necessary to set up Amazon Polly and can make it work in my automation’s, so I know that the set-up is OK and that this is possible.

What I have done in Node-Red is set up a Template Node and added the following:

{"data_template":{"message":"<speak>test</speak>","entity_id":"media_player.entryway_speaker"}}

This is then sent to the Call Service node for TTS, but it does not work. I have formatted the JSON over and over again with no luck, I even used a YAML to JSON converter thinking that it was my lack of experience with JSON that was the problem but still no luck.

What I am looking for is the correct syntax for a basic string message and the correct syntax for a basic string message that includes the payload data from a previous node ie:{{payload}}.

Don’t think that this is relevant but my set-up is RPI3B running HASS.IO and the Node-Red add-on.

Any help would be greatly appreciated.
Thanks

This works for me …

[{"id":"6508075e.eb9ad8","type":"inject","z":"f6747993.c9b448","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":540,"wires":[["85f50c58.a9418"]]},{"id":"ed2a0f12.44a7e","type":"api-call-service","z":"f6747993.c9b448","name":"","server":"9a5aae17.6700c","service_domain":"tts","service":"amazon_polly_say","data":"","mergecontext":"","x":590,"y":540,"wires":[[]]},{"id":"85f50c58.a9418","type":"function","z":"f6747993.c9b448","name":"Message","func":"var message = \"<speak>\" +\n    \"Hello from Amazon Polly.\" +\n    \"<prosody rate='slow' pitch='-2st'>\" +\n    \"Can you hear me now?\" +\n    \"</prosody>\" +\n    \"<emphasis level='moderate'>\" +\n    \"This is an important announcement.\" +\n    \"</emphasis>\" +\n    \"<emphasis level='reduced'>\" +\n    \"This, not so much.\" +\n    \"</emphasis>\" +\n    \"</speak>\"\n\nmsg.payload =\n    {\n        \"data\":\n        {\n            \"entity_id\": \"media_player.kitchen\",\n            \"message\": message\n        }\n        \n    }\n\nreturn msg;","outputs":1,"noerr":0,"x":360,"y":540,"wires":[["ed2a0f12.44a7e"]]},{"id":"9a5aae17.6700c","type":"server","z":"","name":"Home Assistant","url":"http://192.168.0.66:8123","pass":""}]

I have used a function node just for convenience; a change node would work too.

Thanks for the response. Based upon the syntax that works for you, I came up with the following, which is what I am using for my template node - a format of mustache template:

{\"data_template\": {\"message\":\"<speak>testing polly {{payload}}</speak>\", \"entity_id\": \"media_player.entryway_speaker\"}}

The debug output of the template node’s msg.payload is:

{\"data_template\": {\"message\":\"<speak>testing polly again</speak>\", \"entity_id\": \"media_player.entryway_speaker\"}}

Still no luck. Anyone using the call service node to accomplish this?

Why are you using data_template rather than just data?

Based upon the home Assistant Polly use file. See here:

Amazon Polly - Home Assistant

I also tried just data and got the same result.

data_template would work from HA, but perhaps not from NR. Nevertheless, if you’re getting no result from data that can’t be the problem. Does the code I posted work for you? And can you post your code so I can try?

I did try to use your code as provided and changed the area specific items as needed like the speaker entity id and so on and it did not work. I’ll try it again and also provide the code I am using in each node.

Thanks for the help. :slight_smile:

Given that code that works for me doesn’t work for you, it sounds more like a config problem than a coding one. You should probably check your aws_access_key_id and aws_secret_access_key. And maybe restart HA, try again, and then look for anything relevant in the HA log. Actually rereading your post, you said you’ve got it working in HA automations, so that suggests the config is ok. Curious. Could it be related to the HA connection in NR? Do you have other NR flows working successfully with HA? Maybe check the NR log in hass.io as well.

Well, not sure if this is related but my CF card was bad and I had to start over. I did have back-ups but given the problems I was having, I decided to stat from scratch. If I decide to use Node-Red again, I’ll test out your code and report back my findings.

For anyone reading this having problem between node-red and Home Assistant. Your quest, like mine, is finally over. Hopefully.

As Mustash htmlentities the payload, and not handling single and double quotation well;
The easiest way around this is to base64-encode your message before sending it to Home Assistant. In Home Assistant, use message: "{{ message | base64_decode }}" to decode it. Works like a charm!

Hope this helps!

Edit: I found that having {{{payload}}} in the message payload would work better in some instances.

Can you provide a working example? I used the one above and my Sonos speaks the Polly…but it also speaks all the variables as everything is in double quotes.

@michaelblight

Anyone have a workaround for this? The following taken from the function node in the example above speak EVERYTHING not just the intended “speak” text. What is wrong with the formatting?

var message = "<speak>" +
    "Hello from Amazon Polly." +
    "<prosody rate='slow' pitch='-2st'>" +
    "Can you hear me now?" +
    "</prosody>" +
    "<emphasis level='moderate'>" +
    "This is an important announcement." +
    "</emphasis>" +
    "<emphasis level='reduced'>" +
    "This, not so much." +
    "</emphasis>" +
    "</speak>"

msg.payload =
    {
        "data":
        {
            "entity_id": "media_player.sonos_playbar",
            "message": message
        }
        
    }

return msg;