# Access to mqtt retained messages

**URL:** <https://discourse.nodered.org/t/access-to-mqtt-retained-messages/75914>\
**Category:** General\
**Created:** [1 March 2023 08:59 UTC](https://discourse.nodered.org/t/access-to-mqtt-retained-messages/75914 "2023-03-01T08:59:36Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![ianh33](https://avatars.discourse-cdn.com/v4/letter/i/ea5d25/32.png) [@ianh33](https://discourse.nodered.org/u/ianh33)\
**Post date:** [1 March 2023 08:59 UTC](https://discourse.nodered.org/t/access-to-mqtt-retained-messages/75914/1 "2023-03-01T08:59:36Z")

</div>

I have a pi relay /sensor which successfully subscribes to an mqtt mosquitto broker.

I have node red flows which publish 'retained' mqqt data (e.g. astropi/heater/set/ON) .

'Set' triggers a relay on the pi and reports a 'state' back to the broker. (e.g. astropi/heater/state/OFF). At the time the relay is 'set', I can use an "mqtt out" to report the 'state'.

I want a flow which obtains the retained 'state' at a later time (i.e. not at the time of publication). I could store the 'state' on publication in context data but the rest of the system is complex (involving control via a web interface) and I would really rather obtain the data from the mqtt broker if I can.

Is this spossible ?

Thank you

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 March 2023 09:01 UTC](https://discourse.nodered.org/t/access-to-mqtt-retained-messages/75914/2 "2023-03-01T09:01:36Z")

</div>

If the messages are _tagged_ to be `retained` then yes.

All you do is get a `MQTT-IN` node and set the topic.  
Stick a `debug` node to (display entire message) and when you `DEPLOY` you should see the retained message for that topic.

---

<div class="post-metadata">

**Author:** ![ianh33](https://avatars.discourse-cdn.com/v4/letter/i/ea5d25/32.png) [@ianh33](https://discourse.nodered.org/u/ianh33)\
**Post date:** [1 March 2023 09:16 UTC](https://discourse.nodered.org/t/access-to-mqtt-retained-messages/75914/3 "2023-03-01T09:16:50Z")

</div>

Sorry I'm not exmplaing myself very well.

......... done all of that. Everything works as it should do.

Having done that and with everything up and running. How do I query the retained messages held in the mqtt broker and use that data in a flow ?

---

<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:** [1 March 2023 09:19 UTC](https://discourse.nodered.org/t/access-to-mqtt-retained-messages/75914/4 "2023-03-01T09:19:16Z")

</div>

It's not possible. It's not how MQTT works I'm afraid. Context is the goto solution for this.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 March 2023 09:22 UTC](https://discourse.nodered.org/t/access-to-mqtt-retained-messages/75914/5 "2023-03-01T09:22:48Z")

</div>

~~The _retained data_ is in the broker.~~

~~You _get_ it from the broker with the `MQTT-IN` node as I said.~~

~~Then you set the `topic` to _get_ the data.~~  
~~It only _happens_ once at deployment. (Or if the data changes while/after deployed)~~  
~~So you need to have the next part of the flow also built to process the message.~~

~~EXAMPLE:~~  
~~You want to get `some_device/data` (topic).~~  
~~`MQTT-IN` set to `some_deive/data/#`. Connect a `debug` node to that shows the ENTIRE message.~~

~~`DEPLOY` and see if the `debug` node gives you what you want.~~  
~~I hope it does as I believe it should.~~

Ok, all that is wrong.  
SORRY.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 March 2023 09:26 UTC](https://discourse.nodered.org/t/access-to-mqtt-retained-messages/75914/6 "2023-03-01T09:26:12Z")

</div>

Sorry, so am I wrong?  
Oops.

---

<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:** [1 March 2023 09:34 UTC](https://discourse.nodered.org/t/access-to-mqtt-retained-messages/75914/7 "2023-03-01T09:34:25Z")

</div>

Use dynamic subscription, unsubscribe and resubscribe and the retained message will be delivered again

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [1 March 2023 09:38 UTC](https://discourse.nodered.org/t/access-to-mqtt-retained-messages/75914/8 "2023-03-01T09:38:08Z")

</div>

While retained data sent via MQTT will be sent at the startup of node-red, it will only be sent once while NR is running. You can not 'request' the msg at a specified time.

The `broker` controls when data is sent to `subscribers` (i.e. node-red or some other subscriber) and that happens when a message is received by the broker.

When there is a new connection to the broker AND the broker has msgs retained for the topic just subscribed to, the broker will send those msgs to the subscriber.

If you want to save data that is received you can do as @jbudd suggests or, as @Steve-Mcl suggested, store the data in a flow or global context variable for use at a later time.

If you want to learn more about MQTT then see this tutorial [MQTT Essentials - All Core Concepts explained](https://www.hivemq.com/mqtt-essentials/)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 March 2023 09:39 UTC](https://discourse.nodered.org/t/access-to-mqtt-retained-messages/75914/9 "2023-03-01T09:39:05Z")

</div>

Thanks for clarifying that for me.

I was confused but this does help.

---

<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:** [1 March 2023 09:47 UTC](https://discourse.nodered.org/t/access-to-mqtt-retained-messages/75914/10 "2023-03-01T09:47:09Z")

</div>

I can confirm that simply re-injecting to a dynamic MQTT-in node will result in the retained messages being re-sent. This is actually a _really_ useful side-effect when testing dashboards! 😁

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 March 2023 09:48 UTC](https://discourse.nodered.org/t/access-to-mqtt-retained-messages/75914/11 "2023-03-01T09:48:49Z")

</div>

I guess the important part is that it is a DYNAMIC MQTT-IN node not the standard default settings on the MQTT-IN node..

---

<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:** [1 March 2023 10:07 UTC](https://discourse.nodered.org/t/access-to-mqtt-retained-messages/75914/12 "2023-03-01T10:07:52Z")

</div>

Hmm.

MQTT retained messages + dynamic subscription as an alternative to global context variables.  
As an alternative to persistent storage ...  
Accessible from more than one Node-red instance too.

---

<div class="post-metadata">

**Author:** ![stephenn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stephenn/32/66644_2.png) [@stephenn](https://discourse.nodered.org/u/stephenn)\
**Post date:** [1 March 2023 12:51 UTC](https://discourse.nodered.org/t/access-to-mqtt-retained-messages/75914/13 "2023-03-01T12:51:51Z")

</div>

Thanks for all of the info. I was having trouble with Owntracks staying updated and was wondering the same thing about getting access to retained messages. I was about to ask the same question as OP.

---

<div class="post-metadata">

**Author:** ![ianh33](https://avatars.discourse-cdn.com/v4/letter/i/ea5d25/32.png) [@ianh33](https://discourse.nodered.org/u/ianh33)\
**Post date:** [1 March 2023 17:04 UTC](https://discourse.nodered.org/t/access-to-mqtt-retained-messages/75914/14 "2023-03-01T17:04:36Z")

</div>

fair comment, thank you

---

<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:** [1 March 2023 17:25 UTC](https://discourse.nodered.org/t/access-to-mqtt-retained-messages/75914/15 "2023-03-01T17:25:51Z")

</div>

> [@jbudd](#):
>
> MQTT retained messages + dynamic subscription as an alternative to global context variables.

I usually use Join nodes for that. Then you don't need the logic to trigger the fetch.

---

<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:** [1 March 2023 17:54 UTC](https://discourse.nodered.org/t/access-to-mqtt-retained-messages/75914/16 "2023-03-01T17:54:04Z")

</div>

Sorry Colin, I don't understand using join nodes for this?

I only have a couple of global variables - for example API keys where I retrieve the data using an HTML node rather than a contrib node. It lets me manage these variables on a single editor tab instead of scattered among the flows as they are used. Then on a weather flow I use

```auto
msg.appid = global.get('openweathermapid');

```

I _could_ have the weather flow on multiple Node-red hosts in different locations.  
Using retained messages offers the possibility to ensure values are always consistent across the platforms.  
 ![Untitled 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/5/25e57b2fdd46e0df60e822b08863fe49b31c1241.jpeg)  
Just a thought, and probably not a good use case.

---

<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:** [2 March 2023 16:27 UTC](https://discourse.nodered.org/t/access-to-mqtt-retained-messages/75914/17 "2023-03-02T16:27:01Z")

</div>

> [@jbudd](#):
>
> I don't understand using join nodes for this?

Something like this

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/5/5583f491702d30904650090588e260bd2a566091.png)

---

<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:** [1 May 2023 16:27 UTC](https://discourse.nodered.org/t/access-to-mqtt-retained-messages/75914/18 "2023-05-01T16:27:48Z")

</div>

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