Node-RED function: multiple inputs to a single payload

Hi,

I’ve created a function that has two inputs. I would like the resulting payload (debug 12) to have one value, however, it seems to have two values or two separate messages. (I assume, one from each input payload).

I want the newMsg payload to be iCount - which should currently be 3.

var iCount = 0;

if (msg.topic == "time_range") {
    if( msg.payload == true ){
        iCount = iCount + 1;
    }    
}

if (msg.topic == "is_dark") {
    if(msg.payload == true) {
        iCount = iCount + 2; 
    }    
}

var newMsg = { payload: iCount, topic: "result" };

return newMsg;

What am I doing wrong?

Thank you in advance.

W.

When the door button is pushed, it sends a msg to Current time → Time Range → function 1. function 1 sets iCount to 0 and hits the first if statement which changes iCount to 1 and it is sent to debug.

But the door button also sends a msg to Is Dark → Is Dark (function node) → function 1. function 1 again sets iCount to 0 and hits the second if statement which changes iCount to 2 and it is sent to debug.

Not sure what you are trying to accomplish so I can’t tell you how to fix it. If you’re wanting to keep a running count, maybe a flow variable or a counter node.

Thank you @SigP. I see my mistake.

My aim is to create a flow that will turn on one light when the door is opened and it’s dark, but if the door is opened between 23:30 and 08:00 (and it’s dark) it turns on several lights.

Thanks again.

For anyone interested, here is my finished flow:

I added a “join” node with the following settings:

And function 1 contains the following code:

if (msg.payload.time_range == true && msg.payload.is_dark == true){
    iCount = iCount + 1;
    return [msg,null];
}

if (msg.payload.time_range != true && msg.payload.is_dark == true){
    iCount = iCount + 2;
    return [null,msg];
}

It has two outputs.

HTH.

W.

Really nice interview – you can share the code at the end

Thanks :slight_smile: