# 3 switches lock

**URL:** https://discourse.nodered.org/t/3-switches-lock/76354
**Category:** General
**Tags:** node-red-dashboard, function-node
**Created:** [9 March 2023 15:58 UTC](https://discourse.nodered.org/t/3-switches-lock/76354 "2023-03-09T15:58:45Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![JAY](https://avatars.discourse-cdn.com/v4/letter/j/ed8c4c/32.png) [@JAY](https://discourse.nodered.org/u/JAY)
#### Post date: [9 March 2023 15:58 UTC](https://discourse.nodered.org/t/3-switches-lock/76354/1 "2023-03-09T15:58:45Z")

</div>

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 .

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

```

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [9 March 2023 16:05 UTC](https://discourse.nodered.org/t/3-switches-lock/76354/2 "2023-03-09T16:05:40Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![JAY](https://avatars.discourse-cdn.com/v4/letter/j/ed8c4c/32.png) [@JAY](https://discourse.nodered.org/u/JAY)
#### Post date: [9 March 2023 16:12 UTC](https://discourse.nodered.org/t/3-switches-lock/76354/3 "2023-03-09T16:12:53Z")

</div>

NO,its showing 2 messages.  
 ![1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/d/fd424977c3924cae9961ce3c021f54422d90c21a.png)  
 ![2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/0/70478addc18547d287df8d47b771933ca4506955.png)

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [9 March 2023 16:15 UTC](https://discourse.nodered.org/t/3-switches-lock/76354/4 "2023-03-09T16:15:13Z")

</div>

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](https://cookbook.nodered.org/basic/join-streams)

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [9 March 2023 16:18 UTC](https://discourse.nodered.org/t/3-switches-lock/76354/5 "2023-03-09T16:18:15Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [9 March 2023 16:30 UTC](https://discourse.nodered.org/t/3-switches-lock/76354/6 "2023-03-09T16:30:46Z")

</div>

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

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [8 May 2023 16:31 UTC](https://discourse.nodered.org/t/3-switches-lock/76354/7 "2023-05-08T16:31:46Z")

</div>

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