# Repeat Mqtt Message 7 Times and Stop

**URL:** <https://discourse.nodered.org/t/repeat-mqtt-message-7-times-and-stop/46016>\
**Category:** General\
**Created:** [21 May 2021 07:44 UTC](https://discourse.nodered.org/t/repeat-mqtt-message-7-times-and-stop/46016 "2021-05-21T07:44:38Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![kurtufiyde](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@kurtufiyde](https://discourse.nodered.org/u/kurtufiyde)\
**Post date:** [21 May 2021 07:44 UTC](https://discourse.nodered.org/t/repeat-mqtt-message-7-times-and-stop/46016/1 "2021-05-21T07:44:39Z")

</div>

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 .

 ![resend](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/3/031f82745276435e5a8c67153450673c12ccd4fd.png)

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

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [21 May 2021 07:53 UTC](https://discourse.nodered.org/t/repeat-mqtt-message-7-times-and-stop/46016/2 "2021-05-21T07:53:47Z")

</div>

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

```auto
[{"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)

```auto
[{"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.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [21 May 2021 08:07 UTC](https://discourse.nodered.org/t/repeat-mqtt-message-7-times-and-stop/46016/3 "2021-05-21T08:07:06Z")

</div>

I use  
[https://flows.nodered.org/node/node-red-contrib-msg-resend](https://flows.nodered.org/node/node-red-contrib-msg-resend)

when I want to repeat messages

---

<div class="post-metadata">

**Author:** ![kurtufiyde](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@kurtufiyde](https://discourse.nodered.org/u/kurtufiyde)\
**Post date:** [21 May 2021 08:07 UTC](https://discourse.nodered.org/t/repeat-mqtt-message-7-times-and-stop/46016/4 "2021-05-21T08:07:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [21 May 2021 08:11 UTC](https://discourse.nodered.org/t/repeat-mqtt-message-7-times-and-stop/46016/5 "2021-05-21T08:11:32Z")

</div>

`ì*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

---

<div class="post-metadata">

**Author:** ![kurtufiyde](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@kurtufiyde](https://discourse.nodered.org/u/kurtufiyde)\
**Post date:** [21 May 2021 08:13 UTC](https://discourse.nodered.org/t/repeat-mqtt-message-7-times-and-stop/46016/6 "2021-05-21T08:13:48Z")

</div>

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

 ![Error](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/5/e52b412d939e8f808c5c098956f57b017358247f.png)

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [21 May 2021 08:16 UTC](https://discourse.nodered.org/t/repeat-mqtt-message-7-times-and-stop/46016/7 "2021-05-21T08:16:47Z")

</div>

☹  
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?

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [21 May 2021 08:22 UTC](https://discourse.nodered.org/t/repeat-mqtt-message-7-times-and-stop/46016/8 "2021-05-21T08:22:08Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/8/18200862beef1beb55d5f9b7c7c7b3a6a0f98d31.jpeg)

---

<div class="post-metadata">

**Author:** ![kurtufiyde](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@kurtufiyde](https://discourse.nodered.org/u/kurtufiyde)\
**Post date:** [21 May 2021 08:29 UTC](https://discourse.nodered.org/t/repeat-mqtt-message-7-times-and-stop/46016/9 "2021-05-21T08:29:08Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 May 2021 08:30 UTC](https://discourse.nodered.org/t/repeat-mqtt-message-7-times-and-stop/46016/10 "2021-05-21T08:30:50Z")

</div>

> [@kurtufiyde](#):
>
> i am getting these error if i try to install the node
> 
> ![Error](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/5/e52b412d939e8f808c5c098956f57b017358247f.png)

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

Search this forum for EAI\_AGAIN

---

<div class="post-metadata">

**Author:** ![kurtufiyde](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@kurtufiyde](https://discourse.nodered.org/u/kurtufiyde)\
**Post date:** [21 May 2021 08:32 UTC](https://discourse.nodered.org/t/repeat-mqtt-message-7-times-and-stop/46016/11 "2021-05-21T08:32:02Z")

</div>

> [@Steve-Mcl](#):
>
> EAI\_AGAIN

ya ya already on to it checking 🙂

---

<div class="post-metadata">

**Author:** ![kurtufiyde](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@kurtufiyde](https://discourse.nodered.org/u/kurtufiyde)\
**Post date:** [21 May 2021 08:39 UTC](https://discourse.nodered.org/t/repeat-mqtt-message-7-times-and-stop/46016/12 "2021-05-21T08:39:08Z")

</div>

@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 .

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [4 June 2021 08:39 UTC](https://discourse.nodered.org/t/repeat-mqtt-message-7-times-and-stop/46016/13 "2021-06-04T08:39:26Z")

</div>

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