Extracting temperature from msg.payload

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 !!!!

Hi @arishy.

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

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

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.

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

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

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.

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.

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.

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
I leave my MQTT-In nodes on 'auto-detect' as Node-RED seems to do a good job on that setting.
mqtt_in

Thanks for the clarification Dave.

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