Return a msg.payload with more digits and decimals

Hi, my msg.payload is returning a temperature sensor value that is supposed to be as 56,7deg, which means composed at least with 2 digits and 1 decimal.
What should I write in the function node to get this done as I'm getting a temperature only as 5deg, see the below screenshot for more details.


Thanks !

So the first question is ... does the sensor return decimal values? What sensor are you using?

Yes the sensor returns decimal values, one in this case which is for the skin temperature of a motor.
I'm using a smart sensor mounted to a motor body that measures the temperature.

If the sensor is giving you the value 5, then Node-RED isn't going to be able to turn that into 56.7 without more information.

It looks like you are making an http request to get the data? Do you have any information about the API you are using? If that API isn't returning the data you want, then it sounds like any issue with the API or how you are calling it.

The sensor is giving the value 50.0000 at the moment as you can see on the below screenshot.
The returned value after the API is only 5, the question is how can I get XX.X in my msg.payload after my below API:

var Packdata =
{
"CT9.711_SkinTemp" : msg.payload[0],
}
var AvevaData = {};
AvevaData["name"]=msg.payload["name"];
AvevaData["id"]=msg.payload["id"];
AvevaData["timestamp"]=msg.payload["timestamp"];
AvevaData["data"]=Packdata;

msg.payload = AvevaData;
return msg;

You keep talking about your API, but that is a function node, not an API. Is the value showing that the payload has the value "50.000" showing what is going into your function? If so then that is a string, so when you say msg.payload[0] in the function that will only the the first digit. Assuming that you want to convert that to a number from a string then you want

const PackData = {propertyname: Number(msg.payload)}

In future when posting code please copy/paste it here, then I could have copied the relevant code and pasted it here.
However, I do not believe that the debug output you show comes from the function that you posted. Show us clearly what is in the message going into your function.

The value showing that the payload has the value ''50.000'' comes from a Change Node connected to my HTTP_CT9.711 request, see the below screenshot.

Is there anything I should add in the function node to see the value as 50.000 like a padStart() function or toFixed()?
"CT9.711_SkinTemp" : msg.payload[0]

Thanks again.

Change that to
"CT9.711_SkinTemp" : Number(msg.payload)
By using msg.payload[0] you are selecting the first character of the string "50.000", which is a 5.

It finally worked with that !
Then
"CT9.711_SkinTemp" : Number(msg.payload).toFixed(1)
if I want only 1 decimal.
Perfect !

I don't know what you are doing with it, but by calling toFixed() you have converted it back to a string. It is generally best to leave numbers as numbers and only convert and reduce precision when you want to display them.

1 Like

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