# Whats wrong with my " for loop" and array selection

**URL:** <https://discourse.nodered.org/t/whats-wrong-with-my-for-loop-and-array-selection/8570>\
**Category:** General\
**Created:** [3 March 2019 22:27 UTC](https://discourse.nodered.org/t/whats-wrong-with-my-for-loop-and-array-selection/8570 "2019-03-03T22:27:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![BastelKlaus](https://avatars.discourse-cdn.com/v4/letter/b/a587f6/32.png) [@BastelKlaus](https://discourse.nodered.org/u/BastelKlaus)\
**Post date:** [3 March 2019 22:27 UTC](https://discourse.nodered.org/t/whats-wrong-with-my-for-loop-and-array-selection/8570/1 "2019-03-03T22:27:52Z")

</div>

Hi i need to send several requests to my heater, end i will sort his answer to the right output.  
like:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/1/16c6b1835c95e3ba9d382d8115212ea97c654cbf.png)

The heater can handel max 4-5 requests in 1 Buffer, so i have to split the requests.

In my function i try to search with a "for each loop" the Hex byte, that for example "0x0d" Boiler.  
Then i try to read the next byte "readUInt16BE", witch are the temprature or else.  
This message shoud go to the output i whant.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/c/c2db4b43ed7c3a2e7c5f8505d2b55f09d34030be.png)

> **Summary**
>
> ```auto
> var buffer = msg.payload;
> msg = [];
> for (var i = 0; i < buffer.length; i++) {
> if (buffer[i] == 0x0d); //boiler 
> {
> i++;
> msg [0] = {payload : buffer.readUInt16BE(8)/10};
> continue;//next loop
> }
> 
> if (buffer[i] == 0x0f); //Abgas
> {
> i++;
> msg [1] = { payload: buffer.readUInt16BE(i) / 10 };
> continue;//next loop
> }
> 
> //if (buf = ....); //Kessel ........
> //{...}
> if (buffer[i] == 0x7d);
> {
> msg [12] = { payload: "Timestamp" }
> }
> if (buffer[i] != 0x7d);
> {
> continue;
> }
> //else; //anything else
> //{
> //continue;}
> }
> node.warn ()
> return msg;
> 
> ```

Debug logs only 1 Value, but he can find more.  
My inject Buffer was with all request i would do (not the answer of the heater).

I dont think this is the best way to handel it, but once of them i tried 🤣 🤣 🤣 you can see a part in the background.

In this case i think it can`t be 🤔 or I cant 🤔 select the byte witch is to read out of the array with [i], and if i try "else"in the loop i got error.

If anyone think this is the wrong way, or even think i shoud try/do something else please tell me.

---

<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 March 2019 08:06 UTC](https://discourse.nodered.org/t/whats-wrong-with-my-for-loop-and-array-selection/8570/2 "2019-03-04T08:06:58Z")

</div>

Generate a simple test flow with an inject (or inject + function) to pass in a sample buffer containing your data and then your function node and a debug node to show the output. Then export that flow so we can understand better what you are doing.

A question, are the items in the buffer always in the same place or can there be some missing or in the wrong order? If they are always in the same place then it would be easier to extract them by position.

---

<div class="post-metadata">

**Author:** ![BastelKlaus](https://avatars.discourse-cdn.com/v4/letter/b/a587f6/32.png) [@BastelKlaus](https://discourse.nodered.org/u/BastelKlaus)\
**Post date:** [4 March 2019 09:42 UTC](https://discourse.nodered.org/t/whats-wrong-with-my-for-loop-and-array-selection/8570/3 "2019-03-04T09:42:28Z")

</div>

hi so i try to do ...

```auto
[
    {
        "id": "6b7f0f13.02971",
        "type": "tab",
        "label": "Flow 5",
        "disabled": false,
        "info": ""
    },
    {
        "id": "58efb67f.239ed8",
        "type": "inject",
        "z": "6b7f0f13.02971",
        "name": "Werte Abfrage 1",
        "topic": "",
        "payload": "[\"0x7B\",\"0x4D\",\"0x43\",\"0x0D\",\"0xF2\",\"0x0A\",\"0x08\",\"0x00\",\"0x08\",\"0x02\",\"0x30\",\"0x08\",\"0x00\",\"0x0F\",\"0x02\",\"0x31\",\"0x08\",\"0x00\",\"0x0D\",\"0x02\",\"0x32\",\"0x08\",\"0x00\",\"0xA4\",\"0x02\",\"0x33\",\"0x7d\"]",
        "payloadType": "bin",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 220,
        "y": 200,
        "wires": [
            [
                "9d2bda80.388f38"
            ]
        ]
    },
    {
        "id": "9d2bda80.388f38",
        "type": "function",
        "z": "6b7f0f13.02971",
        "name": "",
        "func": "var buffer = msg.payload;\nmsg = [];\nfor (var i = 0; i < buffer.length; i++) {\n if (buffer[i] == 0x0d); //boiler \n {\n i++;\n msg [0] = {payload : buffer.readUInt16BE(8)/10};\n continue;//next loop\n }\n\n if (buffer[i] == 0x0f); //Abgas\n {\n i++;\n msg [1] = { payload: buffer.readUInt16BE(i) / 10 };\n continue;//next loop\n }\n\n //if (buf = ....); //Kessel ........\n //{...}\n if (buffer[i] == 0x7d);\n {\n msg [12] = { payload: \"Timestamp\" }\n }\n if (buffer[i] != 0x7d);\n {\n continue;\n }\n //else; //anything else\n //{\n //continue;}\n}\nnode.warn ()\nreturn msg;",
        "outputs": 12,
        "noerr": 0,
        "x": 530,
        "y": 200,
        "wires": [
            [
                "ffc25e74.0cdf"
            ],
            [
                "5b3fef8a.96d9"
            ],
            [
                "d1f50e1.5af21f"
            ],
            [],
            [],
            [],
            [],
            [],
            [],
            [],
            [],
            []
        ]
    },
    {
        "id": "ffc25e74.0cdf",
        "type": "debug",
        "z": "6b7f0f13.02971",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "x": 790,
        "y": 120,
        "wires": []
    },
    {
        "id": "5b3fef8a.96d9",
        "type": "debug",
        "z": "6b7f0f13.02971",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "x": 790,
        "y": 160,
        "wires": []
    },
    {
        "id": "d1f50e1.5af21f",
        "type": "debug",
        "z": "6b7f0f13.02971",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "x": 790,
        "y": 200,
        "wires": []
    }
]

