Re: [node-red] how to identify if there is a msg.payload help

3,123 views
Skip to first unread message

Nicholas O'Leary

unread,
Jul 8, 2016, 11:01:01 AM7/8/16
to Node-RED

It depends exactly what no payload looks like. It could be that msg.payload exists, but has a null value... in which case:

    if (msg.payload === null)

Or if there is no payload property at all...

    if (!msg.hasOwnProperty('payload'))

Nick


On Fri, 8 Jul 2016, 14:50 , <dave....@gmail.com> wrote:
I have a function node that is being triggered each second it sends the time to return [msg1,....; 
In the same function I want to process a message.payload that only comes into the function node occasionally

But how to identify when there is a msg.payload because when there is no payload I get unidentified, I need something like   

if(there is a msg.payload){
    var parts = msg.payload.split(",");
    etc
    msg1.payload = whatever
}
else{
    var d = new Date();
    etc
    msg1.payload = (h+":"+m+":"+s);
 
return [msg1, ..........

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
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.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
For more options, visit https://groups.google.com/d/optout.

dave....@gmail.com

unread,
Jul 8, 2016, 11:56:12 AM7/8/16
to Node-RED


As is the case sometimes after you do a post then reread it a light bulb comes, I just tried    if (msg.payload){    and changed things around a bit and it worked, so as no one had read my post I deleted it

but thanks anyway, I will try you suggestion.

I am now on my next task and scratching my head again, how to write a simple function that when a inject node is pressed the output from the function node starts at zero and increments by 1 at each press up to 10 then returns to 0 without using a context.global var, is it possable ?


(I feel I have fallen into a trap recently were every variable needs to be context.global I mean is there a limit /overhead in using context global.variables) ?

Dave C-J

unread,
Jul 8, 2016, 12:27:11 PM7/8/16
to node...@googlegroups.com
as well as context.global there is a local context and flow level one...   
Local is just inside that function... so is the one you want for counters and the like.

the only limits are down to memory on your system... and name clashes... (hence use local for in function stuff and then you can use context.count in several functions) 

and no - I can't think of a way of doing a count without keeping any context. (it was one of the reasons we added it in the first place !)

Julian Knight

unread,
Jul 8, 2016, 5:25:52 PM7/8/16
to Node-RED
Is it possible to do simple math in the change Node Dave? I vaguely remember that being talked about at one point. Just thinking that it seems a shame to have to write a function node if all you want to do is increment/decrement a flow/global variable.

Nicholas O'Leary

unread,
Jul 8, 2016, 5:27:16 PM7/8/16
to Node-RED

It isn't currently possible, but as you said, something we've got on the list for a future release.

Nick


--

Julian Knight

unread,
Jul 8, 2016, 5:45:27 PM7/8/16
to Node-RED
Cheers, Nick.

Walter Kraembring

unread,
Jul 9, 2016, 2:09:09 PM7/9/16
to Node-RED
I am now on my next task and scratching my head again...

This is a simple sample that you can put into a function node:

// initialise the variable if it doesn't exist already
context.set('tick',context.get('tick')||0);
var target = 10;

msg.payload = context.get('tick'); 
node.send(msg);

node.status({
fill : "yellow",
shape : "ring",
text : "Counter: "+context.get('tick')+" Target: "+target
});

context.set('tick',context.get('tick')+1);

if( context.get('tick') > target) {
    context.set('tick',0);
}



dave....@gmail.com

unread,
Jul 10, 2016, 3:49:56 AM7/10/16
to Node-RED
Thanks Walter that simple sample is nice, I know its basically whats in the documentation but its much simpler to understand.

I built on this with the result, I now have a Up Down counter and no more head scratching (until my next task)

// initialise the variable if it doesn't exist already
context.set('gh_inc',context.get('gh_inc')||0);
// Count Up
if(msg.payload==1){
    context.set('gh_inc',context.get('gh_inc')+1);
}
if( context.get('gh_inc') > 12) {
    context.set('gh_inc',0);
}
// Count Down
if(msg.payload==-1){
    context.set('gh_inc',context.get('gh_inc')-1);
}
if( context.get('gh_inc') < 0) {
    context.set('gh_inc',12);
}
node.status({
fill : "yellow",
shape : "ring",
text : "Counter: "+context.get('gh_inc')
});
msg.payload = context.get('gh_inc'); 
node.send(msg);

Dave C-J

unread,
Jul 10, 2016, 4:01:08 AM7/10/16
to node...@googlegroups.com
at the end of a function you generally just need to    
   return msg;
(or return null; to exit without sending anything.) 
rather than node.send(msg); . You only need that if you need to send data asynchronously within a callback within your function, or if you need to output multiple values from within a loop.

dave....@gmail.com

unread,
Jul 10, 2016, 4:54:34 AM7/10/16
to Node-RED
ok noted, thanks
Reply all
Reply to author
Forward
0 new messages