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
          }
        ]
      ]
    }
  }
}

Hey.

I have similar need to calculate total, but function returns numbers stucked together. Instead of mock data I have Select from MySQL, numbers in result are ex. 3, 3, 3, 3, 8.5 … and total result is 033333333333333333333333333333333333333333333333333333333333333333333333333333333333338.538.5333333

What is wrong and what I need to change to get total summary. I also tried https://n8n.io/workflows/1497 and result is the same.

Can you share the data you are working with? Do you get an array of numbers and want to return only the total?