Count number of time loop has run

I have a loop that checks if the SSH outputs a certain message, if so then try the command again until it works.

Is there a way to count the number of times a loop has run?

When it gets to a certain number, I then plan to route it down an error path for someone to go and check why and kill the entire flow (which is triggered by a webhook).

1 Like

Hey @RedPacketSec,

If you are using the Split in batches node you can use {{$node["SplitInBatches"].context["currentRunIndex"]}} to get the current index.


You can also use the variable $runIndex in an expression on every node.

1 Like

Interesting, got any examples of this in use?

Not sure I understand. Gets used like any other variable in n8n.

So if you want to for example exit a loop after a certain number of iterations you can set an expression like the following on the IF-Node:
{{ $runIndex === 4 }}


{{ $runIndex }} doesn’t seem to be working. It always returns 0. Is this only for batch or should work with the List Item node too?