Need to design aflow that every 30 min bursts X message with a 30ms interval

Hello dear forum users,

I am testing a equipment and need to make a flow that every 30 min burst Xmessages i have already saved on a variable in a 30 ms interval between them.

I can make one that sends messages every 30 ms or other that sends every 60 seconds but a dynamic one i cannot wrap my head around it.

Use a function node that gets triggered by an inject node every 30 minutes. In the function node iterate over your variable (an array?) and use the async node.send() function described here in the part about sending async messages, something like:

var myArray = [1,2,3,4,5];
myArray.forEach(item => node.send({payload:item}));
return;

and then put a delay node in rate limit mode behind it that only lets through a message every 30 ms.

My "variable" maybe not well explained is a json stored in flow.X variable.
I see the logic behind your explanation! I just don´t see how to make it stop after, let´s say 60s, ON->60s->OFF and then only 30min after does the cylce repeats for 24H.

What format does your variable have? Are you sending the same variable again and again?

It´s an Object with 174 variables. But i am sending the same object over and over again.

but you don’t always want to send all of them but be able to stop sending after a certain amount of time? Because sending all 174 in 30 ms intervals will only need like 4 seconds. So do you want to start over if the 60 seconds are not passed and continue sending them again?

My variable is like this

It goes in a whole.so i want to send the whole list every 30 ms.

Simulating a system that is sending only 1 mes a minute and every 30 mins sends a burst off messages that takes X mins.
after the burst it retakes the one message a minute.

Something like this could do it, of course you would have to adapt it to your times and circumstances as I used way shorter periods to test it and its just a basic example flow. Play with it:

[{"id":"6807ce07.123cd","type":"function","z":"35572fe7.e9aef8","name":"burst","func":"var myObj = msg.payload;\nfunction waitSixty () {\n    setTimeout(()=>{\n        flow.set(\"timeout\", false);\n    },1000);\n}\nfunction repeatThirty (variable) {\n    var interval = setInterval(()=>{\n        node.send({payload:variable});\n        var timeout = flow.get(\"timeout\");\n        if (!timeout) { clearInterval(interval) }\n    },30);\n}\nflow.set(\"timeout\",true);\nwaitSixty();\nrepeatThirty(myObj);\nreturn;","outputs":1,"noerr":0,"x":490,"y":600,"wires":[["73f59f20.280878"]]},{"id":"73f59f20.280878","type":"debug","z":"35572fe7.e9aef8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":710,"y":660,"wires":[]},{"id":"dae2c46e.ee50c8","type":"inject","z":"35572fe7.e9aef8","name":"","topic":"","payload":"{\"a\":1,\"b\":2,\"c\":3,\"d\":4,\"e\":5}","payloadType":"json","repeat":"1","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":600,"wires":[["96bdb47c.7e1b1","b7b885b3.0c9c2"]]},{"id":"96bdb47c.7e1b1","type":"delay","z":"35572fe7.e9aef8","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"10","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":320,"y":600,"wires":[["6807ce07.123cd"]]},{"id":"b7b885b3.0c9c2","type":"switch","z":"35572fe7.e9aef8","name":"","property":"timeout","propertyType":"flow","rules":[{"t":"false"}],"checkall":"true","repair":false,"outputs":1,"x":290,"y":660,"wires":[["73f59f20.280878"]]}]

So you have an inject which sends your var every second in this example, the var is only let through if a variable set by the burst node is false. Every ten seconds in this example flow a message is let through the delay node in rate limit mode to trigger the burst function. The burst function has two functions inside. One is setTimeout() that turns the timeout variable to true for 1 second in this case the other is a setInterval() that send the received msg.payload every 30 ms while the timeout variable is true. This is the same variable that is beeing checked in the switch node. so while the burst runs no messages will be passed there. The function node code is:

var myObj = msg.payload;
function waitSixty () {
    setTimeout(()=>{
        flow.set("timeout", false);
    },1000);
}
function repeatThirty (variable) {
    var interval = setInterval(()=>{
        node.send({payload:variable});
        var timeout = flow.get("timeout");
        if (!timeout) { clearInterval(interval) }
    },30);
}
flow.set("timeout",true);
waitSixty();
repeatThirty(myObj);
return;

Maybe you can apply some of the concepts in this example to your problem :blush:
Johannes

2 Likes

It is exactly this!
My understanding off javascript are far from doing this, thank you a lot!

1 Like

You will get used to javascript and its fortunately not that hard. I really recommend you read up on the methods like setTimeout() and setInterval() i used as adding those basics to your knowledge will always be good and its good to understand the code one uses.
I always liked the documentation by mozilla to look up things like this:

1 Like

Thank you!
I will

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