# Extracting temperature from msg.payload

**URL:** <https://discourse.nodered.org/t/extracting-temperature-from-msg-payload/80242>\
**Category:** General\
**Created:** [2 August 2023 02:55 UTC](https://discourse.nodered.org/t/extracting-temperature-from-msg-payload/80242 "2023-08-02T02:55:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![arishy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/arishy/32/81679_2.png) [@arishy](https://discourse.nodered.org/u/arishy)\
**Post date:** [2 August 2023 02:55 UTC](https://discourse.nodered.org/t/extracting-temperature-from-msg-payload/80242/1 "2023-08-02T02:55:12Z")

</div>

Here is what mqttin capture:  
05:47:56.858 MQT: tele/Den\_ac/SENSOR = {"Time":"2023-08-02T05:47:56","AM2301":{"Temperature":29.8,"Humidity":41.1,"DewPoint":15.2},"TempUnit":"C"}  
I pass it to json node to converted to an object:  
Here is the result:  
"{"Time":"2023-08-02T05:47:56","AM2301":{"Temperature":29.8,"Humidity":41.1,"DewPoint":15.2},"TempUnit":"C"}"

Now I need to write a function to extract Temperature. Can you help. All my attempt gave me the msg Temperature is not defined !!!!

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [2 August 2023 03:38 UTC](https://discourse.nodered.org/t/extracting-temperature-from-msg-payload/80242/2 "2023-08-02T03:38:10Z")

</div>

Hi @arishy.

If you feed your data into a debug node it looks like this:

 ![Untitled 4](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/7/173418a45128c1428d6989ad4bf27d77d06be4b2.jpeg)

The Copy path button tells you how to access the Temperature value (you can paste it into Notepad or wherever).  
In this case it is msg.payload.AM2301.Temperature

> [@arishy](#):
>
> I pass it to json node to converted to an object

The default mqtt-in node behaviour is to to bring the data into Node-red as a string, but you can set it to create a JSON object. There is no need for a json node to convert it.

> [@arishy](#):
>
> Now I need to write a function to extract Temperature.

Try to avoid the function node becoming your first choice in any situation. Node-red has built-in or installable nodes for many programming tasks.  
If you want msg.payload to equal the temperature value, you can use a change node like this

 ![Untitled 5](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/9/19cc7a1986c38e747ab164ed62739873c6452aff.jpeg)

But do you need to do this? You can generally use the message property msg.payload.AM2301.Temperature directly - eg in a dashboard gauge.

---

<div class="post-metadata">

**Author:** ![arishy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/arishy/32/81679_2.png) [@arishy](https://discourse.nodered.org/u/arishy)\
**Post date:** [2 August 2023 04:06 UTC](https://discourse.nodered.org/t/extracting-temperature-from-msg-payload/80242/3 "2023-08-02T04:06:00Z")

</div>

This is great response J.  
I also appreciate your advice of not using function node. And let Node-red does it is magic.  
Can you please correct my understanding of the issue in hand:  
The mqtt capture a string. To EXTRACT anything from string in case it is text but dressed as json TEXT; you need to convert it to JS Object. and you have the two approaches:  
The node/function approach OR the smart way because Node-Red is actually doing it for you in the debug node.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [2 August 2023 04:21 UTC](https://discourse.nodered.org/t/extracting-temperature-from-msg-payload/80242/4 "2023-08-02T04:21:39Z")

</div>

I think the mqtt-in node has changed it's behaviour, I'm not sure when, and I'm currently running a beta version so maybe mine is different to yours.

It's not the debug node doing anything special, you can instruct mqtt-in to pass [valid] messages as javascript objects, so that you can access elements using the dot notation.

It used to be that by default, a valid Json format string would be imported to Node-red as a string.  
In that case, passing the string through a json node would convert it to a javascript object.  
But there is an option in the mqtt node config to treat the input string as a parsed json object so you don't need the json node.

---

<div class="post-metadata">

**Author:** ![arishy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/arishy/32/81679_2.png) [@arishy](https://discourse.nodered.org/u/arishy)\
**Post date:** [2 August 2023 04:32 UTC](https://discourse.nodered.org/t/extracting-temperature-from-msg-payload/80242/5 "2023-08-02T04:32:04Z")

</div>

Thank you for the correction. So; the proper way is to ask mqtt to produce JS object. Then in the debug note do your virtual extraction and Node-red provide help in the debug note to formulate the msg.payload the way you want.  
If I want to create flow variable like temperature etc I will need a function. But even that you remarked that you do not need to because you can use msg.payload.AM2301.Temperature as your ready made variable.

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [2 August 2023 05:06 UTC](https://discourse.nodered.org/t/extracting-temperature-from-msg-payload/80242/6 "2023-08-02T05:06:03Z")

</div>

Just to add my "6-pennies worth"...  
In Node-RED v3.1.0 the MQTT-In node has additional options in the 'Output' drop-down.  
 ![nr_version](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/f/4fb31005ebdc30603a8ac314ea75c4f5b8bf138c.png)  
I leave my MQTT-In nodes on 'auto-detect' as Node-RED seems to do a good job on that setting.  
 ![mqtt_in](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/9/f9ac22b2dc826a62e5882c795d4482347e5262be.png)

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [2 August 2023 05:27 UTC](https://discourse.nodered.org/t/extracting-temperature-from-msg-payload/80242/7 "2023-08-02T05:27:25Z")

</div>

Thanks for the clarification Dave.

---

<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:** [1 October 2023 05:27 UTC](https://discourse.nodered.org/t/extracting-temperature-from-msg-payload/80242/8 "2023-10-01T05:27:54Z")

</div>

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