Node RED - Passing Variables Into Call Service Node

Hi All,
Could use some help- I’ve been googling and faffing around to no avail. I’ve set up TTS, and now I’m trying to use a change node to pass a value into my TTS Call Service node, so that I can change the voice for all notifications with one node. I’ve tried using Global and Msg, tried different syntaxes, and I think I’m making a simple mistake. Below is my config- thank you in advance!!


2023-09-22 09_44_23-Node-RED – Home Assistant

You are using a JSONata expression.

The “{{global.vox}}” looks like mustache template syntax and the two don’t mix. Mustache templates don’t always work with object construction so it is better to use JSONata.

JSONata in HA Node-RED has a function $globalContext() which should be used to return the global context variable.

Without testing, and assuming everything else is correct, I believe

{“voice” : $globalContext(“vox”)}

should do the job.

In reality you don’t really need to use the global context. If you set, say, msg.vox to “YanNeural” in the change node,

{“voice” : vox}

would pick up the value required.

1 Like

This works beautifully! Thank you for taking the time to help me do that correctly. I ended up going with the msg.vox method you describe.

@Biscuit
Following up on this, trying to do the exact same thing in another call service node, but the code de-formats itself. Am I misunderstanding something obvious? Thanks for your help!!


The call service node has two options for the data field.

JSON expects JSON structures, and from your picture this is what you are editing. This only accepts literal, and mustache templates (but these do not work well for objects)

You need to set the field option to J: expression, for JSONata, which will work where you are using the incoming message fields as variables.

1 Like

I see that now- thank you for helping me find my mistake!

Do you have any interest in writing a JSONata guide geared toward using it with Home Assistant? Improving my reference guide with examples and other use cases.

That would help a lot of us - thanks for your investment!

Yes, very much so.

Your contribution to HA Node-RED is outstanding, and you have extensive documentation, however the same questions seem to appear all too regularly and I have recently thought about how best to augment both the documentation and its visibility and accessibility.

I am unsure on the best approach. I was thinking of a YouTube video covering ‘service calls’, to help visually demonstrate key points. I am not familiar with github, so I would need to learn how to update the document content.

I think the current documentation is good but it could use some more examples (in the cookbook) and more exposure.

I am guilty of trying to help answer questions in this forum without referring people to the existing documentation - I will try to do better. Being able to refer people to a YouTube video (primer) would be great.

I am very much a novice with both Github & YouTube but I am happy to help any way I can.

Tangential question to add here. Trying to use a similar .msg variable for an image path, with the same change node preceding the call service node. Notification works wonderfully with the full image path inserted, but not when referencing the .msg variable. What’s the correct way to format this?

@Biscuit Would greatly appreciate your insight. Many thanks!!

Not much insight to give I’m afraid. The Call Service node can be quite tricky to set up and debug.

You don’t say which service, but I assume ‘notification’ and you are using both actions and image. You also don’t say if the service call fails with an error, but I assume is just works but does not include the image.

Debug:
If this works with hard coded image file path (and everything else picked up from the message fields) then at least it works.
If it is not working when trying to pick up the path from msg.img, then I would start by double checking the output from the change node to ensure msg.img is being set, and to a string that matches the path.

There is a feature in the Service node to create output - you can add, for example, msg.sent as ‘sent’ which will, for a successful action, show the full data object pushed out to HA via the API call. If you do this for hard coded image path, and variable image path, then you might see a difference to provide some clue.

Since file paths have \ in them, these are often used as escape characters, so that may be getting the "\ confused when building a string. Sometimes we have to use alternative quotes ’ to get around this, or even build the string up from parts.

Also I would check out ‘img’ as your field use, since .img is also a file extension, and I would be tempted to use msg.filepath or something else just to avoid any conflict.

And, as always, make sure you are not using msg.payload.data in the input as the service call node merges such fields into the data path - you may also have to check if you have any context values in the ‘merge’ field.

Good luck with debugging