I need to set up something in n8n to randomly choose a weekday between Monday and Friday, and also randomly select a time between 9 AM and 3 PM, once every 10 days, anyone have idea how to do that?
So you’re trying to run it at the first occurrence of that weekday/time after the 10 day interval elapses? Does the execution start the clock on the next 10 days?
Sounds like you need to schedule the workflow to run every hour or so, and check a date/time value like $now >= timeForNextExecution
to decide whether to quit or continue with the execution. Then, when you do continue with the execution, at the end, calculate (using random numbers, date functions, etc.) and update the value of timeForNextExecution
to the next date/time you want the execution to continue through the entire workflow.
You could use a DB or some other external datastore, or use the $getWorkflowStaticData feature to retain the timeForNextExecution
value.
I’ll explain in more detail. I need my bot to send a message to a list of clients I have, asking if they’re experiencing any issues. However, their working hours are only Monday through Friday—they don’t work on Saturdays or Sundays. The message must be sent between 9:00 AM and 3:00 PM, which is the time when the support team is available to respond in case a client says they’re having system issues.
The message timing needs to be random within these conditions, because the clients must not realize it’s a bot. My boss requires us to ask this question every 10 days to monitor the clients, and sometimes I forget to do it manually. I managed to build a workflow using Baserow, but it often causes issues.
I believe there must be a simpler and more reliable way, but I just can’t seem to figure it out.
I agree with Hubschrauber.
You should configure your workflow to trigger only during your required working hours and store the last message date for each client in a database.
This way, you can easily check if 10 days have passed before sending a new message, ensuring each client receives it at the right interval without duplication.
The message can be sent to all clients at the same time, that’s not a problem. It doesn’t need to be individual, since the clients don’t communicate with each other, so they won’t know when I contacted each one. What I’m trying to understand is a way to randomly choose a weekday between Monday and Friday, as well as a time between 9 AM and 3 PM, every 10 days, in order to trigger the message. If I use random numbers, won’t it trigger on a Saturday or Sunday, or outside the allowed time, for example?
The details of how to calculate a date with some specific constraints, using JS, isn’t really an n8n issue. You probably just need to review the documentation for a JS Date class, and do some searching for code examples like:
- https://www.google.com/search?q=JS+get+next+monday
- https://www.google.com/search?q=JS+date+add+day
- https://www.google.com/search?q=JS+random+integer+between+0+and
n8n expressions support most (all) of what you will need to end up with exactly the date/interval/etc. you want. Then the n8n technique of “polling” for when it’s time to perform the action should work. You’ll have to consider things like "how soon after the timeForNextExecution
will the next “polling” execution occur, and maybe shift the time of day back. You’ll also probably need to consider things like timezones and be sure you have the workflow, n8n itself, or the system clock set correctly relative to the time of day where your support team and/or clients are.
If you are looking for someone to do all this work for you, you might have better luck asking on the discord “looking for work” board
I hadn’t thought of that possibility. I even used NoCode with some JSON, but it wasn’t very effective, the use of the Wait node was necessary, which consumed a lot of processing. I’ll look into what you sent to see if I can find a solution, thank you!