Wrong output to influxdb (Data as a string)

Hi,
... please could someone help me how "correctly" write values (with tags) to influxdb.
The values in influxdb are not correctly writing (with " " symbol - like STRING)

Thanks

What is coming out of mqtt?

in node-red it looks like ..
image

There is a fairly large issue with InfluxDB that, once you have written to a field, it takes the data type that you first wrote to it. So if you meant to send a float for example but the first time you outputted a string - you have an issue.

There are only two ways around this.

  • Wait for (or arrange) a new "shard" to be created for your database. At which point, your numbers will be taken as numbers for the new shard.
  • Drop the series and recreate it with the correct data.
3 Likes

.. nope .. new field (which is created after drop) is again like "string" ... the nodered send like string and not like value :frowning: ... I think nodered have issue with multiple value in payload. I see the value like Ts: "23441" and not Ts: 23441 in debug window (see
image ).

modul JSON make this, but only with one value - one msg.payload .. and also in influxdb is like value

It looks like you're trying to save an entire JSON string as the value. I recommend looking at the influx batch node and moving those values into apropriate fields and tags for the same measurement.

Example:

1 Like

I believe that the problem is that the values of Ts etc in the string from the MQTT node are already strings. Are they strings in the object you are sending to MQTT?
If you can't change it at that end then you will need to convert then to numbers after the JSON node. You can do that to each one in a function node using the JavaScript Number() function.

so looks raw mqtt

the object is wrong defined in mqtt ...
right is Object - { "a": 1, "b": 2} and not Object - { "a": "1", "b": "2"}
its not like number but like string

... can somebody help write same script for correct this?

take a look at this: https://cookbook.nodered.org/mqtt/receive-json

1 Like

thanks for tips, but not working .. still same

Put a debug nde after the JSON node and set it to display the complter msg object. What does it show?


Hi,
I had the same problem and fixed using a function node to cast all strings to int and float where possible:

function TryParseInt(str, defaultValue) { let v=parseInt(str); return v==str ? v : defaultValue; }
function TryParseFloat(str, defaultValue) { let v= parseFloat(str); return v==str ? v : defaultValue; }


var payload=msg.payload;
for (var key in payload) {
    if (payload.hasOwnProperty(key)) {
        payload[key]=TryParseInt(payload[key],payload[key]);
        payload[key]=TryParseFloat(payload[key],payload[key]);
    }
}


msg.payload=payload;
return msg;

This information (that InfluxDB takes the data type first wrote to it) has just saved me a huge amount of time in debugging.

Thanks!