Describe the issue/error/question
Been struggling with this for hours so cheers in advance if anyone has any ideas.
We need to subtract a number (that changes each loop) from an initial value.
Below are 3 of many many attempts. The issue we appear to be facing is the ‘variable’ set in the ‘function variable value’ node is not affected, possibly because it’s outside of the loop.
Some of our research suggests that it’s possible but some indicate it’s not. We figured it’s likely possible but our knowledge is just not there yet.
Cheers to anyone that can help with this seemingly easy issue.
What is the error message (if any)?
Please share the workflow
Share the output returned by the last node
Information on your n8n setup
n8n version: unknown
Database you’re using (default: SQLite): unknown
Running n8n with the execution process [own(default), main]: unknown
Running n8n via [Docker, npm, n8n.cloud, desktop app]: container
Hey @ersatz.feign ,
welcome to the community
The expressions in your Set and IF node are always using your first nodes output, that never changes.
I’ve build an example workflow merging your two input variables and start looping after that.
Thank you very much for that as that does indeed do what you say.
Perhaps it’s just because we are still learning but we have tried to move things around without any luck. The issue is related to the title of this post in that the random number changes each loop - so do you think it’s possible to subtract a random number that (changes each loop(function randomnumber value)) from the initial variable value which is set once (function variable value?)
Gotcha. I simplified the workflow and added a “Randomize” Set Node using
Workflow with random number:
This is excellent so thank you very much as it proves maths is possible. (It was actually one of the first things we tried.)
It would appear the issue lies in the initial ‘function variable value’ not passing into the loop. At the moment, our first action in the loop (before your ‘Randomize’ node) is fetching external data and the results do not include that ‘function variable value’ that’s needed later on in the loop for your ‘Subtract’ node.
We’ve tried researching so wondering if you know if there is a way for n8n to pass the ‘function variable value’ through a node that is fetching external data? Or, perhaps we need a global variable that can be used later on in the workflow regardless of the actions in between?
We’ve been researching and trying different things for almost all of the time since our last post but unfortunately, not finding anything.
We just need to work out how to pass data through nodes (that fetch external data) so that we can use what was passed through, later on in the workflow. Am hoping that n8n is able to achieve this but can’t find anything to suggest it is.
If anyone happens to know, that would save us dozens of hours attempting to work it out.
Please let us know if we need to start a fresh thread for this as it’s almost not related.
Right, so after an entire day of trying everything we can think of, it would appear n8n is possibly not capable of doing this seemingly simple thing, as per Jan’s comment
In light of that, is anyone feeling kind enough to possibly think of any workarounds?
Effectively, within a loop, we need to do maths on a variable which starts at a set value (and therefore we suspect has to be set outside of the loop as per the initial workflow we posted.)
It might help to think of it as running a counter in a loop but starting at a set number instead of zero. A global variable could work as well I guess but with n8n being so comprehensive, we figured there surely must be an easy method to achieve this and surely can’t be the first people for such a frequent requirement.
(Please let us know if this is best posted in a new thread for the rules but also to get more exposure.)
I’v build a workflow that initiates a counter outside the loop and loads/saves the counter inside the loop. Is this helpful to your scenario?
Load Counter from
Initiate Counter Node
Save Counter to
Initiate Counter Node
Just for a bit of fun I would like to throw this workflow into the mix which should also do the same thing.
Marcus! You did it!
Thank you very much! Just those two save/load lines are game-changing for us and can now be applied to many other workflows too.
We even managed to refactor this one down to ~100 nodes and hope to half that again. But the main thing is- we are operational thanks to you!
Keep up the good work - you probably have no idea how much of an impact you are having on so many people!
Thank you very much for this Jon.
We did attempt to get it working but with so many nodes, troubleshooting can take a while.
We got Marcus’ working and have already implemented it but hope to revisit yours soon and discect it to learn from.
Thanks for all your help and I hope you have a lovely weekend!