Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node-red - need a multi-input function for a number value

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.

like image 570
blupacetek Avatar asked Dec 08 '25 11:12

blupacetek


1 Answers

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!

like image 141
TomServo Avatar answered Dec 11 '25 00:12

TomServo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!