# Two switches into one function node. How to detect if both switches are sending a true signal?

**URL:** <https://discourse.nodered.org/t/two-switches-into-one-function-node-how-to-detect-if-both-switches-are-sending-a-true-signal/61809>\
**Category:** General\
**Tags:** function-node\
**Created:** [26 April 2022 15:26 UTC](https://discourse.nodered.org/t/two-switches-into-one-function-node-how-to-detect-if-both-switches-are-sending-a-true-signal/61809 "2022-04-26T15:26:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ViktorVante](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/viktorvante/32/60660_2.png) [@ViktorVante](https://discourse.nodered.org/u/ViktorVante)\
**Post date:** [26 April 2022 15:26 UTC](https://discourse.nodered.org/t/two-switches-into-one-function-node-how-to-detect-if-both-switches-are-sending-a-true-signal/61809/1 "2022-04-26T15:26:01Z")

</div>

I have two switches connected to a function node. I want this node to send a true signal once both switches are sending "true" to the function block. Otherwise I don't want the function node to do anything. What would the code look like?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [26 April 2022 16:01 UTC](https://discourse.nodered.org/t/two-switches-into-one-function-node-how-to-detect-if-both-switches-are-sending-a-true-signal/61809/2 "2022-04-26T16:01:12Z")

</div>

See [this article in the cookbook](https://cookbook.nodered.org/basic/join-streams) for an example of how to join messages into one object.

Once you have both values in the message, you can check both to see if they are true.

e.g.

```javascript
if (msg.payload.sw1 == true && msg.payload.sw2 == true) {
  return msg; //pass message on to next node
}
return null;//dont pass message
```

---

<div class="post-metadata">

**Author:** ![ViktorVante](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/viktorvante/32/60660_2.png) [@ViktorVante](https://discourse.nodered.org/u/ViktorVante)\
**Post date:** [26 April 2022 16:09 UTC](https://discourse.nodered.org/t/two-switches-into-one-function-node-how-to-detect-if-both-switches-are-sending-a-true-signal/61809/3 "2022-04-26T16:09:38Z")

</div>

The code didn't work and cookbook guides are too advanced for me at this point in time.

I think I need to change msg.topic, so I'm assuming sw1 and sw2 are the topics. How do I changed msg.topic? Do I simply remove "topic" and put sw1 or sw2 in its place?

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [26 April 2022 16:49 UTC](https://discourse.nodered.org/t/two-switches-into-one-function-node-how-to-detect-if-both-switches-are-sending-a-true-signal/61809/4 "2022-04-26T16:49:06Z")

</div>

If you save & update each switch-state in context variables, you can always make the comparison when a message arrives (if you dont keep that statuses saved you do not know the state of the other switch when a message arrives, unless you as @Steve-Mcl mentioned, combine them into a single message)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [26 April 2022 18:00 UTC](https://discourse.nodered.org/t/two-switches-into-one-function-node-how-to-detect-if-both-switches-are-sending-a-true-signal/61809/5 "2022-04-26T18:00:29Z")

</div>

> [@ViktorVante](#):
>
> cookbook guides are too advanced for me at this point in time

In which case ...

I recommend watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

---

<div class="post-metadata">

**Author:** ![ViktorVante](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/viktorvante/32/60660_2.png) [@ViktorVante](https://discourse.nodered.org/u/ViktorVante)\
**Post date:** [27 April 2022 17:43 UTC](https://discourse.nodered.org/t/two-switches-into-one-function-node-how-to-detect-if-both-switches-are-sending-a-true-signal/61809/6 "2022-04-27T17:43:29Z")

</div>

This is great, thank you!

---

<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:** [26 June 2022 17:44 UTC](https://discourse.nodered.org/t/two-switches-into-one-function-node-how-to-detect-if-both-switches-are-sending-a-true-signal/61809/7 "2022-06-26T17:44:27Z")

</div>

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