# Get some status from my MQTT broker at startup

**URL:** <https://discourse.nodered.org/t/get-some-status-from-my-mqtt-broker-at-startup/79236>\
**Category:** General\
**Created:** [16 June 2023 05:32 UTC](https://discourse.nodered.org/t/get-some-status-from-my-mqtt-broker-at-startup/79236 "2023-06-16T05:32:36Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [16 June 2023 05:32 UTC](https://discourse.nodered.org/t/get-some-status-from-my-mqtt-broker-at-startup/79236/1 "2023-06-16T05:32:36Z")

</div>

Hi folks,

An MQTT noob speaking here...

I have some Shellies that send a message to an MQTT topic, every time their relay status changes. Via an MQTT-In node I receive that status, so I can e.g. change the color of a ui-led to green or red to visualize the status of the relay.

However when I restart Node-RED, the status of the relay is of course gone. I could store the relay status in persistent context memory in Node-RED. But that seems a bit of a workaround to me, because the message is already persisted on the MQTT broker. So I was wondering if the last message could be replayed again into Node-RED at startup.

Although while writing this post now, I assume I am talking nononse... Because the Shelly does not only send relay status change messages, but also messages about its wifi connection status and so on.

Would appreciate if somebody could share me some best practises about MQTT for this.

Thanks!!!  
Bart

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [16 June 2023 06:34 UTC](https://discourse.nodered.org/t/get-some-status-from-my-mqtt-broker-at-startup/79236/2 "2023-06-16T06:34:28Z")

</div>

You can use the dynamic subscription option and then just inject msg.action = subscribe and set msg.topic to required topic  
See node help for further info

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

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/c/bcca4f0efa399e656acad68750e14052ab5f211e.png)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/7/b7651755a25b5bc0327b3728284fadfa05626f6a.png)

