MQTT transformation before influxdb

Dear all,

I have a small problem and I am turning in round without solution. I am receiving MQTT topic and I have to divided them by 10 before to sent them to an influxdb database.
Simple but ...it is not working .Probably a problem of type of variable.

Here the function that I am trying to adapt

var msg2 = null;
var value = Number(msg.payload)/10;  // convert it to a number in case it is a string
if (!isNaN(value)) {
    msg2 = {payload: {"OT_item24": value}};
}
return msg2;

if somebody have an idea to help me...

have a good evening

Connect a debug before the function and after function. Show us what goes in and what comes out.

my Mqtt topic is OT_item24.

image

and here my node :image

I am feeling , I am very near but no....snif snif

The divide by 10 function is working. What's your issue?

no, I do not find the value on influxdb when I request OT_item24 by grafana. I am thinking it is because the value is not reconize as number

if I do without transformation is working nice

I don't think your function is populating the new message completely/properly but I'm new with this. Here is an example that works perfectly for me.
image

HTH

All you need is
msg.payload = Number(msg.payload)/10
return msg

In addition, if you set the mqtt node output to Parsed JSON object then it will automatically convert it to a number for you.

Thanks for your reply but when I do like that I have a debug after function : Error: A 400 Bad Request error occurred: {"error":"partial write: field type conflict: input field "value" on measurement "OT_item24" is type float, already exists as type string dropped=1"}

Thanks , I have tried your method but with it , I obtained the debug message :Error: A 400 Bad Request error occurred: {"error":"unable to parse 'OT_item24 device=undefined,value=18.8': invalid boolean"}

From MQTT node the output is string :

OT_item24 : msg.payload : string[3]
"186"

The clue is in the error message. By previously connecting the mqtt node directly the influx node, and without telling the mqtt node to parse the string it receives as a JSON string, you have sent a string to that field instead of a number. Now you can only send it strings. To fix that you will have to delete (drop) that measurement from the database. Or change the measurement name to a new name.

So gracias ! it is working perfectly. have a nice week

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.