Creating threads in mp-function

Hi,

I required to create threads using mp-function node.I can't find any documentation about that. What I need is create multiple dealys according to values in a array.Is there any other methods to achieve that.

Thanks.

Not used the mp-function node, I presume it's uses Javascript like the function node. If so you Can use setTimeout method to create delays in the JS and use node.send to output the messages. If you need more input please explain what you are trying to achieve, with examples of input and out you require.

Hi, I actually want create custom delays according to values in a array. Array contains multiple messages with multiple delays. So I guess this task required threading.

Note, node-red-contrib-mp-function was last published 4 years 10 months ago (as of the date of this post) and as you have discovered, there is not much documentation.

Not saying it won't work, but if anything goes wrong, it looks like you are on your own

Yes still possible in javascript with timeout() and node.send(), Or use split node and change node, you can probably do this without using a function node.

@E1cid Is it possible for multiple and various intervals that stored in a array.

Anything is possible.

This is all a bit vague.

Maybe if you post a sample array and explain what you wish to happen we could offer you suitable advice.

Hi, @Steve-Mcl
Imagine there are values like following example stored in a database.

{id:1,alert:"humidity",interval:"10"}
{Id:2, alert:"level",interval:"10"}
{id:3, alert:"CO2",interval:"20"}

Inject node will request once a minute from database. Above values will retrieved from the database. When the values received to the node-server,I need to set intervals according to those values. And send messeges according to that interval.Also Inject node will keep requesting the values.

What would the payload look like when these "intervals" occur?

Also, when you say interval - do you mean "every 10 seconds" or "once in 10 sec time"?

payload will be a numeric value from another node. I just want to trigger it according to the interval. I need trigger it according once in a whatever value. There will be more than 3 datasets.If it is only for few values I could be able to simply filter it out from the id and pass it in to settimeout function.

With a simple split and delay.

[{"id":"7af3efd.ed4cc1","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"id\":1,\"alert\":\"humidity\",\"interval\":\"10\"},{\"Id\":2,\"alert\":\"level\",\"interval\":\"10\"},{\"id\":3,\"alert\":\"CO2\",\"interval\":\"20\"}]","payloadType":"json","x":160,"y":1900,"wires":[["365e2541.1a7b6a"]]},{"id":"365e2541.1a7b6a","type":"split","z":"bf9e1e33.030598","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":290,"y":1900,"wires":[["49672b41.3fa4e4"]]},{"id":"49672b41.3fa4e4","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"delay","pt":"msg","to":"$number($$.payload.interval)*1000","tot":"jsonata"},{"t":"set","p":"topic","pt":"msg","to":"payload.alert","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":1900,"wires":[["bf80a3d9.252c08"]]},{"id":"bf80a3d9.252c08","type":"delay","z":"bf9e1e33.030598","name":"","pauseType":"delayv","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":640,"y":1900,"wires":[["5f3d4c9f.1b6314"]]},{"id":"5f3d4c9f.1b6314","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":670,"y":1980,"wires":[]}]

or function

[{"id":"7c9f48df.7dfa58","type":"function","z":"bf9e1e33.030598","name":"","func":"msg.payload.forEach(obj => {\n    setTimeout(() => {\n  node.send({payload:obj})\n}, Number(obj.interval)*1000)\n})\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":330,"y":1900,"wires":[["5f3d4c9f.1b6314"]]},{"id":"7af3efd.ed4cc1","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"id\":1,\"alert\":\"humidity\",\"interval\":\"10\"},{\"Id\":2,\"alert\":\"level\",\"interval\":\"10\"},{\"id\":3,\"alert\":\"CO2\",\"interval\":\"20\"}]","payloadType":"json","x":160,"y":1900,"wires":[["7c9f48df.7dfa58"]]},{"id":"5f3d4c9f.1b6314","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":670,"y":1980,"wires":[]}]
1 Like

@E1cid That function is working nicely I think I have to create a connection with database as well otherwise it will ran 10 seconds agian without completing previous interval.