Loop over Item

Describe the problem/error/question

Hi guys.
I want to check an API every minute and if a certain query changes I want to go out of the loop. I like the loop to run for a certain amount of time.
I can’t find out how I can manage that. Can anyone help me?


{
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "b179c77d4379bfcd59361de6cfbc65a16ad4d2c07f085fc31ce786510423c5c8"
  },
  "nodes": [
    {
      "parameters": {
        "batchSize": "=1",
        "options": {
          "reset": true
        }
      },
      "id": "06763bde-5f22-4234-8ffa-bfd038a55676",
      "name": "Loop Over Items",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        280,
        460
      ]
    },
    {
      "parameters": {
        "amount": 2,
        "unit": "seconds"
      },
      "id": "d5954b70-a21e-479e-951f-945b72eb22f0",
      "name": "S1 10min1",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1,
      "position": [
        560,
        480
      ],
      "webhookId": "7fbc94c1-c371-46f8-93a9-c688ec37cbaf"
    },
    {
      "parameters": {
        "url": "https://examplel.com/wp-json/custom/v1/user-meta/",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "user_id",
              "value": "=161"
            },
            {
              "name": "meta_key",
              "value": "=poolplus4"
            }
          ]
        },
        "options": {}
      },
      "id": "3f2a5278-7da0-406d-96c6-217aa4a3bdbc",
      "name": "S1 - Status ",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        800,
        480
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "fa425c8a-381b-49a1-ae40-0061fea72ad6",
              "leftValue": "={{ $json['session status'] }}",
              "rightValue": "=true",
              "operator": {
                "type": "string",
                "operation": "equals",
                "name": "filter.operator.equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "aef913c5-96cd-4b8f-b6da-09537db89ef3",
      "name": "S1 - Check ",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        980,
        480
      ]
    },
    {
      "parameters": {},
      "id": "c2b3b258-5230-4c0b-bb2d-6404112d8e13",
      "name": "When clicking \"Test workflow\"",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        20,
        460
      ]
    }
  ],
  "connections": {
    "Loop Over Items": {
      "main": [
        null,
        [
          {
            "node": "S1 10min1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "S1 10min1": {
      "main": [
        [
          {
            "node": "S1 - Status ",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "S1 - Status ": {
      "main": [
        [
          {
            "node": "S1 - Check ",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "S1 - Check ": {
      "main": [
        null,
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When clicking \"Test workflow\"": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {}
}```

Information on your n8n setup

  • **n8n version:latest
  • **Databas: SQLite
  • **Running n8n via Docker
  • **Operating system:Ubuntu

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

@Mohammadali_Mo , I don’t think you need to engage Loop node here. I would do something like this.

Do bear in mind that workflow could reach the timeout cutoff and stop before the “certain query changes”.

Thanks @ihortom. I need something to count how much the loop gets repeated. for example after 5 repeats exit the loop. Is that possible?

@Mohammadali_Mo , absolutely. Just add checking the variable $runIndex in your IF node. Currently you check if status changed. Add OR condition that validates to true if $runIndex > 4.

Also you seem to check the status with the expression {{ $json['session status'] == "true" }}. Perhaps you meant to use Boolean expression (they way I did with checking $runIndex)?

1 Like

Thank you very much @ihortom. It works perfectly.
One more question. How can I get to know these system variables like $runIndex.
Is it in the documentation? If it is could you do me a favor and tell me?

Yes, you can refer to

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.