Loop for stops after 99 flow

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

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

[{"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"]]}]

All you have posted is a function node

image

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.

[{"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

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.

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.

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 function to sum your value.


this might help you get started

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.

[{"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":[]}]

ok thanks, that's what i wanted

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

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.

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

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

thank you for the info, good evening

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

[{"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":[]}]

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

2 Likes

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