Stop flow after final node executes set number of times

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

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.

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.