String to number

I am reaading one information MQTT in msg.payload. But i receive one string

How i can convert it to number ?
My intent is show this number in one graph or chart

Thank you

The Node-RED Function node uses JavaScript. A quick google search for how to do that will reveal lots of answers, for example:


Or you can use a Change node and a bit of JSONata
e.g string "1234" to 1234

1 Like

though the ui_chart node should cope with a number like string just fine…

1 Like

remember if you are looking what comes out of an MQTT in node, if you will get a JSON string if the published message was an object. So adding a debug node to the output of the MATT in node will show you the msg type and you can see if you need/want to run it thru a ‘JSON’ node before using it (this has bit me in the past)


As you can see, the values comming from topic Umidade (MQTT) I see as string


I need to convert to number, because the chart does not change

Have you tried parseInt() ...

... or parseFloat() ...

.... in a function node ?

I am surprised that the gauge node will not work with a string, but to convert it you can use a function node containing

msg.payload = Number(msg.payload);
return msg;

Me too, will look at that. (Though to be fair the original question did say chart :slight_smile:

Sending strings to Gauge node works fine for me


@ricardokemp how have you got the gauge configured? Post a screenshot or export the flow for the Gauge node.
Also post what you see in the node-red log when node-red is started so we can see the versions of everything. The command node-red-log should show you the log if you are not running node-red in a terminal.

Indeed - simple strings do seem to work on latest.

Thanks for opening my eyes to JSONata. This is easy and makes the Flow very understandable and reusable without writing a function. My initial attempt failed because the input was a string array from a web site. Changing the output to $number(payload[0]) fixed that.

1 Like

Thx for this, you save me :slight_smile:

my string is not only combine number , it also have word on it . like openPercent: 50 . What I should do ?

Can you feed the string into a debug node and show us what the debug node shows please?

1 Like

I just solve my problem with a function node thank !!!

There may well be a better way, I suspect that you are parsing strings instead of converting JSON to javascript objects, but without more detail I cannot be certain.

Thanks for the Number() solution hint, solved a problem for me.

msg.payload = Number(msg.payload);
return msg;

Worked for me, could not get parseFloat() working for some reason. Thank you

What did you try?