Sensor Values in Function Node

Apologies in advance for the question, new to node red.
I am trying to use a function node to create a string that I can then pass as a payload to a call service node that will be used for tts
when i use the below is simply passes the text as is, not the value at the time.

I am using more sensors in the function node, the above is just one

msg.payload =
"The temperature is "+
return msg

Welcome to the forum @wdwithmp.

That syntax is for use in a Template node, which might well be the best way of achieving what you want here. To do the same in a function node, which runs javascript use

msg.payload = "The temperature is " + states.sensor.bedroom_temperature.state


msg.payload = `The temperature is ${states.sensor.bedroom_temperature.state}`

That is assuming that states is a local variable in the function.

To get you going with node-red I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

Hi Mate,

Thanks heaps for the reply
I did try this and it did not work as expected, then I saw your comment "assuming that STATES is a local variable in the function" which I obviously don't understand

thanks for the link, will watch the videos and try to figure this out, if I manage to find the answer I will post it here so someone else might find it, if needed

I said that because I suspect that states might be passed in via the incoming message, in which case maybe it should be msg.states.sensor... or msg.payload.states.sensor.... As well as watching the videos have a read through the node red doc Working with Messages which will help with that. In particular note how you can get the path of a value from the debug pane, which can be very helpful when trying to address it.

Thanks mate, will give it a shot for sure

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.