If node seemingly not comparing numbers

Hi

I have an IF node that doesn’t appear to be comparing two numbers correctly.

I’m getting paginated HTTP data and appending the results to a Google Spreadsheet. There is an IF node which should check that the current page number is smaller than the total page number. If smaller, increment the page number and repeat; else end.

However, the loop keeps running:


(See the Set node after the IF false is being reached)

Any advice would be appreciated.

Typical response format

[
  {
    "currentPage": 1,
    "totalItems": 7329,
    "totalPages": 294,
    "items": [
      {
        "id": 8025216,
        "eventId": 516682,
        "siteUnitNumber": 12616,
        "bathingSite": "GURNARD",
        "eventStart": "2021-10-31T08:18:25",
        "eventStop": "2021-11-05T06:00:01.13",
        "duration": 7061,
        "activity": "Not Reviewed",
        "associatedSiteId": 109650,
        "outfallName": "MARSH ROAD GURNARD"
      },
      {
        "id": 8025217,
        "eventId": 516682,
        "siteUnitNumber": 12617,
        "bathingSite": "COWES",
        "eventStart": "2021-10-31T08:18:25",
        "eventStop": "2021-11-05T06:00:01.13",
        "duration": 7061,
        "activity": "Not Reviewed",
        "associatedSiteId": 109650,
        "outfallName": "MARSH ROAD GURNARD"
      },
      {
        "id": 8025218,
        "eventId": 519475,
        "siteUnitNumber": 12617,
        "bathingSite": "COWES",
        "eventStart": "2021-11-04T22:45:00",
        "eventStop": "2021-11-04T23:15:00",
        "duration": 30,
        "activity": "Not Reviewed",
        "associatedSiteId": 109700,
        "outfallName": "MARKET HILL COWES"
      },
      {
        "id": 8025219,
        "eventId": 519475,
        "siteUnitNumber": 12616,
        "bathingSite": "GURNARD",
        "eventStart": "2021-11-04T22:45:00",
        "eventStop": "2021-11-04T23:15:00",
        "duration": 30,
        "activity": "Not Reviewed",
        "associatedSiteId": 109700,
        "outfallName": "MARKET HILL COWES"
      },
      {
        "id": 8025220,
        "eventId": 519474,
        "siteUnitNumber": 12616,
        "bathingSite": "GURNARD",
        "eventStart": "2021-11-04T22:00:00",
        "eventStop": "2021-11-04T22:15:00",
        "duration": 15,
        "activity": "Not Reviewed",
        "associatedSiteId": 109700,
        "outfallName": "MARKET HILL COWES"
      },
      {
        "id": 8025221,
        "eventId": 519474,
        "siteUnitNumber": 12617,
        "bathingSite": "COWES",
        "eventStart": "2021-11-04T22:00:00",
        "eventStop": "2021-11-04T22:15:00",
        "duration": 15,
        "activity": "Not Reviewed",
        "associatedSiteId": 109700,
        "outfallName": "MARKET HILL COWES"
      },
      {
        "id": 8025222,
        "eventId": 519307,
        "siteUnitNumber": 12609,
        "bathingSite": "LEPE",
        "eventStart": "2021-11-04T12:20:48",
        "eventStop": "2021-11-04T12:39:00",
        "duration": 18,
        "activity": "Not Reviewed",
        "associatedSiteId": 109825,
        "outfallName": "WEST COMMON FAWLEY"
      },
      {
        "id": 8025223,
        "eventId": 519129,
        "siteUnitNumber": 12549,
        "bathingSite": "HERNE BAY CENTRAL",
        "eventStart": "2021-11-03T20:40:50",
        "eventStop": "2021-11-03T21:12:14",
        "duration": 31,
        "activity": "Reviewed",
        "associatedSiteId": 109830,
        "outfallName": "SWALECLIFFE NO1"
      },
      {
        "id": 8025224,
        "eventId": 519129,
        "siteUnitNumber": 12548,
        "bathingSite": "TANKERTON",
        "eventStart": "2021-11-03T20:40:50",
        "eventStop": "2021-11-03T21:12:14",
        "duration": 31,
        "activity": "Reviewed",
        "associatedSiteId": 109830,
        "outfallName": "SWALECLIFFE NO1"
      },
      {
        "id": 8025225,
        "eventId": 519237,
        "siteUnitNumber": 12593,
        "bathingSite": "FELPHAM",
        "eventStart": "2021-11-02T18:22:00",
        "eventStop": "2021-11-02T23:23:00",
        "duration": 301,
        "activity": "Reviewed",
        "associatedSiteId": 110095,
        "outfallName": "LIDSEY"
      },
      {
        "id": 8025226,
        "eventId": 518795,
        "siteUnitNumber": 12593,
        "bathingSite": "FELPHAM",
        "eventStart": "2021-11-02T14:37:00",
        "eventStop": "2021-11-02T16:18:00",
        "duration": 101,
        "activity": "Reviewed",
        "associatedSiteId": 110095,
        "outfallName": "LIDSEY"
      },
      {
        "id": 8025227,
        "eventId": 518702,
        "siteUnitNumber": 12593,
        "bathingSite": "FELPHAM",
        "eventStart": "2021-11-02T09:36:38",
        "eventStop": "2021-11-02T12:24:00",
        "duration": 167,
        "activity": "Reviewed",
        "associatedSiteId": 110095,
        "outfallName": "LIDSEY"
      },
      {
        "id": 8025228,
        "eventId": 518232,
        "siteUnitNumber": 12593,
        "bathingSite": "FELPHAM",
        "eventStart": "2021-11-01T07:23:08",
        "eventStop": "2021-11-02T00:24:00",
        "duration": 1020,
        "activity": "Reviewed",
        "associatedSiteId": 110095,
        "outfallName": "LIDSEY"
      },
      {
        "id": 8025229,
        "eventId": 518684,
        "siteUnitNumber": 12661,
        "bathingSite": "LANGSTONE HARBOUR",
        "eventStart": "2021-11-01T16:21:00",
        "eventStop": "2021-11-01T23:30:00",
        "duration": 429,
        "activity": "Reviewed",
        "associatedSiteId": 109773,
        "outfallName": "BUDDS FARM HAVANT NO2"
      },
      {
        "id": 8025230,
        "eventId": 518798,
        "siteUnitNumber": 12622,
        "bathingSite": "WHITECLIFF BAY",
        "eventStart": "2021-11-01T19:15:00",
        "eventStop": "2021-11-01T22:32:00",
        "duration": 197,
        "activity": "Reviewed",
        "associatedSiteId": 110096,
        "outfallName": "SANDOWN NO1"
      },
      {
        "id": 8025231,
        "eventId": 518798,
        "siteUnitNumber": 12623,
        "bathingSite": "YAVERLAND",
        "eventStart": "2021-11-01T19:15:00",
        "eventStop": "2021-11-01T22:32:00",
        "duration": 197,
        "activity": "Reviewed",
        "associatedSiteId": 110096,
        "outfallName": "SANDOWN NO1"
      },
      {
        "id": 8025232,
        "eventId": 518798,
        "siteUnitNumber": 12624,
        "bathingSite": "SANDOWN",
        "eventStart": "2021-11-01T19:15:00",
        "eventStop": "2021-11-01T22:32:00",
        "duration": 197,
        "activity": "Reviewed",
        "associatedSiteId": 110096,
        "outfallName": "SANDOWN NO1"
      },
      {
        "id": 8025233,
        "eventId": 518574,
        "siteUnitNumber": 12582,
        "bathingSite": "SEAFORD",
        "eventStart": "2021-11-01T21:50:00",
        "eventStop": "2021-11-01T22:08:00",
        "duration": 18,
        "activity": "Reviewed",
        "associatedSiteId": 110920,
        "outfallName": "NEWHAVEN"
      },
      {
        "id": 8025234,
        "eventId": 518797,
        "siteUnitNumber": 12622,
        "bathingSite": "WHITECLIFF BAY",
        "eventStart": "2021-11-01T17:27:00",
        "eventStop": "2021-11-01T19:10:00",
        "duration": 103,
        "activity": "Reviewed",
        "associatedSiteId": 110096,
        "outfallName": "SANDOWN NO1"
      },
      {
        "id": 8025235,
        "eventId": 518797,
        "siteUnitNumber": 12624,
        "bathingSite": "SANDOWN",
        "eventStart": "2021-11-01T17:27:00",
        "eventStop": "2021-11-01T19:10:00",
        "duration": 103,
        "activity": "Reviewed",
        "associatedSiteId": 110096,
        "outfallName": "SANDOWN NO1"
      },
      {
        "id": 8025236,
        "eventId": 518797,
        "siteUnitNumber": 12623,
        "bathingSite": "YAVERLAND",
        "eventStart": "2021-11-01T17:27:00",
        "eventStop": "2021-11-01T19:10:00",
        "duration": 103,
        "activity": "Reviewed",
        "associatedSiteId": 110096,
        "outfallName": "SANDOWN NO1"
      },
      {
        "id": 8025237,
        "eventId": 518497,
        "siteUnitNumber": 12623,
        "bathingSite": "YAVERLAND",
        "eventStart": "2021-11-01T17:40:00",
        "eventStop": "2021-11-01T19:02:14",
        "duration": 82,
        "activity": "Reviewed",
        "associatedSiteId": 110097,
        "outfallName": "SANDOWN NO2"
      },
      {
        "id": 8025238,
        "eventId": 518497,
        "siteUnitNumber": 12624,
        "bathingSite": "SANDOWN",
        "eventStart": "2021-11-01T17:40:00",
        "eventStop": "2021-11-01T19:02:14",
        "duration": 82,
        "activity": "Reviewed",
        "associatedSiteId": 110097,
        "outfallName": "SANDOWN NO2"
      },
      {
        "id": 8025239,
        "eventId": 518497,
        "siteUnitNumber": 12622,
        "bathingSite": "WHITECLIFF BAY",
        "eventStart": "2021-11-01T17:40:00",
        "eventStop": "2021-11-01T19:02:14",
        "duration": 82,
        "activity": "Reviewed",
        "associatedSiteId": 110097,
        "outfallName": "SANDOWN NO2"
      },
      {
        "id": 8025240,
        "eventId": 518488,
        "siteUnitNumber": 12622,
        "bathingSite": "WHITECLIFF BAY",
        "eventStart": "2021-11-01T15:56:00",
        "eventStop": "2021-11-01T17:21:00",
        "duration": 85,
        "activity": "Reviewed",
        "associatedSiteId": 110096,
        "outfallName": "SANDOWN NO1"
      }
    ]
  }
]

