Abort flow if msg.payload doesn't exist

680 views
Skip to first unread message

Paul Reed

unread,
Jul 8, 2014, 6:53:39 AM7/8/14
to node...@googlegroups.com
I've written a flow that sends me a push message if my home network IP address changes (my provider has dynamic IP's!). Which is pretty useful for me at the moment until I sort out my domain, and also helps me with some networking problems.
It basically calls ipecho.com every 15 minutes, and compares the returned IP address with the previous one held in NodeRed (context.lastip).
Before I post it in Flows, I wanted to sort out a bug, where if ipecho.com fails to answer, it treats the blank msg.payload as being different to the last held IP address and sends an alert.

Can anyone suggest a very simple validity check, i.e 

'If msg.payload is blank, then abort the flow.'

Paul

Paul Reed

unread,
Jul 8, 2014, 6:57:11 AM7/8/14
to node...@googlegroups.com
Sorry, the ipecho url is ipecho.net/plain not ipecho.com (too much coffee!!)

Paul

Nicholas O'Leary

unread,
Jul 8, 2014, 7:00:31 AM7/8/14
to node...@googlegroups.com

Hi Paul,

You can use the Switch node to test the value of msg.payload and only pass it on if not null/blank.

Nick

--
http://nodered.org
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Paul Reed

unread,
Jul 8, 2014, 6:04:05 PM7/8/14
to node...@googlegroups.com
Thanks Nick.
First time I've used 'Switch' and it's not such a simple little node as it's title suggests!
Pattern matching via Regex makes it easy to not just reject 'null's', but to positively filter IP addresses instead, which is infinitely better.

Paul

Eugen Kammerloher

unread,
Jul 9, 2014, 2:39:55 PM7/9/14
to node...@googlegroups.com
Hi Paul,

I have the exact same situation.

I parse the Ip in a javascript node with this code:

context.lastIP = context.lastIP || 'first_run';
var ip = msg.payload.trim();
var ipformat = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
var msg1 = null;

if (ip.match(ipformat))
{
if (context.lastIP == 'first_run')
{
context.lastIP = ip;
}
if (context.lastIP != ip)
{
msg1 = {payload: ip, topic: "New IP obtained"};
}
}
else
{
msg1 = {payload: ip, topic: "Problems obtaining external IP"};
}
return msg1;

Paul Reed

unread,
Jul 9, 2014, 5:14:45 PM7/9/14
to node...@googlegroups.com
Hi Eugen, thanks for the reply.
Our codes are very similar except I've used the switch node to contain the regex filter instead of building it into the javascript node, so your solution is probably a better option if you are familiar with javascript.

Paul
Reply all
Reply to author
Forward
0 new messages