Save value between executions

I hope my question is not stupid.

I try to make a “dyndns” workflow.

Trigger node every, say 1 minute → Request node calling https://www.ipify.org/ and passing ip → Request node updating IP on namecheap

this works fine but of course, I want to only update the IP if it has changed.
I added an IF - node but how do I get the value of the ip from the last execution / which has been set?

I tried something like this but the IF condition always evaluates to ‘false’ which is not what it should:

{
  "name": "slydyndns",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        450,
        930
      ]
    },
    {
      "parameters": {
        "interval": 5
      },
      "name": "Interval",
      "type": "n8n-nodes-base.interval",
      "typeVersion": 1,
      "position": [
        860,
        420
      ]
    },
    {
      "parameters": {
        "url": "https://api.ipify.org?format=json",
        "options": {}
      },
      "name": "Get my public IP",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        1150,
        420
      ]
    },
    {
      "parameters": {
        "url": "http://dynamicdns.park-your-domain.com/update",
        "responseFormat": "string",
        "options": {},
        "queryParametersUi": {
          "parameter": [
            {
              "name": "ip",
              "value": "={{$node[\"Get my public IP\"].json[\"ip\"]}}"
            },
            {
              "name": "password",
              "value": "foobar"
            },
            {
              "name": "host",
              "value": "minecraft"
            },
            {
              "name": "domain",
              "value": "example.com"
            }
          ]
        }
      },
      "name": "Update IP on namecheap",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        1700,
        440
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [],
          "string": [
            {
              "value1": "={{$node[\"Get my public IP\"].json[\"ip\"]}}",
              "value2": "={{$node[\"Set\"].parameter[\"values\"][\"string\"][0][\"value\"]}}"
            }
          ]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1390,
        420
      ]
    },
    {
      "parameters": {
        "keepOnlySet": "=true",
        "values": {
          "string": [
            {
              "name": "lastIP",
              "value": "={{$node[\"Function1\"].json[\"lastIP\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        2150,
        440
      ]
    },
    {
      "parameters": {
        "functionCode": "resp = items[0].json.data;\nconst ip = resp.split('<IP>')[1].split('</IP>')[0];\nitems[0].json.lastIP = ip;\n\nreturn items;"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1940,
        440
      ],
      "retryOnFail": true
    }
  ],
  "connections": {
    "Interval": {
      "main": [
        [
          {
            "node": "Get my public IP",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get my public IP": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF": {
      "main": [
        [],
        [
          {
            "node": "Update IP on namecheap",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update IP on namecheap": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function1": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {}
}

So I wonder if my idea to save values between executions is just wrong.

I was wondering, is there something like a ‘wait’ node? Maybe with this I could run the workflow in a loop to have access to the previous value for IP.

I also want to suggest to separate core nodes from “service” nodes in the editor UI for easier discoverability. This will become increasingly problematic with the addition of new (service) nodes.

And one other question: is the documentation for the core nodes currently wip or am I just not able to find it? Not here at least: Code | n8n Docs

Thanks! It’s super fun to play with this! Silvio

So I thought I could use the execute-node and set an env var with the IP address in the shell and then read it back on the next execution but I couldn’t get this to work either.

I don’t know how to read the env var back, process.env.FOO didn’t seem to work in the if-node and several other attempts also failed.

Actually I don’t even know if every run spawns its own shell and therefor my env var is gone on the next execution anyway.

There are multiple possibilities to do that:

  • save values in database
  • save values in file
  • save in static data with the workflow

An example how to do the last can be found here:

Hope that helps!

To your other questions:

I also want to suggest to separate core nodes from “service” nodes in the editor UI for easier discoverability. This will become increasingly problematic with the addition of new (service) nodes.
Ah yes totally agree. We are already planning on improving that in the future.

And one other question: is the documentation for the core nodes currently wip or am I just not able to find it?
Yes that is all still wip. The situation should improve over the next weeks.

Glat do hear that you have fun with n8n!

3 Likes

Just a heads up that we have published community node that allows storing values in different scopes: EXECUTION, WORKFLOW and INSTANCE:

Key/value pairs are put into hashmap that resides in n8n memory and is purged once n8n is restarted.
Functionally we tried to make it similar to “Redis” node, however it does not implement channels.

2 Likes

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