Create a counter

Hello.

I need to create a workflow that has a counter to delivery the following soluction:

There are an application that has a wait status, and I need to send a message showing how long the people are waiting. On workflow has two timers (sleep command) that wait 5 and 10 minutes. After second timer (10min), there is a loop that sends message every 10 minutes, in other words, a first time, the message has 15 minutes, every loop, this will be add 10 minutes and the Teams message sending 25, 35, 45 minutes…

I made this using Set node, but while it is manually, Is there a way to increase this cout automatically?

{
  "nodes": [
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "number": [
            {
              "name": "timer",
              "value": 5
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1640,
        120
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "number": [
            {
              "name": "soma",
              "value": "={{$node[\"Set\"].parameter[\"values\"][\"number\"][0][\"value\"]+10}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set3",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1790,
        120
      ]
    },
    {
      "parameters": {
        "resource": "channelMessage",
        "teamId": "4346b14-e0b-b4e-b38-394b215263a",
        "channelId": "19:[email protected]",
        "messageType": "html",
        "message": "=<h2 style=\"color:red;\"><b>IMPORTANTE</b>  - Aguardando atendimento Conecta </h2>\nContato: {{$node[\"Webhook\"].json[\"body\"][\"data[name]\"]}},aguardando a {{$json[\"soma\"]}} minutos.",
        "options": {}
      },
      "name": "Microsoft Teams1",
      "type": "n8n-nodes-base.microsoftTeams",
      "typeVersion": 1,
      "position": [
        1970,
        110
      ],
      "notesInFlow": true,
      "credentials": {
        "microsoftTeamsOAuth2Api": "HUB_TeamsAPP"
      },
      "notes": "Enviar notificação."
    }
  ],
  "connections": {
    "Set": {
      "main": [
        [
          {
            "node": "Set3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set3": {
      "main": [
        [
          {
            "node": "Microsoft Teams1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hey @leucotron!

You can use the Function node for this. The $item() method can be used here to get the value for each execution. Function | Docs

You will have to create a loop so that these actions are performed continuously.

Hey @harshil1712, Thank you for reply.

Using Function node, do I need to use Set node too?

Hey @harshil1712, could you help about this workflow?
We are using the Function node to generate the the counter and the dynamic Teams message.
The first time, it works, and send “15 minutes” on Teams, but in the loop, this value don’t change, it’ always 15 minutes.
We are trying to use a variable to keep the last value after the Function node, but this variable don’t works.
Where is wrong?
Thanks.

{
    "nodes": [
        {
            "parameters": {
                "executeOnce": false,
                "command": "sleep 10"
            },
            "name": "Execute Command1",
            "type": "n8n-nodes-base.executeCommand",
            "typeVersion": 1,
            "position": [
                880,
                220
            ],
            "notesInFlow": true,
            "alwaysOutputData": false,
            "notes": "Aguarda 15 minutos"
        },
        {
            "parameters": {
                "requestMethod": "POST",
                "url": "https://demo.conecta.com.vc/api/v4/auth/login",
                "options": {},
                "bodyParametersUi": {
                    "parameter": [
                        {
                            "name": "email",
                            "value": "[email protected]"
                        },
                        {
                            "name": "password",
                            "value": "****"
                        },
                        {
                            "name": "device_token",
                            "value": "f8a8dbc3b807c545be9722b41b"
                        }
                    ]
                },
                "headerParametersUi": {
                    "parameter": []
                }
            },
            "name": "HTTP Request2",
            "type": "n8n-nodes-base.httpRequest",
            "typeVersion": 1,
            "position": [
                1020,
                220
            ],
            "notesInFlow": true,
            "notes": "Atualizar API Token"
        },
        {
            "parameters": {
                "requestMethod": "POST",
                "url": "https://demo.conecta.com.vc/api/v4/attendances/show",
                "options": {},
                "bodyParametersUi": {
                    "parameter": [
                        {
                            "name": "contact_id",
                            "value": "={{$node[\"Webhook\"].json[\"body\"][\"data[contact_id]\"]}}"
                        }
                    ]
                },
                "headerParametersUi": {
                    "parameter": [
                        {
                            "name": "Authorization",
                            "value": "=Bearer {{$node[\"HTTP Request2\"].json[\"token\"]}}"
                        }
                    ]
                }
            },
            "name": "HTTP Request4",
            "type": "n8n-nodes-base.httpRequest",
            "typeVersion": 1,
            "position": [
                1160,
                220
            ],
            "notesInFlow": true,
            "notes": "Status de conexões ativas."
        },
        {
            "parameters": {
                "conditions": {
                    "string": [
                        {
                            "value1": "={{$node[\"HTTP Request4\"].json[\"phase\"]}}",
                            "value2": "wait"
                        },
                        {
                            "value1": "={{$node[\"Webhook\"].json[\"body\"][\"data[session_id]\"]}}",
                            "value2": "={{$node[\"HTTP Request4\"].json[\"_id\"]}}"
                        }
                    ],
                    "boolean": [],
                    "dateTime": []
                }
            },
            "name": "IF",
            "type": "n8n-nodes-base.if",
            "typeVersion": 1,
            "position": [
                1370,
                220
            ]
        },
        {
            "parameters": {
                "functionCode": "if (items[0].json.timerAux == undefined) {\n items[0].json.timerAux = 15\n }\n else {\n var addTimer = 10;\n items[0].json.timerAux += addTimer;\n }\n\nreturn items\n"
            },
            "name": "Function",
            "type": "n8n-nodes-base.function",
            "typeVersion": 1,
            "position": [
                1590,
                200
            ]
        },
        {
            "parameters": {
                "keepOnlySet": true,
                "values": {
                    "number": [
                        {
                            "name": "newTimer",
                            "value": "={{$json[\"timerAux\"]}}"
                        }
                    ]
                },
                "options": {}
            },
            "name": "Set1",
            "type": "n8n-nodes-base.set",
            "typeVersion": 1,
            "position": [
                1800,
                200
            ]
        },
        {
            "parameters": {
                "resource": "channelMessage",
                "teamId": "4347614-0e0-44e-b38-391215263a",
                "channelId": "19:[email protected]",
                "messageType": "html",
                "message": "=<h2 style=\"color:red;\">IMPORTANTE - Aguardando atendimento Conecta \nContato: {{$node[\"Webhook\"].json[\"body\"][\"data[name]\"]}}, aguardando a {{$json[\"newTimer\"]}} minutos.",
                "options": {}
            },
            "name": "Microsoft Teams1",
            "type": "n8n-nodes-base.microsoftTeams",
            "typeVersion": 1,
            "position": [
                1990,
                100
            ],
            "notesInFlow": true,
            "credentials": {
                "microsoftTeamsOAuth2Api": "HUB_TeamsAPP"
            },
            "notes": "Enviar notificação."
        },
        {
            "parameters": {
                "functionCode": "items[0].json.timerAux = $json[\"newTimer\"]\n\nreturn items;"
            },
            "name": "Function1",
            "type": "n8n-nodes-base.function",
            "typeVersion": 1,
            "position": [
                1600,
                -240
            ]
        },
        {
            "parameters": {},
            "name": "NoOp2",
            "type": "n8n-nodes-base.noOp",
            "typeVersion": 1,
            "position": [
                1300,
                420
            ]
        }
    ],
    "connections": {
        "Execute Command1": {
            "main": [
                [
                    {
                        "node": "HTTP Request2",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "HTTP Request2": {
            "main": [
                [
                    {
                        "node": "HTTP Request4",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "HTTP Request4": {
            "main": [
                [
                    {
                        "node": "IF",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "IF": {
            "main": [
                [
                    {
                        "node": "Function",
                        "type": "main",
                        "index": 0
                    }
                ],
                [
                    {
                        "node": "NoOp2",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Function": {
            "main": [
                [
                    {
                        "node": "Set1",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Set1": {
            "main": [
                [
                    {
                        "node": "Microsoft Teams1",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Microsoft Teams1": {
            "main": [
                [
                    {
                        "node": "Function1",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Function1": {
            "main": [
                [
                    {
                        "node": "Execute Command1",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        }
    }
}

Hey @leucotron!

I would set the initial value out of the loop using the Set node and manipulate this value inside the loop. Currently, you’re setting the initial value in the loop. Every time the loop runs, this initial value is set. The following image might give you a better idea

Hey @harshil1712, we made the workflow following your exemple and it worked, it create a variable and change this value acording the loop.
As we are not using a Database to store the variable (I think that the flow uses an internal buffer from our n8n instance), some times our workflow stop working, like the variable was lost, for exemple, after 20 minutes, it stops sending the message. I’m rigth? Can this internal buffer causing the problem?

Hey!

Did you changed the timeout for your workflows? I think due to the default timeout the workflow stops the execution.

No…
How can I do this?

You can do this via the below mentioned steps:

  1. Select Settings from the Workflow menu
  2. Toggle Timeout Workflow to true.
  3. Set the timeout value

1 Like

I’ll try this. Tanks.

Hey @harshil1712, I changed the workflow timeout, and make that it has up to 30 minutes, it working, but occours often “Unknown Status” and the workflow stop working.
How can I discovery the error?

Hey @leucotron!

If you click on the folder icon, it will show you the execution and the data that gets passed through the nodes in the workflow.

Hey @harshil1712, clicking on the folder icon is not showing nothing, but I noticed that n8n container is rebooting some times during the day, I’ll oberve it.

If the container restarts it is a sign that n8n crashed. That happens normally if it runs out of memory. That would then also explain the “Unknown” status.

Hey @jan, this afternoon the container didn’t crash and the workflow works fine all time.
Is there a log that I can see the out of memory?
How can I fix it? Do I increasing RAM memory can help?

n8n does not have a log for that.

You can try two things:

  1. Increasing the RAM as you mentioned
  2. If you do not run n8n already in “main” mode and rather in the default “own” switch over. You can find the documentation here.

Hey @jan, I increase the RAM, but didn’t work, the workflow continue as Unknown status, but the n8n containers is ok right now.

Using docker logs -f root_n8n_1 command, there are this:

Editor is now accessible via:
https://hub.leucotron.com.br:5678/
events.js:292
throw er; // Unhandled ‘error’ event
^

Error: read ECONNRESET
at TLSWrap.onStreamRead (internal/stream_base_commons.js:209:20)
Emitted ‘error’ event on TLSSocket instance at:
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
errno: -104,
code: ‘ECONNRESET’,
syscall: ‘read’
}

Hey @leucotron,

Did you change the mode from main to own?

I took a look at what the error means and this is what I found: sockets - How do I debug error ECONNRESET in Node.js? - Stack Overflow

From what has been shared in the above link, it is possible that the API errors out and doesn’t send a response back to n8n, causing the above error.

Hey @harshil1712 , thanks for reply.

About " Did you change the mode from main to own ?" I read the n8n documentation but I didn’t understantd how to do that. I mean, How to use the “export EXECUTIONS_PROCESS=own” command? Do I need to send it on docker-compose or on n8n container shell? Could you help me about this?

Hey @harshil1712
I made the docker-compose change. Is it right?
screenshot