3 switches lock

Hi guys,I want to make 3 switches in which if switch 1 and switch 2 are in true its output will be true,if one of the switch is false the other switch(output) is false.The output switch can only be in true if both of switch 1 and switch 2 are on.I used this function to make it but its still showing false .

if (msg.payload.switch1 && msg.payload.switch2) {
    msg.payload = true;
} else {
    msg.payload = false;
return msg;

Are msg.payload.switch1 & msg.payload.switch2 in the same message, show us the debug of the msg going into the function node, add debugs to the previous node/s.
What are the switches?

NO,its showing 2 messages.

You will need to join the messages here is a link to cookbook example Create a single message from separate streams of messages : Node-RED

1 Like

msg.payload.switch1 does not hang around available to the function node until you need it.
As far as the function knows, a message exists while that node is processing it, then all knowledge of is is gone.

To get around this you either need a Join node to combine the two messages into one or else store one value in a context variable (which does retain it's value over multiple messages).

You are among many many people who have been caught by this surprising feature of flow based programming. If you search the forum for join or context you can find many worked examples to overcome it.

1 Like

Or have a look at node-red-contrib-boolean-logic-ultimate

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.