3

So I'm just getting to grips with node-red and I need to create a conditional global function.

I have two separate global.payloads set to a number value of either 0 or 1.

What I need to happen now is, if global.payload is equal to value 1 then follow this flow, if it is equal to value 0 then follow this one.

I'm just a little confused with the syntax for the function statement. Any help gratefully appreciated.

TomServo
  • 7,248
  • 5
  • 30
  • 47
blupacetek
  • 155
  • 1
  • 9
  • Perhaps my new answer below better reflects what you're trying to accomplish, given your comment that you're working with two separate global payloads. – TomServo Jul 25 '17 at 13:11

2 Answers2

2

Since you haven't accepted the current answer, thought I'd give this a try. I think this is what you need to handle inputs from two separate global contexts. I'm simulating them here with two separate inject nodes to demonstrate:

enter image description here

The checkconf inject node emits a 1 or a 0. Same for the meshstatus node. Substitute your real inputs for those inject nodes. The real work is done inside the function:

var c = context.get('c') || 0;  // initialize variables
var m = context.get('m') || 0;

if (msg.topic == "checkconf")  // update context based on topic of input
{
    c = {payload: msg.payload};
    context.set("c", c);  // save last value in local context
}

if (msg.topic == 'meshstatus') // same here
{
    m = {payload: msg.payload};
    context.set('m', m); // save last value in local context
}

// now do the test to see if both inputs are triggered...
if (m.payload == 1) // check last value of meshstatus first
{
    if (c.payload == 1)  // now check last value of checkconf
        return {topic:'value', payload: "YES"};
}
else
    return {topic:'value', payload: "NO"};

Be sure to set the "topic" property of whatever you use as inputs so the if statements can discriminate between the two input. Good luck!

TomServo
  • 7,248
  • 5
  • 30
  • 47
1

You can use the Switch node to do this, rather than a Function node.

enter image description here

knolleary
  • 9,777
  • 2
  • 28
  • 35
  • Hi thanks for the feedback... What I actually have is 2 global.payloads... one called global.payload(checkconf) and the other called global.payload(meshstatus) I need to evaluate a condition that says, if global.payload(meshstatus) is set to 1, then check if global.payload(checkconf) also equals 1.. if if does then do this, and if not then do this.. i've been trying my brain in knots trying to work this out :-S – blupacetek Jul 23 '17 at 06:59