String to number

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!

1 Like