Workflow

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        50,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "number": [
            {
              "name": "page",
              "value": 1
            }
          ],
          "string": []
        },
        "options": {}
      },
      "name": "Set Page Number",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "amount": "={{Math.floor(Math.random() * 5)+1}}",
        "unit": "seconds"
      },
      "name": "Wait",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1,
      "position": [
        1450,
        200
      ],
      "webhookId": "c4428081-65cc-44a5-b2ae-6fb1cd716c6f"
    },
    {
      "parameters": {
        "values": {
          "number": [
            {
              "name": "page",
              "value": "={{$node[\"HTTP\"].json[\"currentPage\"]+1}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Increment Page",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        750,
        0
      ]
    },
    {
      "parameters": {
        "authentication": "headerAuth",
        "options": {},
        "queryParametersUi": {
          "parameter": [
            {
              "name": "page",
              "value": "={{$json[\"page\"]}}"
            }
          ]
        }
      },
      "name": "HTTP",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        650,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "id": "2",
          "name": "Southern Water API"
        }
      }
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "operation": "append",
        "sheetId": "={{$node[\"Prepare Sheet\"].parameter[\"sheetId\"]}}",
        "range": "A:J",
        "options": {}
      },
      "name": "Add to Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 1,
      "position": [
        1050,
        300
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "3",
          "name": "Google Sheets theneilkirk"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{parseInt($node[\"HTTP\"].json[\"currentPage\"])}}",
              "operation": "smallerEqual",
              "value2": 3
            }
          ]
        }
      },
      "name": "If last page",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "var events = [];\nfor (item in items[0].json[\"items\"]) {\n  events.push({json:items[0].json[\"items\"][item]});\n}\n\nreturn events;"
      },
      "name": "Extract Events",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        850,
        300
      ]
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "operation": "clear",
        "range": "A2:J"
      },
      "name": "Prepare Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 1,
      "position": [
        250,
        300
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "3",
          "name": "Google Sheets theneilkirk"
        }
      }
    },
    {
      "parameters": {
        "values": {
          "number": [
            {
              "name": "lastPage",
              "value": "={{$node[\"HTTP\"].json[\"currentPage\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1430,
        470
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Prepare Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Page Number": {
      "main": [
        [
          {
            "node": "HTTP",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait": {
      "main": [
        [
          {
            "node": "Increment Page",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Increment Page": {
      "main": [
        [
          {
            "node": "HTTP",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP": {
      "main": [
        [
          {
            "node": "Extract Events",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Add to Sheet": {
      "main": [
        [
          {
            "node": "If last page",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If last page": {
      "main": [
        [
          {
            "node": "Wait",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Events": {
      "main": [
        [
          {
            "node": "Add to Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Sheet": {
      "main": [
        [
          {
            "node": "Set Page Number",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hi @theneilkirk, I’m sorry to hear you’re running into this behaviour. I’d need to give your specific workflow a go later today (unless someone else beats me to it, of course). Can you confirm if the example output you have provided is from your HTTP Request node or from another node?

The first thing that comes to mind without looking into this in too much depth is that the if node runs once for each item it receives, so if your Add to Sheet node outputs multiple items that might be the culprit here.

On the general question whether the IF node might not be comparing numbers correctly, I have put together a quick workflow going through multiple pages and it seems to be working fine:

Example Workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://reqres.in/api/users",
        "options": {},
        "queryParametersUi": {
          "parameter": [
            {
              "name": "page",
              "value": "={{$json[\"page\"]}}"
            }
          ]
        }
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "number": [
            {
              "name": "page",
              "value": "={{$json[\"page\"] || 1}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set Page No",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{$json[\"page\"]}}",
              "operation": "largerEqual",
              "value2": "={{$json[\"total_pages\"]}}"
            }
          ]
        }
      },
      "name": "Last Page?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        850,
        300
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "number": [
            {
              "name": "page",
              "value": "={{$json[\"page\"] + 1}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Increment Page No",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        650,
        550
      ]
    },
    {
      "parameters": {},
      "name": "End",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1050,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set Page No",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Last Page?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Page No": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Last Page?": {
      "main": [
        [
          {
            "node": "End",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Increment Page No",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Increment Page No": {
      "main": [
        [
          {
            "node": "Set Page No",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

1 Like

You were totally right. I simply changed the flow to split after the HTTP request: one branch to the IF node and the other branch to the Function. Worked perfectly.

Thank you!

1 Like