Node-Red mqtt node in String sorting with javascript

Hi Guys!

I am receiving a data srtring from a gateway at mqtt in node in node red.

I am not able to understand, why my node-red debug window is showing the string length of 41, however if i count manually, length is 37 character. Where are the other 4 characters.
This might be the reason i am getting NAN in temperature 3 fields. I tried to solved it but no way.

Where is the problem guys???

Thank you.
nodered_prob

There must be some hidden control characters at the end.

Pass the string into a Function node with the following and pass the result onto a debug node:

msg.payload = Buffer.from(msg.payload)
return msg

Then share the resulting output.

@knolleary thank you very much for quick support.

I did that and i am getting the following output

nodered_prob_snip2

Are you sure you're passing the original string to that function node? Very odd to get a 1 byte buffer from a 41 character string

I am sure. @knolleary . In mqtt node in , I am chosen a string (output), I have not chosen buffer, or autodected.


var payload = msg.payload;
payload = payload.toString().replace(/NAN/gi,"0")
payload = payload.split(',');

var nodeID;
var label1;
var humidity1;
var temperature1;
var humidity2;
var temperature2;
var humidity3;
var temperature3;


    nodeID = (payload[0]);
    label1 = "node_";
    nodeID = label1.concat(nodeID);
    

    
    humidity1 = ((payload[1]));
    temperature1 = ((payload[2]));


    humidity2 = ((payload[3]));
    temperature2 = ((payload[4]));

    humidity3 = ((payload[5]));
    temperature3 = ((payload[6]));


//to save all the measurement as numbers in the database


    humidity1 = Number(humidity1);
    humidity2 = Number(humidity2);
    humidity3 = Number(humidity3);
    temperature1 = Number(temperature1);
    temperature2 = Number(temperature2);
    temperature3 = Number(temperature3);









    humidity1 = Number(humidity1.toFixed(2));
    humidity2 = Number(humidity2.toFixed(2));
    humidity3 = Number(humidity3.toFixed(2));
    temperature1 = Number(temperature1.toFixed(2));
    temperature2 = Number(temperature2.toFixed(2));
    temperature3 = Number(temperature3.toFixed(2));
    
    
    var THI1 = (0.8*temperature1) + ((humidity1/100)*(temperature1-14.4)) +46.4;
    var THI2 = (0.8*temperature2) + ((humidity2/100)*(temperature2-14.4)) +46.4;
    var THI3 = (0.8*temperature3) + ((humidity3/100)*(temperature3-14.4)) +46.4;

    payload = [{"nodeID":nodeID,"humidity1":humidity1,"temperature1":temperature1,"humidity2":humidity2,"temperature2":temperature2,"humidity3":humidity3,"temperature3":temperature3,"THI1":THI1,"THI2":THI2,"THI3":THI3}];



    msg.payload = payload;
    msg.payload = Buffer.from(msg.payload)
    
return msg;

That is not what I meant. We want to see what the raw output of the mqtt node is - not what your parsing code is doing.

Pass the output from the MQTT node to the function code I provided and on to a debug node.

nodered_prob_snip3

As you can see there are two zero bytes on the front, and presumable another in the bit we can't see as you haven't expanded it. If you have control of whatever is publishing that then I suggest fixing the problem there.

@knolleary @Colin

Thank you very much. on my gateway in python script i have found some strange character pattern, then i trimmed it with .strip() method to got the clean string.
Could any body tell me, why my LoRa gateway is adding these stange pattern at the start and end of string and what they mean???

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