# MQTT QoS 1 not working

**URL:** <https://discourse.nodered.org/t/mqtt-qos-1-not-working/59616>\
**Category:** General\
**Tags:** mqtt\
**Created:** [9 March 2022 20:26 UTC](https://discourse.nodered.org/t/mqtt-qos-1-not-working/59616 "2022-03-09T20:26:57Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![WhiteLion](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/whitelion/32/12266_2.png) [@WhiteLion](https://discourse.nodered.org/u/WhiteLion)\
**Post date:** [9 March 2022 20:26 UTC](https://discourse.nodered.org/t/mqtt-qos-1-not-working/59616/1 "2022-03-09T20:26:57Z")

</div>

Hi folks,  
since my home automation / IoT devices grows I get the problem that mqtt messages get dropped. So I switched all mqtt nodes from QoS 0 to QoS 1 and hoped to get an easy fix. -\> fail.  
I am unsure why this does not work. When I watch the broker traffic / topics with mqtt.fx I still see the message have QoS 0 and the problem remains. Why is that ?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/3/f3368a55ca18307ebb712daf75fd83f417a231a7.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/c/4c74cc6d6440f2ee909c9ffc974d9b08af66b383.png)

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [9 March 2022 20:37 UTC](https://discourse.nodered.org/t/mqtt-qos-1-not-working/59616/2 "2022-03-09T20:37:00Z")

</div>

Although probably not causing your problem, but MQTT topics should not start with `/` separator, as it's purpose is to separate 2 topic levels.  
For example `SwitchCommandsA/lighting`.

---

<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:** [9 March 2022 20:46 UTC](https://discourse.nodered.org/t/mqtt-qos-1-not-working/59616/3 "2022-03-09T20:46:03Z")

</div>

Which versions of node-red (see that bottom of the Menu dropdown), nodejs (command `node -v`), and the mqtt nodes (in Manage Palette) are you using?

---

<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:** [9 March 2022 20:48 UTC](https://discourse.nodered.org/t/mqtt-qos-1-not-working/59616/4 "2022-03-09T20:48:11Z")

</div>

Also, how have you configured the MQTT In node?

**[Edit]** and which broker are you using?

---

<div class="post-metadata">

**Author:** ![WhiteLion](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/whitelion/32/12266_2.png) [@WhiteLion](https://discourse.nodered.org/u/WhiteLion)\
**Post date:** [9 March 2022 21:21 UTC](https://discourse.nodered.org/t/mqtt-qos-1-not-working/59616/5 "2022-03-09T21:21:41Z")

</div>

- node red is V2.2.2
- nodejs is v14.18.1
- The mqtt nodes I am using are coming with node red (v2.2.2) by default. I cannot find a version in "Managed Palette". - At least I guess these nodes get updates too when updating node red.  
The mqtt config:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/c/ac94491711ea25a856fdb44ad192d2550ddbe58a.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/7/3738d8fe8960e2d89290bc9785192d8886eb2906.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/8/18f925f3cbd6f8a1beb5cd866d36b669cc22ba14.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/a/ea9226a8782e3a76fd353350b00bead94725a7c3.png)  
Th broker is mosquitto version 1.5.7

---

<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:** [9 March 2022 21:33 UTC](https://discourse.nodered.org/t/mqtt-qos-1-not-working/59616/6 "2022-03-09T21:33:22Z")

</div>

> [@WhiteLion](#):
>
> The mqtt nodes I am using are coming with node red (v2.2.2) by default. I cannot find a version in "Managed Palette".

Yes, you are right, they are part of the core.

Rather than use a third party client use an MQTT In node to subscribe to the topic. Make sure you specify QoS 1 there too and see what you get. It works for me

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/2/a203cc3cb58af64514a12c6918b1f40acafa4bb4.png)

Test flow. Note that the debug node is set to Complete Message so you can see the QoS

```auto
[{"id":"189aa4a4d572cdd9","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":2520,"wires":[["1d65cfb647cce860"]]},{"id":"703075d6505da00b","type":"debug","z":"bdd7be38.d3b55","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":470,"y":2580,"wires":[]},{"id":"a2cd79334f6b8714","type":"mqtt in","z":"bdd7be38.d3b55","name":"","topic":"test/qos","qos":"1","datatype":"auto","broker":"adf2dac3.b20fe8","nl":false,"rap":true,"rh":0,"inputs":0,"x":310,"y":2580,"wires":[["703075d6505da00b"]]},{"id":"1d65cfb647cce860","type":"mqtt out","z":"bdd7be38.d3b55","name":"","topic":"test/qos","qos":"1","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"adf2dac3.b20fe8","x":320,"y":2520,"wires":[]},{"id":"adf2dac3.b20fe8","type":"mqtt-broker","name":"","broker":"localhost","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"tigger/LWT","birthQos":"0","birthPayload":"Online","birthMsg":{},"closeTopic":"tigger/LWT","closeQos":"0","closePayload":"Offline","closeMsg":{},"willTopic":"tigger.LWT","willQos":"0","willPayload":"Offline","willMsg":{},"sessionExpiry":""}]

```

---

<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:** [9 March 2022 21:35 UTC](https://discourse.nodered.org/t/mqtt-qos-1-not-working/59616/7 "2022-03-09T21:35:49Z")

</div>

I suspect the QoS 0 value you see in MQTT FX is the subscription QoS not how the packet was published.

Remember, node-red is a client & so is MQTT FX. MQTT subscriptions have a QoS value to the broker just as publish have a QoS value to the broker.

The QoS part from node-red to broker is handled between the broker and the publishing client & the magic that makes it work is under the hood.

---

<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:** [9 March 2022 21:46 UTC](https://discourse.nodered.org/t/mqtt-qos-1-not-working/59616/8 "2022-03-09T21:46:01Z")

</div>

> [@Steve-Mcl](#):
>
> I suspect the QoS 0 value you see in MQTT FX is the subscription QoS

+1 to that. I have just tried with MQTT Explorer and it also shows 0. I would have thought it (and MQTT.fx) should subscribe with QoS 2 so it would actually show the published QoS. Otherwise it is a waste of time showing anything, I think. I can't see anything in mqtt explorer settings that allow this to be changed.

---

<div class="post-metadata">

**Author:** ![WhiteLion](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/whitelion/32/12266_2.png) [@WhiteLion](https://discourse.nodered.org/u/WhiteLion)\
**Post date:** [9 March 2022 22:07 UTC](https://discourse.nodered.org/t/mqtt-qos-1-not-working/59616/9 "2022-03-09T22:07:19Z")

</div>

@Colin  
Thx for your example.  
wait what .... do you mean that I didn t need mosquitto because node red nodes do work without any installation of a broke because there is a broker implemented !?

@Steve-Mcl  
Thanx for your explanation !  
So QoS 1 is properly send but it then is no fix for my problem I guess.  
Well.... I thought a bit and I think my implementation is the problem why it does not work as expected. All my light switches have subscribed to the topic "SwitchCommandsA" and look if the message contains their MAC == ID ....  
Now the broker send with QoS 1 and is okay with anyone taking the message. Dam it. While coding the ESP stuff it seemed to be a nice thing.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [9 March 2022 22:08 UTC](https://discourse.nodered.org/t/mqtt-qos-1-not-working/59616/10 "2022-03-09T22:08:56Z")

</div>

> [@WhiteLion](#):
>
> do you mean that I didn t need mosquitto because node red nodes do work without any installation of a broke because there is a broker implemented !?

No, you still need the broker.

---

<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:** [11 March 2022 20:18 UTC](https://discourse.nodered.org/t/mqtt-qos-1-not-working/59616/11 "2022-03-11T20:18:32Z")

</div>

> [@WhiteLion](#):
>
> Now the broker send with QoS 1 and is okay with anyone taking the message

Everyone subscribing to the message is guaranteed to get it at least once.  
How exactly do you know that messages are getting lost? Is there some particular circumstance under which this appears to happen?

---

<div class="post-metadata">

**Author:** ![WhiteLion](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/whitelion/32/12266_2.png) [@WhiteLion](https://discourse.nodered.org/u/WhiteLion)\
**Post date:** [16 March 2022 17:18 UTC](https://discourse.nodered.org/t/mqtt-qos-1-not-working/59616/12 "2022-03-16T17:18:37Z")

</div>

Sorry for my late response. The problem is still not solved. I took some time to test things and make sure what could solve it.

**The problem:**  
Devices sometimes do not get the command from the MQTT - broker. As a result the light does not turn of or on proper. The problem grows with more devices connected.  
More devices does not only mean more workload to MQTT broker and devices it does mean more WiFi workload and that s where I guess the reason.

**The solution (hopefully):**  
Make sure all my devices get the command from MQTT even under difficult conditions (lost connection...etc.).

**Technical situation:**  
Not every device (ESP12) has its own topic. They are bundled in types that does mean. e.g. all switch have subscribed with QoS 0 to "SwitchCommandsA"  
and publish with QoS 0 to "SwitchTypA". The ESPs deserialize the message and drop messages witch does not contains there ID.

In the past I thought "bundle switches to one topic" is good concept. At least from implementation perspective IF the switch acclivity and the number of switches is limited.  
I still don t think this causes the current problems because I watched the activity and even on "one message per minute" my problem accrues.

**Solving the problem first idea:**  
To make sure ESPs get the commands under all conditions:

- ESPs need to subscribe the topic with QoS 1/2 instead of QoS 0.
- Node Red needs to send with QoS 1/2 too.
- Clean falg set to false.

I dropped this idea because I found this:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/d/1d2089bddb31b176b4ec3cba90b93ba32982cd54.jpeg)

(Source: [http://www.steves-internet-guide.com/mqtt-clean-sessions-example/](http://www.steves-internet-guide.com/mqtt-clean-sessions-example/) )

**Solving the problem second idea:**  
According to the table (above) the device will get all messages one a reconnection. Since the device only needs the last message setting the retain flag should fix my problem.  
I tested it out: switched the state of a light while it was disconnected from power and connected to power again: it worked while my tests.

BUT yesterday I it happened again. - The light didn´t switched off ☹

I tested again and found out that retain is coupled to one topic and will only deliver the LAST MESSAGE PER TOPIC. If the WiFi connection is lost to more than one device I properly run into that issue.

**Solving the problem third idea:**

- Every device subscribe an own and a group topic like mqtt topic with retain flag and QoS 0.

**The current code wise implementation (ESP8266):**

```auto
...

#define MQTT_QOS 1

if (client_global.connect(id.c_str(), settingsMemory.mqtt_User.c_str(), settingsMemory.mqtt_Password.c_str())) 
{
  client_global.subscribe(settingsMemory.mqtt_Subscribe.c_str(), MQTT_QOS);    
  break;
} 
...

```

**the fix (?):**

```auto
...

#define MQTT_QOS 1

if (client_global.connect(id.c_str(), settingsMemory.mqtt_User.c_str(), settingsMemory.mqtt_Password.c_str())) 
{
  client_global.subscribe(settingsMemory.mqtt_Subscribe.c_str(), MQTT_QOS); #
  client_global.subscribe(WiFi.macAddress().c_str(), MQTT_QOS);  
  break;
} 
...

```

Did I miss something ?

---

<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 March 2022 17:35 UTC](https://discourse.nodered.org/t/mqtt-qos-1-not-working/59616/13 "2022-03-16T17:35:51Z")

</div>

> [@WhiteLion](#):
>
> Since the device only needs the last message setting the retain flag should fix my problem.

> [@WhiteLion](#):
>
> found out that retain is coupled to one topic and will only deliver the LAST MESSAGE PER TOPIC. If the WiFi connection is lost to more than one device I properly run into that issue.

Those two appear to be in contradiction. You say in the first that the device only needs the last message, but then say that it is a problem for you.

---

<div class="post-metadata">

**Author:** ![WhiteLion](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/whitelion/32/12266_2.png) [@WhiteLion](https://discourse.nodered.org/u/WhiteLion)\
**Post date:** [16 March 2022 18:46 UTC](https://discourse.nodered.org/t/mqtt-qos-1-not-working/59616/14 "2022-03-16T18:46:58Z")

</div>

You are right. Because of that my solution would be to give every device a specific topic to subscribe (mac address). As I described above many switches subscribe to the same topic. If something goes wrong (e.g. connection lost) then only the last / one message for this topic is retained. This will lead to my problem.

---

<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 March 2022 21:13 UTC](https://discourse.nodered.org/t/mqtt-qos-1-not-working/59616/15 "2022-03-16T21:13:15Z")

</div>

Do you mean that currently you publish messages for all devices to some/topic where the device that should take note is in, for example, `msg.payload.id`, or something along those lines anyway? If so then yes, move the id to the topic, so topic is `some/topic/id`. Then anything that wants access to all those messages can subscribe to `some/topic/#`, and can then parse the topic to get the id if necessary, and any devices that only want messages for themselves can subscribe to `some/topic/my_id`

---

<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 May 2022 21:13 UTC](https://discourse.nodered.org/t/mqtt-qos-1-not-working/59616/16 "2022-05-15T21:13:56Z")

</div>

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