String to number

#1

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

0 Likes

#2

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

Nick

0 Likes

#3

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

1 Like

#4

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

1 Like

#5

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)

0 Likes

#6

Hello

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

0 Likes

#7

Have you tried parseInt() ...

... or parseFloat() ...

.... in a function node ?

0 Likes

#8

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;
1 Like

#9

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

0 Likes

#10

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.

0 Likes

#11

Indeed - simple strings do seem to work on latest.

0 Likes