Adding a delay in a for loop in the function node

Hi,

So, I've been working on a certain project and this forum has been a great help in getting it done. Thank you.

I have recently been working with a Json object and running a for loop to get some values from it. I tried to use a delay node to send one message at a time but I keep loosing values the way I set it up. even with rate limit option.

var ts = msg.payload.VeriBalancePlatformPOSLog.Transaction[0].TenderControlTransaction[0].TillSettle[0].TenderSummary;
var len=ts.length;
for (var z=0;z<len;z++)
{
var tdr = ts[z].Ending[0].$.TenderType;
var amt = ts[z].Ending[0].Amount[0]._;
var tdr = Number(tdr);
var hr = flow.get("hourf");
var mn = flow.get("minf");
var sec = flow.get("secf");
var padday = flow.get("pdf");
var yearr = flow.get("yearf");
var seqn = Number(flow.get("seqf"));
var tid = flow.get("tidf");
var all = { "length" : len, "tender" : tdr, "amount" : amt, "year" : yearr, "pd" : padday, "hour" : hr, "min" : mn, "second" : sec, "seq" : seqn, "tids" : tid};
//var all = { "length" : len, "tender" : tdr, "amount" : amt};
msg.payload = all;
node.send(msg);
(I'm trying to add a 2 second delay here after each value is returned)
}

Is this a possibility to add a delay in the function itself?

Thank you in advance.

The delay node won't lose messages if correctly configured. Show us what you tried.

Thank you for the reply Colin. here is the flow,

[{"id":"17036a42.e69016","type":"tab","label":"help","disabled":false,"info":""},{"id":"3253279d.e351f8","type":"debug","z":"17036a42.e69016","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1520,"y":275,"wires":[]},{"id":"21b24bf1.561124","type":"inject","z":"17036a42.e69016","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":485,"y":275,"wires":[["aae80713.948828"]]},{"id":"465c3aac.346234","type":"xml","z":"17036a42.e69016","name":"BI","property":"payload","attr":"","chr":"","x":900,"y":275,"wires":[["652b9406.2681dc"]]},{"id":"652b9406.2681dc","type":"function","z":"17036a42.e69016","name":"values","func":"var ts = msg.payload.VeriBalancePlatformPOSLog.Transaction[0].TenderControlTransaction[0].TillSettle[0].TenderSummary;\nvar len=ts.length;\nfor (var z=0;z<len;z++)\n{\nvar tdr = ts[z].Ending[0].$.TenderType;\nvar amt = ts[z].Ending[0].Amount[0]._;\nvar tdr = Number(tdr) - 200;\nvar hr = flow.get(\"hourf\");\nvar mn = flow.get(\"minf\");\nvar sec = flow.get(\"secf\");\nvar padday = flow.get(\"pdf\");\nvar yearr = flow.get(\"yearf\");\nvar seqn = flow.get(\"seqf\");\nvar tid = flow.get(\"tidf\");\nvar all = { \"length\" : len, \"tender\" : tdr, \"amount\" : amt, \"year\" : yearr, \"pd\" : padday, \"hour\" : hr, \"min\" : mn, \"second\" : sec, \"seq\" : seqn, \"tids\" : tid};\n//var all = { \"length\" : len, \"tender\" : tdr, \"amount\" : amt};\nmsg.payload = all;\nnode.send(msg);\n}","outputs":1,"noerr":0,"x":1100,"y":275,"wires":[["3d29ede6.970882"]]},{"id":"aae80713.948828","type":"file in","z":"17036a42.e69016","name":"incoming","filename":"C:\\\\storeman\\\\paymentscollected\\\\incometest.txt","format":"utf8","chunk":false,"sendError":false,"x":685,"y":275,"wires":[["465c3aac.346234"]]},{"id":"3d29ede6.970882","type":"delay","z":"17036a42.e69016","name":"","pauseType":"delay","timeout":"3","timeoutUnits":"seconds","rate":"1","nbRateUnits":"3","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1285,"y":275,"wires":[["3253279d.e351f8"]]}]

and this the file,

<?xml version="1.0" encoding="utf-8"?>
<VeriBalancePlatformPOSLog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.nrf-arts.org/IXRetail/namespace/">
  <Transaction MajorVersion="6" MinorVersion="0" FixVersion="1">
    <BusinessUnit>
      <UnitID>1234</UnitID>
    </BusinessUnit>
    <WorkstationID>770030</WorkstationID>
    <BusinessDayDate>2019-01-28</BusinessDayDate>
    <TenderControlTransaction>
      <TillSettle>
        <TenderSummary>
          <Ending TenderType="1301">
            <Amount Currency="">-726.19</Amount>
            <Count>1</Count>
          </Ending>
        </TenderSummary>
        <TenderSummary>
          <Ending TenderType="1330">
            <Amount Currency="">-2362.45</Amount>
            <Count>1</Count>
          </Ending>
        </TenderSummary>
        <TenderSummary>
          <Ending TenderType="1322">
            <Amount Currency="">-7.50</Amount>
            <Count>1</Count>
          </Ending>
        </TenderSummary>
        <TenderSummary>
          <Ending TenderType="1310">
            <Amount Currency="">-292.47</Amount>
            <Count>1</Count>
          </Ending>
        </TenderSummary>
        <TenderSummary>
          <Ending TenderType="1320">
            <Amount Currency="">-15.40</Amount>
            <Count>1</Count>
          </Ending>
        </TenderSummary>
        <TenderSummary>
          <Ending TenderType="1303">
            <Amount Currency="">-350.55</Amount>
            <Count>1</Count>
          </Ending>
        </TenderSummary>
      </TillSettle>
    </TenderControlTransaction>
    <Fingerprint>3.27.posrequeueidx-227.currdt-2019-01-28T10:48:18.clidx-70</Fingerprint>
  </Transaction>
</VeriBalancePlatformPOSLog>

Thank you.

The delay node needs to be in rate limit mode, not delay mode, and make sure that Drop Intermediate Messages is not ticked. In delay mode it will delay them all the same time, so there will be no delay between the messages, just at the start.
Make that change and then run it and show us how you know there are some messages missing in the output. Put a node.warn statement in the function to display the length of the array for example.
In fact if you run it first without the delay node then with it and see what the difference is.

I probably did not make the change before I sent the flow to you, but I did use the rate limit and made sure the the drop intermediate is not checked and attaching the image of my output.

1: is the output without the delay node
2: is the out with delay set to rate limit and no drop intermediatenode

Something horrible is going wrong there, it is not dropping messages, it has repeated the last one multiple times.
Can you tell us what operating system you are running on and show us the log you get when you start node-red? Dependent on how you installed you may be able to do

node-red-stop
node-red-start

That will enable us to see which versions of node-red etc are being used. The log is the one that starts with a Welcome to node red message.

I noticed that, I probably should have worded it better.

I am on a windows 10 machine, and this the log at welcome,

20 Feb 10:29:28 - [info]

Welcome to Node-RED
===================

20 Feb 10:29:28 - [info] Node-RED version: v0.19.5
20 Feb 10:29:28 - [info] Node.js  version: v10.14.1
20 Feb 10:29:28 - [info] Windows_NT 10.0.17134 x64 LE
20 Feb 10:29:29 - [info] Loading palette nodes
Missing ENV var CONFIG_PATH
20 Feb 10:29:31 - [info] Dashboard version 2.13.0 started at /ui
20 Feb 10:29:31 - [warn] rpi-gpio : Raspberry Pi specific node set inactive
20 Feb 10:29:31 - [warn] ------------------------------------------------------
20 Feb 10:29:31 - [warn] [node-red/tail] Not currently supported on Windows.
20 Feb 10:29:31 - [warn] [node-red-contrib-mssql/mssql] Type already registered
20 Feb 10:29:31 - [warn] [node-red-contrib-mssql-use-utc/mssql] Type already registered
20 Feb 10:29:31 - [warn] [node-red-contrib-viseo-file/file-xlsx] Error: Cannot find module 'request-promise' (line:2)
20 Feb 10:29:31 - [warn] ------------------------------------------------------
20 Feb 10:29:31 - [info] Settings file  : \Users\*******\.node-red\settings.js
20 Feb 10:29:31 - [info] Context store  : 'default' [module=memory]
20 Feb 10:29:31 - [info] User directory : \Users\*******\.node-red
20 Feb 10:29:31 - [warn] Projects disabled : editorTheme.projects.enabled=false
20 Feb 10:29:31 - [info] Flows file     : \Users\*******\.node-red\flows_Kevin.json
20 Feb 10:29:31 - [info] Server now running at http://127.0.0.1:1880/
20 Feb 10:29:31 - [warn]

---------------------------------------------------------------------
Your flow credentials file is encrypted using a system-generated key.

If the system-generated key is lost for any reason, your credentials
file will not be recoverable, you will have to delete it and re-enter
your credentials.

You should set your own key using the 'credentialSecret' option in
your settings file. Node-RED will then re-encrypt your credentials
file using your chosen key the next time you deploy a change.
---------------------------------------------------------------------

20 Feb 10:29:31 - [info] Starting flows
20 Feb 10:29:31 - [info] Started flows

I would start by sorting the mssql and viseo issues. It seems you have multiple mssql nodes installed. Uninstall the one(s) you are not using. I think the viseo node has not installed properly, if you are not using it then uninstall it, if you are using it then try installing from the command line using npm and see what it says.
A question, are you sure that what you showed with the debug output is consistent? It might be worth putting debug nodes showing the messages going into the delay and out of it. Give them names so you can see which message is from which node. Then you will be able to see both sets of data in one go.

You are using node.send inside a loop. This has to be used with caution.

Please try the following code inside your function node, just for testing purposes. You will see it works fine You can remove the delay node from your flow.

var ts = msg.payload.VeriBalancePlatformPOSLog.Transaction[0].TenderControlTransaction[0].TillSettle[0].TenderSummary;
var len=ts.length;


for (var z=0;z<len;z++)
{
var tdr = ts[z].Ending[0].$.TenderType;
var amt = ts[z].Ending[0].Amount[0]._;
tdr = Number(tdr) - 200;

var all = { "length" : len, "tender" : tdr, "amount" : amt};

node.send({"payload" : { "length" : len, "tender" : tdr, "amount" : amt}});
}

Thank you. yes, the output is consistent. I tried using two debug nodes and the results were consistent. repeats values after the first result.

Hi @Andrei, Thank you so much for replying. Also yes, the code seems to work. Thank you for your input, I will try to use this and check if I am able to accomplish what I am trying to. Will keep you posted.

You could eventually process the data without using a function node. Normally there are many different ways to achieve the same result in Node-RED.

Flow (can be simplified):

[{"id":"99ab38fa.0ef288","type":"tab","label":"help","disabled":false,"info":""},{"id":"7d2d2e2c.0cf6f","type":"debug","z":"99ab38fa.0ef288","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":750,"y":260,"wires":[]},{"id":"11b6393c.9f0d27","type":"inject","z":"99ab38fa.0ef288","name":"Go","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":160,"wires":[["76b62a28.5281c4"]]},{"id":"ba2383ad.b7226","type":"xml","z":"99ab38fa.0ef288","name":"BI","property":"payload","attr":"","chr":"","x":430,"y":160,"wires":[["8323761c.9e0548"]]},{"id":"76b62a28.5281c4","type":"file in","z":"99ab38fa.0ef288","name":"Read XML","filename":"C:\\Users\\OCM\\.node-red\\static\\nrfiles\\inc.xml","format":"utf8","chunk":false,"sendError":false,"x":290,"y":160,"wires":[["ba2383ad.b7226"]]},{"id":"8323761c.9e0548","type":"change","z":"99ab38fa.0ef288","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"msg.payload.VeriBalancePlatformPOSLog.Transaction[0].TenderControlTransaction[0].TillSettle[0].TenderSummary","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":200,"y":260,"wires":[["596218c2.002038"]]},{"id":"596218c2.002038","type":"split","z":"99ab38fa.0ef288","name":"Split1","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"topic","x":370,"y":260,"wires":[["978bd5dc.bf6608"]]},{"id":"978bd5dc.bf6608","type":"change","z":"99ab38fa.0ef288","name":"","rules":[{"t":"set","p":"payload.tdr","pt":"msg","to":"payload.Ending[0].$.TenderType","tot":"msg"},{"t":"set","p":"payload.amt","pt":"msg","to":"payload.Ending[0].Amount[0]._","tot":"msg"},{"t":"set","p":"payload.len","pt":"msg","to":"parts.count","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"{\"len\": parts.count, \"tender\":payload.Ending[0].\"$\".TenderType, \"amount\" : payload.Ending[0].Amount[0]._ }","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":260,"wires":[["7d2d2e2c.0cf6f"]]}]
1 Like