notify on public ip change

This flow remembers (by saving it to a json file) your IP between flow restart or OS reboots so you only get notified when the IP Actually changes. Customize the file name payload field or use the default. Your choice. If the file does not exist it will create it. Add your notification flavor at the New IP debug node at the far right; but before the last 3 functions. (email, twitter etc). Be sure to read the comments in the flow for more details. Hope this inspires!

[{"id":"efe2d8e2.101d28","type":"json","name":"","x":2103.999855041504,"y":197.00000762939453,"z":"54c91512.ab36ec","wires":[["aa452094.55bae"]]},{"id":"aa452094.55bae","type":"file","name":"writeFile","filename":"","appendNewline":false,"overwriteFile":true,"x":2231.9999771118164,"y":197.00000762939453,"z":"54c91512.ab36ec","wires":[]},{"id":"ea07170c.15f8e8","type":"file in","name":"readFile","filename":"","format":"utf8","x":657.2503623962402,"y":170.00008392333984,"z":"54c91512.ab36ec","wires":[["879674f6.786988"]]},{"id":"cd20464d.32dfb8","type":"inject","name":"","topic":"filename","payload":"","payloadType":"string","repeat":"1440","crontab":"","once":false,"x":111.00000762939453,"y":174.99974060058594,"z":"54c91512.ab36ec","wires":[["da603295.259fd"]]},{"id":"da603295.259fd","type":"change","action":"replace","property":"filename","from":"","to":"msg.payload","reg":false,"name":"filename","x":233.00000762939453,"y":175.99964904785156,"z":"54c91512.ab36ec","wires":[["e1d4e355.1e2b2"]]},{"id":"9791b6c5.686e48","type":"change","action":"replace","property":"filename","from":"","to":"myPublicIp.json","reg":false,"name":"myPublicIp.json","x":501.0000228881836,"y":209.9998321533203,"z":"54c91512.ab36ec","wires":[["ea07170c.15f8e8"]]},{"id":"e1d4e355.1e2b2","type":"switch","name":"","property":"filename.length","rules":[{"t":"gt","v":0,"v2":0},{"t":"else"}],"checkall":"false","outputs":2,"x":356.0000228881836,"y":176.99961853027344,"z":"54c91512.ab36ec","wires":[["ea07170c.15f8e8"],["9791b6c5.686e48"]]},{"id":"879674f6.786988","type":"switch","name":"","property":"payload.length","rules":[{"t":"gt","v":0,"v2":0},{"t":"else"}],"checkall":"true","outputs":2,"x":778.0000076293945,"y":169.99977111816406,"z":"54c91512.ab36ec","wires":[["28508baa.d7af74"],["9e9c4988.6163b8"]]},{"id":"38cdd923.c73226","type":"change","action":"replace","property":"lastip","from":"","to":"msg.payload","reg":false,"name":"last ip from file","x":1063.0000381469727,"y":136.99964904785156,"z":"54c91512.ab36ec","wires":[["18dba7fa.e72458"]]},{"id":"867b0155.7985","type":"debug","name":"Stable IP","active":false,"console":"false","complete":"false","x":2023.6942520141602,"y":139.47185707092285,"z":"54c91512.ab36ec","wires":[]},{"id":"2171d65a.de8e2a","type":"function","name":"Compare IP","func":"var lastip = msg.lastip.ip;\nvar currentip = msg.payload.ip;\nmsg.currentip = currentip;\nmsg.topic = \"stable\";\nif (lastip !== currentip) {\nmsg.payload.ip = currentip;\nmsg.topic = \"new\";\n}\n\nreturn msg;\n","outputs":"1","x":1554.249885559082,"y":195.90872192382812,"z":"54c91512.ab36ec","wires":[["7fa361a4.805ca"]]},{"id":"28508baa.d7af74","type":"json","name":"","x":920.0000534057617,"y":136.99964904785156,"z":"54c91512.ab36ec","wires":[["38cdd923.c73226"]]},{"id":"7fa361a4.805ca","type":"template","name":"Description","template":"Your {{topic}} Public IP: http://{{payload.ip}}","x":1698.9999160766602,"y":194.90869903564453,"z":"54c91512.ab36ec","wires":[["9d009b0d.62ff68"]]},{"id":"9d009b0d.62ff68","type":"switch","name":"","property":"topic","rules":[{"t":"eq","v":"stable"},{"t":"else"}],"checkall":"false","outputs":2,"x":1824.9999160766602,"y":193.9996109008789,"z":"54c91512.ab36ec","wires":[["867b0155.7985"],["a0961f69.5f69e","89e2ba80.761d48","1f43f075.e0bc1","6b234ee.f94dcb"]]},{"id":"a0961f69.5f69e","type":"function","name":"prepare","func":"var newFile = {};\nnewFile.filename = msg.filename;\nnewFile.payload = {ip: msg.currentip};\n\nreturn newFile;","outputs":"1","x":1983.000099182129,"y":197.9996109008789,"z":"54c91512.ab36ec","wires":[["efe2d8e2.101d28"]]},{"id":"9e9c4988.6163b8","type":"change","action":"replace","property":"lastip","from":"","to":"null","reg":false,"name":"null","x":922.0000610351562,"y":198.2496566772461,"z":"54c91512.ab36ec","wires":[["18dba7fa.e72458"]]},{"id":"89e2ba80.761d48","type":"debug","name":"New IP","active":false,"console":"false","complete":"false","x":2023,"y":254.99964046478271,"z":"54c91512.ab36ec","wires":[]},{"id":"18dba7fa.e72458","type":"http request","name":"Server","method":"GET","url":"http://ipecho.net/plain","x":1244.0000381469727,"y":196.99964904785156,"z":"54c91512.ab36ec","wires":[["4e6deebe.b1921"]]},{"id":"4e6deebe.b1921","type":"function","name":"payload.ip","func":"var regObj = new RegExp(\"(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9]).){3}(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\");\nvar str = msg.payload;\nvar myIp = {};\nmyIp = str.match(regObj);\nmsg.payload = {ip : myIp[0]};\nreturn msg;","outputs":1,"x":1400.0001602172852,"y":195.99964141845703,"z":"54c91512.ab36ec","wires":[["2171d65a.de8e2a"]]},{"id":"f93b2aaf.06c4d8","type":"comment","name":"Filename","info":"use the \"payload\" property to define a custom file name to hold\nthe public Ip address.\nThis flow uses the json format and will create a file when\nit is not found or when the public ip address changes.","x":112.27273559570312,"y":125.27236938476562,"z":"54c91512.ab36ec","wires":[]},{"id":"e2387874.1dc788","type":"comment","name":"Is there a filename defined?","info":"If it is blank, inject the default file name into the flow.","x":414.77281951904297,"y":135.27239990234375,"z":"54c91512.ab36ec","wires":[]},{"id":"84b5bfa8.7b4a4","type":"comment","name":"Valid file?","info":"By checking the length of the payload we can determine if\nthe file exists or not.","x":776.022762298584,"y":125.27236080169678,"z":"54c91512.ab36ec","wires":[]},{"id":"baa85fda.4557a","type":"comment","name":"New file check","info":"If the file does not exist, set the lastip property\nto something that will not match when we check a service for\nour public ip.","x":936.022762298584,"y":242.77236366271973,"z":"54c91512.ab36ec","wires":[]},{"id":"76a85c87.8957a4","type":"comment","name":"good file","info":"convert the string to json and move it to the\nmsg.lastip property","x":919.7727661132812,"y":91.52236080169678,"z":"54c91512.ab36ec","wires":[]},{"id":"c1b31971.3e4ce8","type":"comment","name":"Check IP","info":"Use a web service to check your ip address.\nThen use a function to extract the ip from the result.\nTested with these Services:\nhttp://checkip.dyndns.org/\nhttp://echoip.com/\nhttp://ipecho.net/plain\nhttp://bot.whatismyipaddress.com/\n","x":1256.022705078125,"y":146.02236938476562,"z":"54c91512.ab36ec","wires":[]},{"id":"2ae8338b.d517cc","type":"comment","name":"Compare","info":"compare the results from the WEB with file","x":1548.5227661132812,"y":244.02236938476562,"z":"54c91512.ab36ec","wires":[]},{"id":"cfea625d.3015a","type":"comment","name":"Text Description","info":"Add some words around the result to tell you whats going on.","x":1694.772720336914,"y":149.0223617553711,"z":"54c91512.ab36ec","wires":[]},{"id":"b9a0c538.465f38","type":"comment","name":"Results","info":"The results will flow out the top if they are unchanged\nor flow out the bottom when they have changed.\nwe also write that to our file for next time.\nThis would be the ideal place to email or tweet on change.","x":1822.2727241516113,"y":244.02237033843994,"z":"54c91512.ab36ec","wires":[]},{"id":"ef989b74.106768","type":"comment","name":"No?","info":"Then use the default defined here.","x":487.2727355957031,"y":251.52234649658203,"z":"54c91512.ab36ec","wires":[]},{"id":"245971d4.dba68e","type":"comment","name":"Save if different","info":"This ensures we remember the ip thru power cycles ;-)","x":2199.7727279663086,"y":252.77237033843994,"z":"54c91512.ab36ec","wires":[]}]
johngeffe

Flow Info

created 3 years ago

Node Types

Core
  • change (x4)
  • comment (x11)
  • debug (x2)
  • file (x1)
  • file in (x1)
  • function (x3)
  • http request (x1)
  • inject (x1)
  • json (x2)
  • switch (x3)
  • template (x1)

Tags

  • IP
  • address
  • network
  • alert
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option