String to number

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;
8 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:

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?

Hi @Colin

I deleted that function now, but it was something like this:

//read payload
var stringValue = msg.payload;

//convert string to float to 2 decimal places
var floatValue = parseFloat(stringValue).toFixed(2);

//set and return payload
msg.payload = floatValue;
return msg;

Found here originally.

This is the flow I am using it for, to convert the seperate mqtt strings into floats, joint together into a "key/value" object, then format for influxdb (influxdb needs float values to match the existing data type).

1 Like

To diagnose it we would need a failing example. However, I believe that parseFloat is fairly strict on what you can give it, whereas I think Number is a little more forgiving, so perhaps you didn't just have a straight number in the string.

In the flow you posted an image of are the three functions identical? If so then you don't need all three, feed all the MQTT nodes into one function.

1 Like

Hmm I did not get a notification so apologies for the delay in replying.

Short answer:

It's working so it may be worth leaving, thanks for noticing the multiple node, I will try with only one.

Long answer:

Starting at the beginning, my Arduino code is doing this (simplified):

//Setup variables garage
  float temp_garage = (garage.cTemp);
  int hum_garage = (garage.humidity);

 if (garage.get() == 0)  //garage sensor is responding
  {
    //Write garage temp
    MQTTclient.publish(temperature_garage_topic, String(temp_garage , 1).c_str(), true);
    //
    //Write garage humidity
    MQTTclient.publish(humidity_garage_topic, String(hum_garage).c_str(), true);
}

And the mqtt nodes are outputting strings:

(I need to check if there is a way to publish number values with pubsub, instead of strings).

The influxdb node was throwing up the error:

Error: A 400 Bad Request error occurred: {"error":"partial write: field type conflict: input field on measurement is type string, already exists as type float dropped=1"}

Which I found was due to it being a string, not a float value as already exists in the database. Using the parseFloat function caused the same error.

Very elegant solution. Greetings.

thanks mate, you helped me today <3

Greetings. New to this process of becoming totally befuddled and confused. After some trial and error have made some progress!

Temperature data via an ESP32 is sent to a Raspberry Pi having MQTT, Node-Red, InfluxDB and Grafana. Using Micropython code available online from Randomnerdtutorials.

In Node-Red it shows the temperature reading as a string - containing a comma at the end. Example: "24.6,"

Two issues arise - (a) getting rid of the comma and
(b) converting the string to a float value.

The change node was used to remove the comma. In series another change node was utilised to set the string to a number. Currently stuck at converting the decimal number into a float in order for Grafana to access it from InfluxDB as the following error message given in Grafana is: InfluxDB Error: unsupported mean iterator type: *query.stringInterruptIterator

The following is an example reading from the debug

9/9/2021, 5:49:17 PMnode: 311e36be4ae4f2ed
esp/dht/temperature : msg.payload : number

26.6

Update: After spending far too much time on this found the tofloat node (as opposed to the toFloat one which caused an error message) and am happy to report that the first Grafana graph has shown up under the current set-up. Some testing to be done!