Trying to convert mqtt output to input into influxdb

Hello,

i need your help with converting mqtt outputs.
I have a bunch of "mqtt in" nodes to recieve messages from a mqtt server.
These messages are sturctured like this:
debug
The payload of these messages need to look like this:
debug_2
...so they can be input correctly into influxdb.

How can I achieve the obove?

Thank you for your help!

Hi @phkul.

If your payload is a string with spaces between the elements, you could use a function like this

let myarray = msg.payload.split(" ");
msg.payload = {
    "name": myarray[0],
    "value": myarray[1].split("=")[1],
    "time": myarray[2],
}
return msg;

Edit @bakman2 remembered to convert the value to a number :grinning_face_with_smiling_eyes:
You may need to convert the date/time too?
And we both assumed that your "vaule" was a typo.

You could use a function node to split the string;

// msg.payload input =  "something_german value=192.00 1234567890000"

let m = msg.payload.split(" ")
let name = m[0].trim()
let value = m[1].split("=")[1]
let time = m[2].trim()

msg.payload = {name,value:parseFloat(value),time}

return msg;

output:

msg.payload : Object
object
name: "something_german"
value: 192
time: "1234567890000"

edit @jbudd typed a bit quicker :wave:

Most of the time it is easier to get the sending device to format the JSON correctly, than to use some fancy foot work in Node-red to fix it.

Just a note, I know sometimes it is not possible.

@jbudd @bakman2 thank you so much for your help. Your solutions solved my problem perfectly!