# Two way switch infinite loop

**URL:** <https://discourse.nodered.org/t/two-way-switch-infinite-loop/67115>\
**Category:** General\
**Tags:** function-node, mqtt\
**Created:** [30 August 2022 09:24 UTC](https://discourse.nodered.org/t/two-way-switch-infinite-loop/67115 "2022-08-30T09:24:18Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![petertmooney](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/petertmooney/32/67093_2.png) [@petertmooney](https://discourse.nodered.org/u/petertmooney)\
**Post date:** [2 September 2022 15:32 UTC](https://discourse.nodered.org/t/two-way-switch-infinite-loop/67115/21 "2022-09-02T15:32:36Z")

</div>

The varnewmsg bit is meant to change to on or off to send on the the next devices. The Mqtt ones work fine but the entity.landing\_lamp doesn’t. And I’m not sure if I am writing it wrong. Sorry on mobile atm.

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [2 September 2022 16:33 UTC](https://discourse.nodered.org/t/two-way-switch-infinite-loop/67115/22 "2022-09-02T16:33:21Z")

</div>

As written **newMsg** will allways have a topic of Landing Lamp

---

<div class="post-metadata">

**Author:** ![petertmooney](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/petertmooney/32/67093_2.png) [@petertmooney](https://discourse.nodered.org/u/petertmooney)\
**Post date:** [2 September 2022 16:49 UTC](https://discourse.nodered.org/t/two-way-switch-infinite-loop/67115/23 "2022-09-02T16:49:12Z")

</div>

How could I get it to have the state? Eg. On or off?

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [3 September 2022 01:01 UTC](https://discourse.nodered.org/t/two-way-switch-infinite-loop/67115/24 "2022-09-03T01:01:46Z")

</div>

It's hard to understand exactly what you are trying to do here. Don't worry we all had to start somewhere 😉

Perhaps you could share more of the flow and a description of what you want this function to do, even a sketch might help !

Looking at your function -

You don't seem to be using the incoming topics as they are commented out in the logic ?

You don't seem to be using topics on the outgoing messages either, as there are 3 MQTT OUT nodes which already have the **topic set.** This makes this code redundant, and you cannot define 3 values to the same variable like this anyway as it will always be the last value you set as I pointed out.

```auto
var newMsg = { topic: "zigbee2mqtt/Stairs Switch/state_l2"}; 
var newMsg = { topic: "zigbee2mqtt/Landing Switch/state_l2" };
var newMsg = { topic: "Landing Lamp"};

```

To send to the topic you define you need to leave topic blank in the MQTT node, and then only have 1 node.  
You can set payload and topic within your if statments like this

```auto
var newMsg = { topic: "zigbee2mqtt/Stairs Switch/state_l2", payload: msg.payload } ;

```

As it stand if any of the 3 MQTT topics sends an On or Off payload, the function will stop at the first matching condition - eg **if (landingstairs === 0)** it will then send an ON payload to all three outgoing topics.  
I'm **guessing** that all three will then send a new message back into the function, and after several iterations it may stop if there are no more matches ?

Regarding **landinglamp** there is no outgoing topic for that in the flow you shared.

[Previous page](https://discourse.nodered.org/t/two-way-switch-infinite-loop/67115.md?page=1)
