Calculation of total

Hi, How can i calculate the total of these fields ? +duration ?

Thanks,
Stefan

Hey @Stefan!

Can you please share some more details? What total do you want to calculate? Do you want to calculate the time duration a workflow takes to execute?

1 Like

These here. I would like to have the summary in total of the duration.

The example below should do it.

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      number: 10\n    }\n  },\n    {\n    json: {\n      number: 30\n    }\n  }\n]"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        490,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [],
          "number": [
            {
              "name": "duration",
              "value": "={{$node[\"Function\"].json[\"number\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        750,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "let total = 0;\n\nfor (const item of items) {\n  total = total + item.json.duration\n}\n\nreturn [\n  {\n    json: {\n      total,\n    }\n  }\n]"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        950,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}