# Count and short objects in function node after uint16 modification

**URL:** <https://discourse.nodered.org/t/count-and-short-objects-in-function-node-after-uint16-modification/41120>\
**Category:** General\
**Created:** [16 February 2021 20:11 UTC](https://discourse.nodered.org/t/count-and-short-objects-in-function-node-after-uint16-modification/41120 "2021-02-16T20:11:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![panban-ux](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/panban-ux/32/21609_2.png) [@panban-ux](https://discourse.nodered.org/u/panban-ux)\
**Post date:** [16 February 2021 20:11 UTC](https://discourse.nodered.org/t/count-and-short-objects-in-function-node-after-uint16-modification/41120/1 "2021-02-16T20:11:10Z")

</div>

I have the following node:

```auto
[{"id":"f7844f50.7553c","type":"function","z":"11942aca.455835","name":"","func":"var lookup = {\n 73 : \"lemon\",\n 75 : \"pear\",\n 0 : \"apple\",\n 2 : \"orange\",\n 57 : \"kiwi\",\n 42 : \"grapefruit\",\n 1 : \"bad apple\",\n 255 : \"coconut\"\n \n}\n\nvar result = {};\nfor (let i = 0; i < msg.payload.length - 1; i=i+4) {\n let i1 = msg.payload[i];\n let i2 = msg.payload[i+2];\n let name = lookup[i1] || \"Unknown\" + i1;\n result[name] = {\n id: i1,\n value: i2\n }\n}\n\nmsg.payload = result;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":760,"y":200,"wires":[["ad3a363a.68ba28"]]}]

```

In which i sent byte objects from my python application in this form to node-red [0,0,213,2,1,0,96,0,2,0,1,0]. The first 4 bytes are referred to a particular object (here apples) i have stored in a JSON file. More specifically the first byte in order 0 shows my class id of apples, second byte is an extension of first after uint16 conversion.

Now here is the purpose of this post, byte 3 here with a value of 213 is my object counting for apples, when this value gets beyond 255+1 and overflows it resets to 0. Forth byte here with a value of 2 is showing that iterated 2 times(counted already 2 full bytes) by 256+256. My question is how am i possible whenever i reach a point of overflow to add these values to the position of byte 3 and get the accurate count of my particular object.  
Bear in mind that i inject this to TTN [0 725 1 96 2 1] in a byte array. As you will see my first object has 725 counts 256+256+213. The same goes from byte 5 and byte 9.  
Original post is the following, but until that time i had objects up until 200 so i had to do a modification in my python code to facilitate more

> [@Sort a payload array into object id's and into a UI](https://discourse.nodered.org/t/sort-a-payload-array-into-object-ids-and-into-a-ui/36582):
>
> In short i've got this array [73,4,75,2,56,2,58,1,57,1,45,1] and some how i want to manipulate this data into a UI dashboard just to monitor how many times an object appears. In this case 73 is the id in my back end application for a lemon and it appears 4 times and so on. To recap this sequence [0 2 4 6 8 10] contains the id's of my objects and odd sequence in the middle is the count times of the object. Where do i start for example if my array gives eg [73,45] so to draw in my ui i got 45 le…

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [16 February 2021 20:48 UTC](https://discourse.nodered.org/t/count-and-short-objects-in-function-node-after-uint16-modification/41120/2 "2021-02-16T20:48:14Z")

</div>

If i understand correctly

```auto
let i2 = msg.payload[i+2]+(msg.payload[i+3]*255);

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [16 February 2021 20:50 UTC](https://discourse.nodered.org/t/count-and-short-objects-in-function-node-after-uint16-modification/41120/3 "2021-02-16T20:50:29Z")

</div>

> [@panban-ux](#):
>
> In which i sent byte objects from my python application in this form to node-red [0,0,213,2,1,0,96,0,2,0,1,0].

I never understand why people make things difficult for themselves.

Why not send something extensible and easy to convert like JSON?

---

<div class="post-metadata">

**Author:** ![panban-ux](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/panban-ux/32/21609_2.png) [@panban-ux](https://discourse.nodered.org/u/panban-ux)\
**Post date:** [16 February 2021 21:05 UTC](https://discourse.nodered.org/t/count-and-short-objects-in-function-node-after-uint16-modification/41120/4 "2021-02-16T21:05:34Z")

</div>

> [@Steve-Mcl](#):
>
> Why not send something extensible and easy to convert like JSON?

Already in my python code i counted the entries then i converted them to a numpy array and then bytes object to TTN. My air medium is Lora Steve so i only have the luxury to send these numbers and not a JSON file.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [16 February 2021 21:08 UTC](https://discourse.nodered.org/t/count-and-short-objects-in-function-node-after-uint16-modification/41120/5 "2021-02-16T21:08:36Z")

</div>

> [@panban-ux](#):
>
> My air medium is Lora Steve so i only have the luxury to send these numbers and not a JSON file.

ah my apologies.

---

<div class="post-metadata">

**Author:** ![panban-ux](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/panban-ux/32/21609_2.png) [@panban-ux](https://discourse.nodered.org/u/panban-ux)\
**Post date:** [16 February 2021 21:10 UTC](https://discourse.nodered.org/t/count-and-short-objects-in-function-node-after-uint16-modification/41120/6 "2021-02-16T21:10:26Z")

</div>

Thanks anyway to both, you already had helped me in the original post and thanks to @E1cid for this current one

---

<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 March 2021 21:10 UTC](https://discourse.nodered.org/t/count-and-short-objects-in-function-node-after-uint16-modification/41120/7 "2021-03-02T21:10:28Z")

</div>

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