# Sensor Values in Function Node

**URL:** <https://discourse.nodered.org/t/sensor-values-in-function-node/41748>\
**Category:** General\
**Created:** [28 February 2021 09:26 UTC](https://discourse.nodered.org/t/sensor-values-in-function-node/41748 "2021-02-28T09:26:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wdwithmp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wdwithmp/32/35639_2.png) [@wdwithmp](https://discourse.nodered.org/u/wdwithmp)\
**Post date:** [28 February 2021 09:26 UTC](https://discourse.nodered.org/t/sensor-values-in-function-node/41748/1 "2021-02-28T09:26:33Z")

</div>

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.  
{{{states.sensor.bedroom\_temperature.state}}}

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

msg.payload =  
"The temperature is "+  
"{{states.sensor.bedroom\_temperature.state}}"  
return msg

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 February 2021 09:43 UTC](https://discourse.nodered.org/t/sensor-values-in-function-node/41748/2 "2021-02-28T09:43:57Z")

</div>

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

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

```

or

```auto
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](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). 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.

---

<div class="post-metadata">

**Author:** ![wdwithmp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wdwithmp/32/35639_2.png) [@wdwithmp](https://discourse.nodered.org/u/wdwithmp)\
**Post date:** [28 February 2021 10:47 UTC](https://discourse.nodered.org/t/sensor-values-in-function-node/41748/3 "2021-02-28T10:47:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 February 2021 13:21 UTC](https://discourse.nodered.org/t/sensor-values-in-function-node/41748/4 "2021-02-28T13:21:33Z")

</div>

> [@wdwithmp](#):
>
> I saw your comment "assuming that STATES is a local variable in the function" which I obviously don't understand

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.

---

<div class="post-metadata">

**Author:** ![wdwithmp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wdwithmp/32/35639_2.png) [@wdwithmp](https://discourse.nodered.org/u/wdwithmp)\
**Post date:** [1 March 2021 06:14 UTC](https://discourse.nodered.org/t/sensor-values-in-function-node/41748/5 "2021-03-01T06:14:25Z")

</div>

Thanks mate, will give it a shot for sure

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [30 April 2021 06:14 UTC](https://discourse.nodered.org/t/sensor-values-in-function-node/41748/6 "2021-04-30T06:14:59Z")

</div>

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