# Using switch node

**URL:** <https://discourse.nodered.org/t/using-switch-node/83757>\
**Category:** General\
**Created:** [16 December 2023 10:36 UTC](https://discourse.nodered.org/t/using-switch-node/83757 "2023-12-16T10:36:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sirhc](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@Sirhc](https://discourse.nodered.org/u/Sirhc)\
**Post date:** [16 December 2023 10:36 UTC](https://discourse.nodered.org/t/using-switch-node/83757/1 "2023-12-16T10:36:35Z")

</div>

Hello

I am trying to implement some logic in my flow, to trigger one of two nodes based on the payload.

I am not sure which node to use for this. Basically the logic is:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/e/de01ec0dd44bde12aa5dc077162f64b981ce949f.png)

```auto
var payloadmsg = "";
if (msg.payload.state_l4 != undefined) {
payloadmsg = { state_l3: "ON" }; // TRIGGER NODE #1 (AND NOT NODE #2)

} else {
payloadmsg = { state_l4: "ON" }; // TRIGGER NODE #2 (AND NOT NODE #1)
}

msg.payload = payloadmsg;

return msg;

```

What I need to do, is trigger a subsequent node if msg.payload.state\_I4 is undefined, and another node if it is defined (shown as node 1 and 2 in the illustration)

The function node only has one output, so I can't use that. What node should I use for this?

---

<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:** [16 December 2023 10:40 UTC](https://discourse.nodered.org/t/using-switch-node/83757/2 "2023-12-16T10:40:22Z")

</div>

You can add outputs to the function node in the setup tab, and can send messages to each output, all documented [Writing Functions : Node-RED](https://nodered.org/docs/user-guide/writing-functions)

Something like this should work, after you add the outputs

```auto
let output = [];
let index;
let payloadmsg = "";
if (msg.payload.state_l4 != undefined) {
payloadmsg = { state_l3: "ON" }; // TRIGGER NODE #1 (AND NOT NODE #2)
index = 0;
} else {
payloadmsg = { state_l4: "ON" }; // TRIGGER NODE #2 (AND NOT NODE #1)
index = 1;
}

msg.payload = payloadmsg;
output[index] = msg;

return output;

```

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [16 December 2023 10:49 UTC](https://discourse.nodered.org/t/using-switch-node/83757/3 "2023-12-16T10:49:31Z")

</div>

And remember to not “recycle” the same `msg` if sending to multiple outputs at the same time.  
Create a new object for each, or at least derived from the incoming `msg`

This often fools people, as the `msg` is an object, so if you send to output 1, change it, then to output 2 - output 1 will see the changes - which you likely don't want

```auto
const Output1 = {
   ...msg
}

/* Manipulate Output1 */

const Output2 = {
   ...msg
}

/* Manipulate Output2 */

return [Output1, Output2]

/* Only Output1 */
// return [Output1, undefined]

```

---

<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:** [16 December 2023 11:06 UTC](https://discourse.nodered.org/t/using-switch-node/83757/4 "2023-12-16T11:06:02Z")

</div>

What's wrong with the switch node?  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/d/9d7ac73068c220c9129a1c6021878dc8b9fe391b.png)

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [16 December 2023 11:07 UTC](https://discourse.nodered.org/t/using-switch-node/83757/5 "2023-12-16T11:07:54Z")

</div>

Nothing, not a dam thing! 😆

---

<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:** [16 December 2023 11:40 UTC](https://discourse.nodered.org/t/using-switch-node/83757/6 "2023-12-16T11:40:59Z")

</div>

> [@marcus-j-davies](#):
>
> remember to not “recycle” the same `msg` if sending to multiple outputs at the same time.

Though this is not an issue in this case as it is only required to send to 1 output and not the other.

---

<div class="post-metadata">

**Author:** ![Sirhc](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@Sirhc](https://discourse.nodered.org/u/Sirhc)\
**Post date:** [17 December 2023 08:03 UTC](https://discourse.nodered.org/t/using-switch-node/83757/7 "2023-12-17T08:03:34Z")

</div>

Thanks heaps - the dual function node did the trick

---

<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:** [15 February 2024 08:04 UTC](https://discourse.nodered.org/t/using-switch-node/83757/8 "2024-02-15T08:04:01Z")

</div>

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