# Loop for stops after 99 flow

**URL:** https://discourse.nodered.org/t/loop-for-stops-after-99-flow/36093
**Category:** General
**Created:** [17 November 2020 20:21 UTC](https://discourse.nodered.org/t/loop-for-stops-after-99-flow/36093 "2020-11-17T20:21:11Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![fabien](https://avatars.discourse-cdn.com/v4/letter/f/85e7bf/32.png) [@fabien](https://discourse.nodered.org/u/fabien)
#### Post date: [17 November 2020 20:21 UTC](https://discourse.nodered.org/t/loop-for-stops-after-99-flow/36093/1 "2020-11-17T20:21:12Z")

</div>

Hello,

I simulated a for loop, as shown in the forum.  
But the loop ends after 99 flow:  
[{"id":"bef94100.efd14","type":"function","z":"ced45fb2.1ebde","name":"","func":"var C = context.get('C')||0;\nif(msg.payload && C\<150){\n context.set('C',C+=1);\n msg.C= C;\n return [msg,null]; \n }\nif(msg.payload && C==150){\n context.set('C',0);\n msg.T= 'OK';\n return [null,msg];\n} \n","outputs":2,"noerr":0,"x":605,"y":297,"wires":[["75315de1.7f1ab4"],["9280ee06.af878"]]}]

thank you in advance for your help

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [17 November 2020 20:23 UTC](https://discourse.nodered.org/t/loop-for-stops-after-99-flow/36093/2 "2020-11-17T20:23:10Z")

</div>

Code needs to be wrapped in code tags to make it useful. In this case, it would probably be more useful to just share the javascript

---

<div class="post-metadata">

### Author: ![fabien](https://avatars.discourse-cdn.com/v4/letter/f/85e7bf/32.png) [@fabien](https://discourse.nodered.org/u/fabien)
#### Post date: [17 November 2020 20:34 UTC](https://discourse.nodered.org/t/loop-for-stops-after-99-flow/36093/3 "2020-11-17T20:34:24Z")

</div>

```auto
[{"id":"bef94100.efd14","type":"function","z":"ced45fb2.1ebde","name":"","func":"var C = context.get('C')||0;\nif(msg.payload && C<150){\n context.set('C',C+=1);\n msg.C= C;\n return [msg,null]; \n }\nif(msg.payload && C==150){\n context.set('C',0);\n msg.T= 'OK';\n return [null,msg];\n} \n","outputs":2,"noerr":0,"x":605,"y":297,"wires":[["75315de1.7f1ab4"],["9280ee06.af878"]]}]

```

---

<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: [17 November 2020 20:39 UTC](https://discourse.nodered.org/t/loop-for-stops-after-99-flow/36093/4 "2020-11-17T20:39:34Z")

</div>

All you have posted is a function node

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/0/30316da399dc4ef0d2aaf6218054d65f2dea9209.png)

I am guessing you need to post more in order for people to help you out.  
You need to select the relevant nodes before exporting.

---

<div class="post-metadata">

### Author: ![fabien](https://avatars.discourse-cdn.com/v4/letter/f/85e7bf/32.png) [@fabien](https://discourse.nodered.org/u/fabien)
#### Post date: [17 November 2020 20:44 UTC](https://discourse.nodered.org/t/loop-for-stops-after-99-flow/36093/5 "2020-11-17T20:44:04Z")

</div>

```auto
[{"id":"ced45fb2.1ebde","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"4b9f66f0.6153f8","type":"debug","z":"ced45fb2.1ebde","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":836,"y":193,"wires":[]},{"id":"75315de1.7f1ab4","type":"function","z":"ced45fb2.1ebde","name":"","func":"if(msg.A ){\n msg.payload= 1;\n return msg; \n}\n","outputs":1,"noerr":0,"x":611,"y":194,"wires":[["bef94100.efd14","4b9f66f0.6153f8"]]},{"id":"bef94100.efd14","type":"function","z":"ced45fb2.1ebde","name":"","func":"var C = context.get('C')||0;\nif(msg.payload && C<150){\n context.set('C',C+=1);\n msg.C= C;\n return [msg,null]; \n }\nif(msg.payload && C==150){\n context.set('C',0);\n msg.T= 'OK';\n return [null,msg];\n} \n","outputs":2,"noerr":0,"x":605,"y":297,"wires":[["75315de1.7f1ab4"],["9280ee06.af878"]]},{"id":"930558b2.073dd8","type":"inject","z":"ced45fb2.1ebde","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":291,"y":183,"wires":[["2a4fa5f.af4565a"]]},{"id":"2a4fa5f.af4565a","type":"change","z":"ced45fb2.1ebde","name":"","rules":[{"t":"set","p":"A","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":451,"y":193,"wires":[["75315de1.7f1ab4"]]},{"id":"9280ee06.af878","type":"debug","z":"ced45fb2.1ebde","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":851,"y":323,"wires":[]}]

```

oups

---

<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: [17 November 2020 20:47 UTC](https://discourse.nodered.org/t/loop-for-stops-after-99-flow/36093/6 "2020-11-17T20:47:51Z")

</div>

Hmm, most strange arrangement and use of msg variables you have there?!?!?.

Perhaps you could explain what the goal is?

I very strongly advise against creating loops in node-red & would like to help you get to a much better solution.

---

<div class="post-metadata">

### Author: ![fabien](https://avatars.discourse-cdn.com/v4/letter/f/85e7bf/32.png) [@fabien](https://discourse.nodered.org/u/fabien)
#### Post date: [17 November 2020 21:08 UTC](https://discourse.nodered.org/t/loop-for-stops-after-99-flow/36093/7 "2020-11-17T21:08:25Z")

</div>

Ok, thank you for dwelling on my problem.  
my goal is to retrieve data from a csv file;  
I must take the values ​​of the second column and deduce the sum of all these values.

---

<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: [17 November 2020 21:14 UTC](https://discourse.nodered.org/t/loop-for-stops-after-99-flow/36093/8 "2020-11-17T21:14:24Z")

</div>

ok, so dont loop. one mistake and you will cause an infinite loop that will eventually exhaust stack space and crash node-red.

Use `file in` node set to read line by line - this will generate a msg for each row in the file.

Then connect the file in to a CSV node to turn your CSV strings into usable values.

you can then feed that into a function and sum the values.

* * *

alternatively, you could read the file in one go (assuming it isnt too large for memory), push it through the CSV node (to generate an array) then use a function node and the [reduce](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce) function to sum your value.

* * *

[this](https://cookbook.nodered.org/basic/parse-csv) might help you get started

---

<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: [17 November 2020 21:25 UTC](https://discourse.nodered.org/t/loop-for-stops-after-99-flow/36093/9 "2020-11-17T21:25:37Z")

</div>

Ok now i have an idea what you need to do, This is possible in a change node with JSONata expression.  
I do not know your csv format, so my example may not be the same format.  
The template node is just to create a csv string to feed into csv node.  
A simple JSONata expression sums the values of column 2 `$sum(payload[*].col2)`  
e.g.

```auto
[{"id":"c815e9db.a393f8","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"255","payloadType":"num","x":190,"y":1860,"wires":[["69d31ace.bab5cc"]]},{"id":"69d31ace.bab5cc","type":"template","z":"8d22ae29.7df6d","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"this,1,that\nthere,2,then\nwhy,3,what","output":"str","x":330,"y":1860,"wires":[["80935f54.3dc4c"]]},{"id":"80935f54.3dc4c","type":"csv","z":"8d22ae29.7df6d","name":"","sep":",","hdrin":"","hdrout":"none","multi":"mult","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":470,"y":1860,"wires":[["3be8855b.56d692"]]},{"id":"3be8855b.56d692","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$sum(payload[*].col2)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":1860,"wires":[["6869ab35.391d14"]]},{"id":"6869ab35.391d14","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":850,"y":1860,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![fabien](https://avatars.discourse-cdn.com/v4/letter/f/85e7bf/32.png) [@fabien](https://discourse.nodered.org/u/fabien)
#### Post date: [17 November 2020 21:44 UTC](https://discourse.nodered.org/t/loop-for-stops-after-99-flow/36093/10 "2020-11-17T21:44:51Z")

</div>

ok thanks, that's what i wanted

---

<div class="post-metadata">

### Author: ![fabien](https://avatars.discourse-cdn.com/v4/letter/f/85e7bf/32.png) [@fabien](https://discourse.nodered.org/u/fabien)
#### Post date: [17 November 2020 21:52 UTC](https://discourse.nodered.org/t/loop-for-stops-after-99-flow/36093/11 "2020-11-17T21:52:11Z")

</div>

I will take the solution from E1cid, it's easier to use

May the "reduce" function works well, when I have more time I will look into it

thanks again

---

<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: [17 November 2020 21:57 UTC](https://discourse.nodered.org/t/loop-for-stops-after-99-flow/36093/12 "2020-11-17T21:57:26Z")

</div>

not a problem - glad it works for you

I personally dont do JSONata (i just find JS easier and there are shed loads of JS programmers to lean on when I get stuck and i can step debug JS functions in VSCode debugger if i am really stuck)

I will add for completeness, if your file is very large, JSONata can be pretty slow (like 1000x slower than equivalent JS in some cases I have encountered) - but likely not an issue for you.

---

<div class="post-metadata">

### Author: ![fabien](https://avatars.discourse-cdn.com/v4/letter/f/85e7bf/32.png) [@fabien](https://discourse.nodered.org/u/fabien)
#### Post date: [17 November 2020 22:04 UTC](https://discourse.nodered.org/t/loop-for-stops-after-99-flow/36093/13 "2020-11-17T22:04:34Z")

</div>

just a little problem. if the first line is not a number (header), it will pose a problem for the sum.

---

<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: [17 November 2020 22:05 UTC](https://discourse.nodered.org/t/loop-for-stops-after-99-flow/36093/14 "2020-11-17T22:05:53Z")

</div>

you can tell the csv node to ignore the 1st line, or that the first line contains column names.

---

<div class="post-metadata">

### Author: ![fabien](https://avatars.discourse-cdn.com/v4/letter/f/85e7bf/32.png) [@fabien](https://discourse.nodered.org/u/fabien)
#### Post date: [17 November 2020 22:13 UTC](https://discourse.nodered.org/t/loop-for-stops-after-99-flow/36093/15 "2020-11-17T22:13:32Z")

</div>

thank you for the info, good evening

---

<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: [17 November 2020 22:18 UTC](https://discourse.nodered.org/t/loop-for-stops-after-99-flow/36093/16 "2020-11-17T22:18:33Z")

</div>

for you (or future readers) this is how you do the array `reduce`...

```auto
[{"id":"c815e9db.a393f8","type":"inject","z":"a9fbaedc.8f9c1","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"255","payloadType":"num","x":170,"y":460,"wires":[["69d31ace.bab5cc"]]},{"id":"69d31ace.bab5cc","type":"template","z":"a9fbaedc.8f9c1","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"col1,col2,col3\nthis,1,that\nthere,2,then\nwhy,3,what","output":"str","x":310,"y":460,"wires":[["80935f54.3dc4c"]]},{"id":"80935f54.3dc4c","type":"csv","z":"a9fbaedc.8f9c1","name":"","sep":",","hdrin":true,"hdrout":"none","multi":"mult","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":450,"y":460,"wires":[["3be8855b.56d692","38d0540.06eefac"]]},{"id":"6869ab35.391d14","type":"debug","z":"a9fbaedc.8f9c1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":830,"y":460,"wires":[]},{"id":"3be8855b.56d692","type":"change","z":"a9fbaedc.8f9c1","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$sum(payload[*].col2)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":460,"wires":[["6869ab35.391d14"]]},{"id":"38d0540.06eefac","type":"function","z":"a9fbaedc.8f9c1","name":"reduce","func":"msg.payload = msg.payload\n .map(item => item.col2)\n .reduce((accumulator, currentValue) => accumulator + currentValue)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":590,"y":520,"wires":[["2967ff0.6194c02"]]},{"id":"2967ff0.6194c02","type":"debug","z":"a9fbaedc.8f9c1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":830,"y":520,"wires":[]}]

```

---

<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: [17 November 2020 22:51 UTC](https://discourse.nodered.org/t/loop-for-stops-after-99-flow/36093/17 "2020-11-17T22:51:29Z")

</div>

For information  
500 line csv - JSONata 26ms Javascript 4ms  
10000 line csv -JSONata 400ms Javascript 10ms

---

<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: [16 January 2021 22:51 UTC](https://discourse.nodered.org/t/loop-for-stops-after-99-flow/36093/18 "2021-01-16T22:51:40Z")

</div>

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