```

i hope this works, this is the first one i try to post an Flow.

> [@Colin](#):
>
> A question, are the items in the buffer always in the same place or can there be some missing or in the wrong order? If they are always in the same place then it would be easier to extract them by position.

--\> In my test i see, the Heater returns (mirrored request and answer) in various position, so i have to search for them...  
--\> it looks like i can only request 4 at a time, so i will crone the requests later. This means he will answer only a few of possible request at ones.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/d/d2f6e816c1a2944ec2231e9abc8645610157842f.png)  
This is an example answer, so he mirror my request and put the answer behind.

In the test folw i generate a buffer witch 4 requests and a example answer  
mirror Request"0x00","0x0F" answer "0x02","0x31"

---

<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 March 2019 10:32 UTC](https://discourse.nodered.org/t/whats-wrong-with-my-for-loop-and-array-selection/8570/4 "2019-03-04T10:32:21Z")

</div>

The main problem is one that is very difficult to see. Notice that the editor does not correctly indent the lines after your if statements? There is a reason for that. Hint, semi colon terminates a statement.

Also you have `readUInt16BE(8)` at one point, it should be `i`

The way to find problems like this is to insert node.warn statements through the code then you will see where it is going.  
You might like to look at the javascript Switch statement, it will make the code a little simpler.

---

<div class="post-metadata">

**Author:** ![BastelKlaus](https://avatars.discourse-cdn.com/v4/letter/b/a587f6/32.png) [@BastelKlaus](https://discourse.nodered.org/u/BastelKlaus)\
**Post date:** [4 March 2019 12:01 UTC](https://discourse.nodered.org/t/whats-wrong-with-my-for-loop-and-array-selection/8570/5 "2019-03-04T12:01:43Z")

</div>

thx @Colin

it was the semi colon. 💩 ; 😅  
I put an node warn at the end, but i shoud read more about warn and how to use.

Now i get more messages thx!

---

<div class="post-metadata">

**Author:** ![BastelKlaus](https://avatars.discourse-cdn.com/v4/letter/b/a587f6/32.png) [@BastelKlaus](https://discourse.nodered.org/u/BastelKlaus)\
**Post date:** [4 March 2019 12:07 UTC](https://discourse.nodered.org/t/whats-wrong-with-my-for-loop-and-array-selection/8570/6 "2019-03-04T12:07:34Z")

</div>

> [@Colin](#):
>
> You might like to look at the javascript Switch statement, it will make the code a little simpler.

how do you mean this, or how can i do this?  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/8/8f46546151569b8a10c8673fd84d03db41cc921e.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 March 2019 12:21 UTC](https://discourse.nodered.org/t/whats-wrong-with-my-for-loop-and-array-selection/8570/7 "2019-03-04T12:21:08Z")

</div>

Not a switch node, the javascript switch statement. You can use it instead of `if`  
[https://www.w3schools.com/js/js\_switch.asp](https://www.w3schools.com/js/js_switch.asp)

---

<div class="post-metadata">

**Author:** ![BastelKlaus](https://avatars.discourse-cdn.com/v4/letter/b/a587f6/32.png) [@BastelKlaus](https://discourse.nodered.org/u/BastelKlaus)\
**Post date:** [4 March 2019 12:40 UTC](https://discourse.nodered.org/t/whats-wrong-with-my-for-loop-and-array-selection/8570/8 "2019-03-04T12:40:09Z")

</div>

ok thx

at first i think there is an hidden box in Info or... wich i didn't found 😀

[https://www.w3schools.com](https://www.w3schools.com) is always an open tab in my browser.