```auto
[{"id":"2c1581c91250dcf6","type":"mqtt in","z":"50aa70b641104f06","name":"","topic":"","qos":"2","datatype":"auto-detect","broker":"ad1faea18d26b2bf","nl":false,"rap":true,"rh":0,"inputs":1,"x":510,"y":180,"wires":[["e3f87b6e7823901e"]]},{"id":"e3f87b6e7823901e","type":"debug","z":"50aa70b641104f06","name":"debug 132","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":180,"wires":[]},{"id":"40c716f11b71b278","type":"inject","z":"50aa70b641104f06","name":"","props":[{"p":"action","v":"subscribe","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cheerlights","x":350,"y":180,"wires":[["2c1581c91250dcf6"]]},{"id":"ad1faea18d26b2bf","type":"mqtt-broker","name":"","broker":"mqtt.cheerlights.com","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willRetain":"false","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""}]

```

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [16 June 2023 20:38 UTC](https://discourse.nodered.org/t/get-some-status-from-my-mqtt-broker-at-startup/79236/3 "2023-06-16T20:38:09Z")

</div>

Hi Simon,

Thanks for the tip!  
Ok your proposal makes sense. But when I inject a message to start the subscription, nothing happens. I assume that only new messages will be received this way, and not the old existing messages? Because I need the last message(s) in order to know the current status of the Shelly switch...

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [16 June 2023 20:56 UTC](https://discourse.nodered.org/t/get-some-status-from-my-mqtt-broker-at-startup/79236/4 "2023-06-16T20:56:43Z")

</div>

It will pick up any retained topic  
Can you set your Shelly to publish on a retained topic?

---

<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:** [16 June 2023 21:45 UTC](https://discourse.nodered.org/t/get-some-status-from-my-mqtt-broker-at-startup/79236/5 "2023-06-16T21:45:48Z")

</div>

Only the last message on a given topic is retained by the broker.

I found retained messages don't by themselves prevent dashboard/real world misalignment because sensors (etc) send their data and can then lose power.  
You have to combine the retained message with Last Will and Testament Online/Offline messages (also retained).

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [16 June 2023 21:48 UTC](https://discourse.nodered.org/t/get-some-status-from-my-mqtt-broker-at-startup/79236/6 "2023-06-16T21:48:32Z")

</div>

> [@cymplecy](#):
>
> Can you set your Shelly to publish on a retained topic?

Yes indeed it seems there is a "retain" flag in the Shelly switch user interface:  
_" The **Retain flag** is used to tell the broker to remember the last value until it gets a new one. Switches that don’t publish data very often would usually set the flag to true._

I will need to play with that in the next days. Thanks for the pointers!

---

<div class="post-metadata">

**Author:** ![greengolfer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/greengolfer/32/28276_2.png) [@greengolfer](https://discourse.nodered.org/u/greengolfer)\
**Post date:** [17 June 2023 16:18 UTC](https://discourse.nodered.org/t/get-some-status-from-my-mqtt-broker-at-startup/79236/7 "2023-06-17T16:18:51Z")

</div>

I don’t think you can be sure that the state value will be the last message (so potentially retained) on the topic.  
The fallback can be to use the rest API of the Shellies. Fetching the state value through this API when nodered starts.

---

<div class="post-metadata">

**Author:** ![hominidae](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hominidae/32/4174_2.png) [@hominidae](https://discourse.nodered.org/u/hominidae)\
**Post date:** [17 June 2023 19:09 UTC](https://discourse.nodered.org/t/get-some-status-from-my-mqtt-broker-at-startup/79236/8 "2023-06-17T19:09:48Z")

</div>

Just remember, that a re-joining subscriber/client to a vbroker will only be re-supplied again with that topic data, when re-connecting with a different client-ID. That setting is in the broker config for the mqtt node (cleint-ID: leave empty for automatic generation).

---

<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:** [17 June 2023 19:27 UTC](https://discourse.nodered.org/t/get-some-status-from-my-mqtt-broker-at-startup/79236/9 "2023-06-17T19:27:11Z")

</div>

I don't think that your suggestion would work, because maybe the message was sent whilst node-RED was connected, and therefore your client will have confirmed receipt of it, so why would it want to send it again?

You could always ask your shelly's for their current status, which may also be a more resilient option.

Try the attached flow, leaving the mqtt topics as they are, just update to your mqtt server.  
If the inject node is changed to inject once on startup, then it should ask all connected shelly devices to report their statuses, and use that to update the colour of your ui\_led's.

```auto
[{"id":"8b0af7dd77f5a07c","type":"inject","z":"f8cf7ea5344d6287","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"announce","payloadType":"str","x":120,"y":1350,"wires":[["3099f08e68b6c323"]]},{"id":"3099f08e68b6c323","type":"mqtt out","z":"f8cf7ea5344d6287","name":"","topic":"shellies/command","qos":"0","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"aa11e6d8.6cd088","x":300,"y":1350,"wires":[]},{"id":"9024984f2d995620","type":"mqtt in","z":"f8cf7ea5344d6287","name":"","topic":"shellies/+/info","qos":"0","datatype":"auto","broker":"aa11e6d8.6cd088","nl":false,"rap":true,"rh":0,"inputs":0,"x":105,"y":1300,"wires":[["e7898a73420c34c6"]]},{"id":"d99dacb4bba6148f","type":"debug","z":"f8cf7ea5344d6287","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":410,"y":1300,"wires":[]},{"id":"e7898a73420c34c6","type":"json","z":"f8cf7ea5344d6287","name":"","property":"payload","action":"","pretty":false,"x":260,"y":1300,"wires":[["d99dacb4bba6148f"]]},{"id":"aa11e6d8.6cd088","type":"mqtt-broker","name":"Local server","broker":"192.168.0.8","port":"1883","clientid":"Local node-RED","autoConnect":true,"usetls":false,"compatmode":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"admin","birthQos":"0","birthRetain":"false","birthPayload":"Hello from Local node-RED","birthMsg":{},"closeTopic":"admin","closeQos":"0","closeRetain":"false","closePayload":"Local node-RED is about to exit","closeMsg":{},"willTopic":"admin","willQos":"0","willRetain":"false","willPayload":"Local node-RED has now exited","willMsg":{},"sessionExpiry":""}]

```

---

<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:** [23 June 2023 07:44 UTC](https://discourse.nodered.org/t/get-some-status-from-my-mqtt-broker-at-startup/79236/10 "2023-06-23T07:44:31Z")

</div>

Did you resolve this @BartButenaers

---

<div class="post-metadata">

**Author:** ![tree-frog](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tree-frog/32/89303_2.png) [@tree-frog](https://discourse.nodered.org/u/tree-frog)\
**Post date:** [24 June 2023 02:45 UTC](https://discourse.nodered.org/t/get-some-status-from-my-mqtt-broker-at-startup/79236/11 "2023-06-24T02:45:17Z")

</div>

If I can interpret for my understanding, you want the status/value of an old message but you want the broker to update your MQTT receive node with a particular old message on startup. Depending on the broker, there could be a way to access old messages. You need to know which one, of course. If the messages are stored on the broker log, or database. Otherwise the messages are flushed once the subscription is cleared. You will need to seek the information from your broker's user guide(s).

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [29 June 2023 05:36 UTC](https://discourse.nodered.org/t/get-some-status-from-my-mqtt-broker-at-startup/79236/12 "2023-06-29T05:36:48Z")

</div>

Hi Paul,  
I will continue with this whenever I find time. But thanks to all the replies I have know an idea about the possibilities. Will post my feedback here when I continue with it...

---

<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:** [28 August 2023 05:37 UTC](https://discourse.nodered.org/t/get-some-status-from-my-mqtt-broker-at-startup/79236/13 "2023-08-28T05:37:15Z")

</div>

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