Repeat Mqtt Message 7 Times and Stop

Hi Everyone ,

I am using mqtt to power ON the device and using two STOP TIMER NODES the SECONDTIMER NODE wait for 12 second and send the payload to TRIGGER NODE which send message for every two 2 second ( 6 to 7 times ) and after 26 seconds First timer node send off payload ( Because trigger node has no stop option after 6 or 7 times sending same payload ) . which then reset the TRIGGER NODE to stop sending further .

Please let me know whether this can be done effectively with single node which send 7 message and then stop sending the message . and the same process should happen every 25 MIN

also i need this to happen every 25min to send the ON message 7 times .

[{"id":"b3705d6b.1943b","type":"mqtt out","z":"9ceecf2a.f669a","name":"Move Wing","topic":"Device/switch/move_wing/command","qos":"","retain":"","broker":"ee301c7a.1e202","x":1050,"y":100,"wires":[]},{"id":"b4d6ef6e.c7d358","type":"mqtt in","z":"9ceecf2a.f669a","name":"","topic":"Power On","qos":"2","datatype":"auto","broker":"ee301c7a.1e202","x":80,"y":80,"wires":[["93afd9d6.cb514","d100bdf2.3dbc7"]]},{"id":"de487f50.f358a","type":"trigger","z":"9ceecf2a.f669a","name":"resend it every 2 sec","op1":"ON","op2":"0","op1type":"str","op2type":"str","duration":"-2","extend":false,"overrideDelay":false,"units":"s","reset":"OFF","bytopic":"all","topic":"topic","outputs":1,"x":800,"y":100,"wires":[["b3705d6b.1943b"]]},{"id":"93afd9d6.cb514","type":"stoptimer","z":"9ceecf2a.f669a","duration":"12","units":"Second","payloadtype":"num","payloadval":"ON","name":"","x":490,"y":160,"wires":[[],["de487f50.f358a"]]},{"id":"d100bdf2.3dbc7","type":"stoptimer","z":"9ceecf2a.f669a","duration":"26","units":"Second","payloadtype":"num","payloadval":"OFF","name":"","x":490,"y":40,"wires":[[],["de487f50.f358a"]]},{"id":"ee301c7a.1e202","type":"mqtt-broker","name":"MQTT","broker":"10.0.0.3","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

This could be done with a function node and setTimeout()
e.g.

[{"id":"8c670823.21ac9","type":"function","z":"c74669a0.6a34f8","name":"","func":"for(let i=0;i<7;i++){\n    setTimeout(() => {node.send(msg)}, i*2*1000)\n}\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":280,"y":2160,"wires":[["a570b87f.d22fe8"]]},{"id":"2b89ad16.499f9a","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":" 12345","payloadType":"str","x":90,"y":2160,"wires":[["8c670823.21ac9"]]},{"id":"a570b87f.d22fe8","type":"debug","z":"c74669a0.6a34f8","name":"out 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":2080,"wires":[]}]

[edit]
for 25min repeat I would add a context to stop the function running endlessly
eg. (be carefull with this as you could end up with an endless loop)

[{"id":"2b89ad16.499f9a","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":" 12345","payloadType":"str","x":90,"y":2160,"wires":[["8c670823.21ac9"]]},{"id":"8c670823.21ac9","type":"function","z":"c74669a0.6a34f8","name":"","func":"for(let i=0;i<7;i++){\n    setTimeout(() => {node.send(msg)}, i*2*1000)\n}\nif(flow.get(\"run\")){\n    setTimeout(() => {node.send([null,msg])}, 25*60*1000)\n}\nreturn null;","outputs":2,"noerr":0,"initialize":"","finalize":"","x":280,"y":2160,"wires":[["a570b87f.d22fe8"],["e0e0f85d.008228"]]},{"id":"a570b87f.d22fe8","type":"debug","z":"c74669a0.6a34f8","name":"out 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":550,"y":2160,"wires":[]},{"id":"e0e0f85d.008228","type":"switch","z":"c74669a0.6a34f8","name":"","property":"run","propertyType":"flow","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":280,"y":2200,"wires":[["8c670823.21ac9"]]},{"id":"10edad9.21280d2","type":"switch","z":"c74669a0.6a34f8","name":"","property":"parts.index","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"str"},{"t":"eq","v":"1","vt":"str"},{"t":"eq","v":"2","vt":"str"}],"checkall":"false","repair":false,"outputs":3,"x":560,"y":2020,"wires":[["5369c443.3d6454"],["2863627c.de639e"],["a570b87f.d22fe8"]]},{"id":"5369c443.3d6454","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":730,"y":2000,"wires":[]},{"id":"2863627c.de639e","type":"debug","z":"c74669a0.6a34f8","name":"out 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":2040,"wires":[]},{"id":"71bb8075.c7fc48","type":"split","z":"c74669a0.6a34f8","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":370,"y":2020,"wires":[["10edad9.21280d2"]]},{"id":"449af8d3.050b68","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[false,false,false]","payloadType":"json","x":150,"y":2020,"wires":[["71bb8075.c7fc48"]]},{"id":"b14b8c12.eb568","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":80,"y":2260,"wires":[["586e2d84.6993f4"]]},{"id":"586e2d84.6993f4","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"run","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":2280,"wires":[[]]},{"id":"f6c3ffe.e6d3f","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":70,"y":2300,"wires":[["586e2d84.6993f4"]]}]

set flow.run true to continue loop and false to stop loop.

I use
https://flows.nodered.org/node/node-red-contrib-msg-resend

when I want to repeat messages

1 Like

@E1cid Thanks for sharing the function code and it works , i am too far away from coding part :slight_smile: hoping to learn them some day , can you tell me what this part in the script does " i21000 "

ì*2*1000 is the time out in milliseconds
eg 1 x 2 x 1000 on first loop
2 x 2 x 1000 on second, etc

1 Like

@cymplecy i am getting these error if i try to install the node

:frowning:
I'll page @BartButenaers the node author (he isn't able to program at moment so there won't be a fast response)

Just for info - what system is your Node-RED setup on and what version of NR/node.js are you running?

FYI - I just tried installing it on a NR 1.3.5 node,js 10.x windows instance and it installed OK

@cymplecy mine running on debian 10 v 1.2.5 seems i am getting the error when installing any additional nodes not sure what happened suddenly let me try to check on my side

Your node-red server has a DNS issue or is not connected to internet.

Search this forum for EAI_AGAIN

2 Likes

ya ya already on to it checking :slight_smile:

@cymplecy @Steve-Mcl it was DNS issue , my firewall was blocking the DNS not sure why but fixed it .

now i am able to install without problem .

1 Like

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