[SOLVED]Trouble with storing and retrieving IP data

Hello,
I am trying to make a flow that will email me when my IP changes on a pi I have at another property.
I am using the nodered-contrib-IP object.
My default IP gets added to lastIp but it never gets updated via the flow.set function and the "My current IP" gets returned every time.
Function:

var lastIp = flow.get('lastIp')||'192.168.1.0';
var currentIp = msg.payload.publicIPv4;
node.warn(lastIp);
if (lastIp === currentIp) {
    msg.payload = "same IP" + lastIp;
    return msg;
}
else if (lastIp != currentIp) {
    msg.payload = "My current IP is "+currentIp;
flow.set('lastIp', lastIp);
return msg;
}

Here is the flow:

[{"id":"3c107d5a.937752","type":"ip","z":"43e22b7d.f2cf34","name":"ip","https":false,"timeout":"5000","internalIPv4":true,"internalIPv6":false,"publicIPv4":true,"publicIPv6":false,"x":490,"y":560,"wires":[["7b66da7f.387524"]]},{"id":"db2b1954.738d78","type":"debug","z":"43e22b7d.f2cf34","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":830,"y":560,"wires":[]},{"id":"b359d53e.74d828","type":"inject","z":"43e22b7d.f2cf34","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":340,"y":560,"wires":[["3c107d5a.937752"]]},{"id":"7b66da7f.387524","type":"function","z":"43e22b7d.f2cf34","name":"Compare IP","func":"//context.lastip = context.lastip || 'initial';\nvar lastIp = flow.get('lastIp')||'192.168.1.0';\nvar currentIp = msg.payload.publicIPv4;\nnode.warn(lastIp);\n//node.warn(\"help\"+currentIp);\nif (lastIp === currentIp) {\n    msg.payload = \"same IP\" + lastIp;\n    return msg;\n//context.get = currentIp;\n//msg.payload = \"same \"+currentip;\n}\nelse if (lastIp != currentIp) {\n    msg.payload = \"My current IP is \"+currentIp;\nflow.set('lastIp', lastIp);\nreturn msg;\n}","outputs":1,"noerr":0,"x":650,"y":560,"wires":[["db2b1954.738d78"]]}]

Thanks for any input for this rookie!
Cheers,
Jon

Not answering your question, sorry, but is there a reason the IP needs to change? It could potentially be a lot easier using a static ip.

Static IP is not available through the cellular provider.
Cheers,
Jon

Stupid mistake. :slight_smile:
Cheers,
Jon

I see this is solved but I have a flow that does exactly what you are asking. My outside IP stays the same for quite a while then will change. I wanted to know when it did.

It's a good idea to show what the mistake is so others will know (set the flow variable with the wrong value) and in case they import your flow thinking it is fixed and to fully name the contrib node (node-red-contrib-ip) you are using.

Note also, that msg.payload.publicIPv4 (returned by node-red-contrib-ip) will change when you attach to a router at a new location or your ISP assigns you a new IP, but it does not report it your Pi gets a new IP address assigned by your own router. So, unless you are forcing your Pi to receive a fixed IP address, msg.payload.publicIPv4 could stay the same but msg.payload.internalIPv4 could change.

If you want to make sure that you have the actual address of the Pi and not the router, youshould be using msg.payload.internalIPv4 and might want to check both variables for change (although I just check the internal IP address)

Also have you tested to see what happens if you are using a wired connection for the Pi?

Hi zenofmud,
You make a point. The error was in passing the wrong data.
I do not recommend anyone use this flow as it was just to prove a concept.
I addressed the rest of your concerns once I got it working.

var lastPublicIp = flow.get('lastPublicIp')||'192.168.1.0';
var lastInternalIp = flow.get('lastInternalIp')||'192.168.1.0';
var currentPublicIp = msg.payload.publicIPv4;
var currentInternalIp = msg.payload.internalIPv4;
node.warn(currentPublicIp + ' internal '+ lastInternalIp);
if (lastPublicIp === currentPublicIp && lastInternalIp === currentInternalIp) {
    msg.payload.Ip = "same IP " + currentPublicIp + ' Internal ' + currentInternalIp;
    return msg;
}
else if (lastPublicIp != currentPublicIp || lastInternalIp != currentInternalIp) {
    msg.payload.Ip = "My current IP is "+currentPublicIp + 'internal IP' + currentInternalIp;
flow.set('lastPublicIp', currentPublicIp);
flow.set('lastInternalIp', currentInternalIp);
return msg;
}

I wont be able to fully test it until I get up to the cottage where the cellular router is but it is currently working with a LAN cable and I am hopeful this will work up at the cottage too...
Cheers,
Jon

1 Like

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