Catch a variable value in a n-2 Function node through Switch?

WF contains Function then Swicth then ExecuteCommand
In the Function node I set up a local hash whose I need a member in the ExecuteCommand
I could put this value in Function output to make it travel but the Switch stops it because it only accepts limited integers on output
How could I do without setting up a gaz plant, and easy to maintain ?

Hey @minstrel!

The switch node doesn’t limit the output. It the condition is not met, it will not give the output. Make sure that the condition you have is satisfied and returns an output to the case (0,1,2,3) you mention. You can read more about the Switch node here.

Also if you can, please share the workflow here. I will take a look and see if there is anything else that is preventing the correct output. :slightly_smiling_face:

Hi harshil

Maybe I was not clear …
Here the Swicth node, you see the outDiag of the diagOnActive Function node as input value
You also see that outputs are just integers
So I can’t see at all how can I pass my hash value defined in the Function node to the nodes following the Switch ?

{
      "parameters": {
        "dataType": "string",
        "value1": "={{$node[\"diagOnActive\"].json[\"outDiag\"]}}",
        "rules": {
          "rules": [
            {
              "operation": "contains",
              "value2": "AGENT-ERROR",
              "output": 2
            },
            {
              "operation": "contains",
              "value2": "BDD_ERROR"
            },
            {
              "operation": "contains",
              "value2": "COLLECTOR_ERROR",
              "output": 1
            },
            {
              "operation": "contains",
              "value2": "NO-ERROR",
              "output": 3
            }
          ]
        }      },
     "name": "whatError ?",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 1,
      "position": [
        -90,
        500
      ],
      "notesInFlow": true
    },

Hey,

Can you please check the workflow you shared? It looks incomplete.

A minimalist use case here showing my request, to make it simpler : how to pass ap["active"] value to the first Set node ?

{
  "name": "passVar",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        -20,
        190
      ]
    },
    {
      "parameters": {
        "functionCode": "var ap = {};\nap[\"active\"] = \"vm1\";\nap[\"passive\"] = \"vm2\";\nitems[0].json.outDiag = \"NO-ERROR\";\nreturn items;"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        150,
        190
      ]
    },
    {
      "parameters": {
        "dataType": "string",
        "value1": "={{$node[\"Function\"].json[\"outDiag\"]}}",
        "rules": {
          "rules": [
            {
              "operation": "contains",
              "value2": "NO-ERROR"
            },
            {
              "operation": "contains",
              "value2": "BDD-ERROR",
              "output": 1
            },
            {
              "value2": "SYSTEM-ERROR",
              "output": 2
            }
          ]
        }
      },
      "name": "Switch",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 1,
      "position": [
        350,
        190
      ]
    },
    {
      "parameters": {
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        620,
        50
      ]
    },
    {
      "parameters": {
        "options": {}
      },
      "name": "Set1",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        620,
        200
      ]
    },
    {
      "parameters": {
        "options": {}
      },
      "name": "Set2",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        620,
        340
      ]
    }
  ],
  "connections": {
    "Function": {
      "main": [
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Set1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Set2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        []
      ]
    },
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {},
  "id": "14"
}

I ran the Function node and found out that you have only added outDiag to the items, and as a result, the Function node only gives outDiag as the output.

[
  {        
    "outDiag": "NO-ERROR"      
  }    
]

I have modified the workflow you shared.

{
  "name": "",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "var ap = {};\nap[\"active\"] = \"vm1\";\nap[\"passive\"] = \"vm2\";\nitems[0].json.outDiag = \"NO-ERROR\";\nitems[0].json.ap = ap\nreturn items;"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "dataType": "string",
        "value1": "={{$node[\"Function\"].json[\"outDiag\"]}}",
        "rules": {
          "rules": [
            {
              "operation": "contains",
              "value2": "NO-ERROR"
            },
            {
              "operation": "contains",
              "value2": "vm2",
              "output": 1
            },
            {
              "value2": "SYSTEM-ERROR",
              "output": 2
            }
          ]
        }
      },
      "name": "Switch",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "ap",
              "value": "={{$node[\"Function\"].json[\"ap\"][\"active\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        920,
        160
      ]
    },
    {
      "parameters": {
        "options": {}
      },
      "name": "Set1",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        920,
        310
      ]
    },
    {
      "parameters": {
        "options": {}
      },
      "name": "Set2",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        920,
        450
      ]
    }
  ],
  "connections": {
    "Function": {
      "main": [
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Set1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Set2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {}
}

Hope this helps. :slightly_smiling_face:

1 Like

Of course harshil, by referencing any node output in whole WF …
Sorry it’s not a yet a reflex :no_mouth: thanks !