How to do "and" function in node red

In node red when multiple pallets are connected to a trigger any of them would execute the trigger. For example if one has two inject node, inject node one OR inject node two would turn on the trigger. Now how can I go about executing the trigger when all the input node inject on message. For example inject node one AND inject node two send on message for trigger to execute?

Any help would be appreciated.

You can use the function node

There was a big learning curve but it much worth it. I am using function node, with global get/set and it just makes everything run so smoothly. :slight_smile:

1 Like

This is very cool, very impressive.

There are two common ways to do this.

The simple way is with the join node. You can configure it to combine multiple messages into one. For example, if you set it up in manual mode to join two messages, they could come from inject 1 and inject 2. However, without doing something more complex, it would also join two messages arising from clicking inject 1 twice.

The more complex way is with a function node. This runs on every individual message received, and needs to decide whether to pass on the message (return msg;) or ignore it (return null;). To achieve what you want it needs some way of storing one message until the second one arrives, and this is commonly done using global or flow context (flow.set("m1", msg);). Then you write script that identifies message2 has arrived, and message1 was already received (flow.get("m1") is not null), so you pass on the “combined” message (whatever that means in your context), and then reset message1 (flow.set("m1", null);).

In both cases you may need to cater for all the possible sequences (eg. m1, m2, m1, m2; or m1, m1, m1, m2; vice versa; etc.) depending on your situation.