# Sub Pub loop... :\<

**URL:** <https://discourse.nodered.org/t/sub-pub-loop/44281>\
**Category:** General\
**Created:** [16 April 2021 12:43 UTC](https://discourse.nodered.org/t/sub-pub-loop/44281 "2021-04-16T12:43:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ThisGuiii](https://avatars.discourse-cdn.com/v4/letter/t/8e8cbc/32.png) [@ThisGuiii](https://discourse.nodered.org/u/ThisGuiii)\
**Post date:** [16 April 2021 12:43 UTC](https://discourse.nodered.org/t/sub-pub-loop/44281/1 "2021-04-16T12:43:21Z")

</div>

Hello,

I am fairly new to Node-RED and have been stuck on this one issue going into week two here.. I have an ESP8266 controlling a relay and a touch switch that also controls the relay. Simple as this seems, when I toggle the power on from the UI or the switch I drop into a loop and I cannot figure out how to circumvent this..

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

My flow if this helps

```auto
 [{"id":"c4c9280f.8a10c8","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"43882981.be16a8","type":"debug","z":"c4c9280f.8a10c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":890,"y":360,"wires":[]},{"id":"b681cd55.407de","type":"mqtt out","z":"c4c9280f.8a10c8","name":"SUB Output - masterbed/light/state","topic":"masterbed/light/state","qos":"2","retain":"","broker":"9b92d4c7.9761f8","x":1040,"y":460,"wires":[]},{"id":"7a84ae6a.775c1","type":"ui_switch","z":"c4c9280f.8a10c8","name":"","label":"Light Switch","tooltip":"","group":"5772cf6f.359d1","order":3,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","topicType":"str","style":"","onvalue":"1","onvalueType":"str","onicon":"","oncolor":"","offvalue":"0","offvalueType":"str","officon":"","offcolor":"","animate":true,"x":670,"y":460,"wires":[["43882981.be16a8","b681cd55.407de"]]},{"id":"912c69cc.6aa638","type":"mqtt in","z":"c4c9280f.8a10c8","name":"PUB Input - masterbed/light/state","topic":"masterbed/light/state","qos":"2","datatype":"auto","broker":"9b92d4c7.9761f8","x":310,"y":460,"wires":[["7a84ae6a.775c1"]]},{"id":"3f860de6.dd4152","type":"inject","z":"c4c9280f.8a10c8","name":"Off","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"masterbed/light/state","payload":"1","payloadType":"str","x":430,"y":640,"wires":[["7a84ae6a.775c1"]]},{"id":"924c12de.adee8","type":"inject","z":"c4c9280f.8a10c8","name":"On","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"masterbed/light/state","payload":"0","payloadType":"str","x":430,"y":580,"wires":[["7a84ae6a.775c1"]]},{"id":"9b92d4c7.9761f8","type":"mqtt-broker","name":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"5772cf6f.359d1","type":"ui_group","name":"Bedroom Light","tab":"6e47d42d.482f1c","order":1,"disp":true,"width":"6","collapse":false},{"id":"6e47d42d.482f1c","type":"ui_tab","name":"Tab 1","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

```

---

<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 April 2021 13:04 UTC](https://discourse.nodered.org/t/sub-pub-loop/44281/2 "2021-04-16T13:04:03Z")

</div>

Welcome to the forum @ThisGuiii

In the switch clear the Message Pass Through checkbox, and set it Show State of Input.

---

<div class="post-metadata">

**Author:** ![LosinIt](https://avatars.discourse-cdn.com/v4/letter/l/439d5e/32.png) [@LosinIt](https://discourse.nodered.org/u/LosinIt)\
**Post date:** [16 April 2021 19:10 UTC](https://discourse.nodered.org/t/sub-pub-loop/44281/3 "2021-04-16T19:10:44Z")

</div>

I chose to put similar logic into the ESP32 I'm using instead of NR. If you want to go that route I'll share the code with you.

---

<div class="post-metadata">

**Author:** ![ThisGuiii](https://avatars.discourse-cdn.com/v4/letter/t/8e8cbc/32.png) [@ThisGuiii](https://discourse.nodered.org/u/ThisGuiii)\
**Post date:** [17 April 2021 12:36 UTC](https://discourse.nodered.org/t/sub-pub-loop/44281/4 "2021-04-17T12:36:53Z")

</div>

I have sunk so much time into this one snag, worn out three pages deep on google searches many times..  
That did the trick! Thank you so much!

---

<div class="post-metadata">

**Author:** ![ThisGuiii](https://avatars.discourse-cdn.com/v4/letter/t/8e8cbc/32.png) [@ThisGuiii](https://discourse.nodered.org/u/ThisGuiii)\
**Post date:** [17 April 2021 12:40 UTC](https://discourse.nodered.org/t/sub-pub-loop/44281/5 "2021-04-17T12:40:48Z")

</div>

Hey LosinIt,

If you wouldn't mind, I'd like to see what code you have in production.

---

<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:** [16 June 2021 12:41 UTC](https://discourse.nodered.org/t/sub-pub-loop/44281/6 "2021-06-16T12:41:39Z")

</div>

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