# Node-Red mqtt node in String sorting with javascript

**URL:** <https://discourse.nodered.org/t/node-red-mqtt-node-in-string-sorting-with-javascript/54703>\
**Category:** General\
**Created:** [4 December 2021 09:10 UTC](https://discourse.nodered.org/t/node-red-mqtt-node-in-string-sorting-with-javascript/54703 "2021-12-04T09:10:17Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![saeed1](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@saeed1](https://discourse.nodered.org/u/saeed1)\
**Post date:** [4 December 2021 09:10 UTC](https://discourse.nodered.org/t/node-red-mqtt-node-in-string-sorting-with-javascript/54703/1 "2021-12-04T09:10:17Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/1/c1209b621506021ecbba93ab94ad26812c9181fd.png)

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [4 December 2021 09:13 UTC](https://discourse.nodered.org/t/node-red-mqtt-node-in-string-sorting-with-javascript/54703/2 "2021-12-04T09:13:47Z")

</div>

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:

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

```

Then share the resulting output.

---

<div class="post-metadata">

**Author:** ![saeed1](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@saeed1](https://discourse.nodered.org/u/saeed1)\
**Post date:** [4 December 2021 09:20 UTC](https://discourse.nodered.org/t/node-red-mqtt-node-in-string-sorting-with-javascript/54703/3 "2021-12-04T09:20:07Z")

</div>

@knolleary thank you very much for quick support.

I did that and i am getting the following output

![nodered_prob_snip2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/c/0c22357fb404097e82fb33f2fd6a90f6d91d3248.png)

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [4 December 2021 09:26 UTC](https://discourse.nodered.org/t/node-red-mqtt-node-in-string-sorting-with-javascript/54703/4 "2021-12-04T09:26:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![saeed1](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@saeed1](https://discourse.nodered.org/u/saeed1)\
**Post date:** [4 December 2021 09:34 UTC](https://discourse.nodered.org/t/node-red-mqtt-node-in-string-sorting-with-javascript/54703/5 "2021-12-04T09:34:35Z")

</div>

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

```auto

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;

```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [4 December 2021 09:46 UTC](https://discourse.nodered.org/t/node-red-mqtt-node-in-string-sorting-with-javascript/54703/6 "2021-12-04T09:46:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![saeed1](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@saeed1](https://discourse.nodered.org/u/saeed1)\
**Post date:** [4 December 2021 09:51 UTC](https://discourse.nodered.org/t/node-red-mqtt-node-in-string-sorting-with-javascript/54703/7 "2021-12-04T09:51:38Z")

</div>

![nodered_prob_snip3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/c/bcd494999a809118460cbfbf4392a5f3c6da8fc9.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [4 December 2021 09:56 UTC](https://discourse.nodered.org/t/node-red-mqtt-node-in-string-sorting-with-javascript/54703/8 "2021-12-04T09:56:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![saeed1](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@saeed1](https://discourse.nodered.org/u/saeed1)\
**Post date:** [4 December 2021 13:59 UTC](https://discourse.nodered.org/t/node-red-mqtt-node-in-string-sorting-with-javascript/54703/9 "2021-12-04T13:59:36Z")

</div>

@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???

 ![Screenshot (434)](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/3/e394c5a86b4e66884d934223a41355197e693970.png)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [2 February 2022 14:00 UTC](https://discourse.nodered.org/t/node-red-mqtt-node-in-string-sorting-with-javascript/54703/10 "2022-02-02T14:00:03Z")

</div>

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