# Stop flow after final node executes set number of times

**URL:** https://discourse.nodered.org/t/stop-flow-after-final-node-executes-set-number-of-times/6238
**Category:** General
**Created:** [30 December 2018 18:09 UTC](https://discourse.nodered.org/t/stop-flow-after-final-node-executes-set-number-of-times/6238 "2018-12-30T18:09:28Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![bgbaker](https://avatars.discourse-cdn.com/v4/letter/b/a183cd/32.png) [@bgbaker](https://discourse.nodered.org/u/bgbaker)
#### Post date: [30 December 2018 18:09 UTC](https://discourse.nodered.org/t/stop-flow-after-final-node-executes-set-number-of-times/6238/1 "2018-12-30T18:09:28Z")

</div>

![flow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/d/dbe08e02509a6697556be6ea65682a4f950373de.png)

Hi everyone.

Very new to Node-Red and I am mainly using it for home automation with HA.

I have created a flow that checks connectivity using advanced ping and after 3 failures it needs to execute a python script which reboots my router.  
I want it to try do this a set number of times and then not try again in the case that there is a problem on the ISP side or some other issue and it is not with the router.  
I cant think of the logic or what you can use to stop the flow. Would really appreciate your help.  
I have uploaded a shot of the flow for info

Regards

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [30 December 2018 20:30 UTC](https://discourse.nodered.org/t/stop-flow-after-final-node-executes-set-number-of-times/6238/2 "2018-12-30T20:30:34Z")

</div>

As with any program, what you need here is a counter and a test.

So after each check, use either a function node or a change node with JSONata to increment a flow variable (I think there is a dedicated counter node somewhere so you could also check [flows.nodered.org](http://flows.nodered.org).

Before your check add a switch node to check that the counter variable is less than your maximum. If it is, do the check, otherwise do nothing.

For fun, here is a somewhat different approach to a similar issue except this reboots the device that Node-RED is running on if the router can't be reached for at least 60 sec. I haven't put a counter on it though I probably should.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/0/0bc0d405ef84b7922fbb517c930f6dfea72346b5.png)
