# MQTT topics creates loop with Serial Port

**URL:** https://discourse.nodered.org/t/mqtt-topics-creates-loop-with-serial-port/96914
**Category:** General
**Tags:** mqtt, node-red-node-serialport
**Created:** [5 May 2025 21:06 UTC](https://discourse.nodered.org/t/mqtt-topics-creates-loop-with-serial-port/96914 "2025-05-05T21:06:03Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![WannabeMaker](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wannabemaker/32/100599_2.png) [@WannabeMaker](https://discourse.nodered.org/u/WannabeMaker)
#### Post date: [5 May 2025 21:06 UTC](https://discourse.nodered.org/t/mqtt-topics-creates-loop-with-serial-port/96914/1 "2025-05-05T21:06:03Z")

</div>

Hello,  
I have question regarding how to deal with MQTT topics.

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

I have the following flow that I use to control a sensirion MFC5500. The flow works well without bug. The one problem that I have is when I want to do read the sensor value, I inadvertently created an endless loop.  
I trigger a measurement by sending a payload ( "-") on the topic mfc/A01/read. The sensor returns its value as a new payload on the same topic; which starts an infinite loop. In itself it is not the worst unforeseen issue since I need to get measurement at regular intervals, but I would rather have a little bit more control as to when those happen.

What would be the best practice to deal with a situation like this? Is it to check the type of payload (ex. string vs. number) and ignore messages that are not the right type? Is it to create a different topic for the sensor data in a separate branch?

In this specific instance I have multiple type of sensors and it is convenient for me to segregate them by type (ex. mfc, temp, pressure, etc..) and then using a different topic to identify the specific sensor (ex. A01, H03, etc...).

I know their might not be one right solution, I am just curious what the hive mind thinks.  
Thank you in advance.  
W.

---

<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: [5 May 2025 22:26 UTC](https://discourse.nodered.org/t/mqtt-topics-creates-loop-with-serial-port/96914/2 "2025-05-05T22:26:00Z")

</div>

Personally I would arrange for requests sent to the sensor device to have a different topic from replies. For example `mfc/A01/read` and `mfc/A01/reply`.  
Then your mqtt-in node could subscribe to `mfc/+/reply` (/+/ means any single word in the topic, thus it matches `mfc/A01/reply` and `mfc/H03/reply`, etc).

Failing that, put a switch node after your mqtt-in to discard those messages where msg.payload === "-"

---

<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: [3 August 2025 22:26 UTC](https://discourse.nodered.org/t/mqtt-topics-creates-loop-with-serial-port/96914/3 "2025-08-03T22:26:09Z")

</div>

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