String to number

Hello
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:

Nick

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

4 Likes

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

2 Likes

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)

Hello

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

Picture

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;
9 Likes

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

[{"id":"ba62ae94.76cd78","type":"ui_gauge","z":"6dc690a3.1abc88","name":"","group":"88342af7.aabb3","order":0,"width":0,"height":0,"gtype":"gage","title":"Gauge","label":"units","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":251,"y":1335,"wires":[]},{"id":"5a7769b0.762a9","type":"inject","z":"6dc690a3.1abc88","name":"","topic":"","payload":"20","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":85,"y":1332,"wires":[["ba62ae94.76cd78"]]},{"id":"87df3a5c.06d738","type":"inject","z":"6dc690a3.1abc88","name":"","topic":"","payload":"40","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":84,"y":1402,"wires":[["ba62ae94.76cd78"]]},{"id":"88342af7.aabb3","type":"ui_group","z":"","name":"Default","tab":"afb71d0f.e96268","disp":false,"width":"6"},{"id":"afb71d0f.e96268","type":"ui_tab","z":"","name":"Home","icon":"dashboard"}]

@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:

1 Like

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?