# Ignore retained MQTT messages at restart

**URL:** <https://discourse.nodered.org/t/ignore-retained-mqtt-messages-at-restart/45458>\
**Category:** General\
**Created:** [10 May 2021 21:40 UTC](https://discourse.nodered.org/t/ignore-retained-mqtt-messages-at-restart/45458 "2021-05-10T21:40:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![drtorchwood](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drtorchwood/32/32268_2.png) [@drtorchwood](https://discourse.nodered.org/u/drtorchwood)\
**Post date:** [10 May 2021 21:40 UTC](https://discourse.nodered.org/t/ignore-retained-mqtt-messages-at-restart/45458/1 "2021-05-10T21:40:54Z")

</div>

Hi there,

I'm facing an annoying problem with my node-red installation. The MQTT server and node-red are running on two different machines. In addition, I have Tasmota installed on a esp8266 device that measures temperature etc. and works as a Zigbee coordinator, too.  
Now, if I restart node-red ghost switching occurs because the last Zigbee messages are read and they trigger some actions. I cannot turn off message retain because I need them for another application. Therefore, my question is: is it possible to make node-red ignore the retained (or all) messages at restart?

---

<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:** [10 May 2021 22:10 UTC](https://discourse.nodered.org/t/ignore-retained-mqtt-messages-at-restart/45458/2 "2021-05-10T22:10:58Z")

</div>

I think that if you turn off the clean start feature then you may not get them but I've never tried.

Alternatively, you would need a simple gate function to filter them out at startup. Use a gate node and block until a flag is set true. Use a trigger node with a delayed start to change the flag.

---

<div class="post-metadata">

**Author:** ![drtorchwood](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drtorchwood/32/32268_2.png) [@drtorchwood](https://discourse.nodered.org/u/drtorchwood)\
**Post date:** [11 May 2021 20:51 UTC](https://discourse.nodered.org/t/ignore-retained-mqtt-messages-at-restart/45458/3 "2021-05-11T20:51:36Z")

</div>

I tried without the clean session parameter but unfortunately this did not solve the issue.  
Now, I added a gate and it works. If someone has the same problem, this is the simple solution:

```auto
[{"id":"4da2fc62.711074","type":"inject","z":"edcab380.b1f18","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":180,"wires":[["9c03b679.3b4fb8"]]},{"id":"9c03b679.3b4fb8","type":"gate","z":"edcab380.b1f18","name":"","controlTopic":"control","defaultState":"closed","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","statusCmd":"status","persist":false,"x":330,"y":240,"wires":[["60de5ea5.3a0fe"]]},{"id":"1d065f9a.31f48","type":"inject","z":"edcab380.b1f18","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"5","topic":"control","payload":"open","payloadType":"str","x":170,"y":280,"wires":[["9c03b679.3b4fb8"]]},{"id":"60de5ea5.3a0fe","type":"debug","z":"edcab380.b1f18","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":520,"y":260,"wires":[]}]

```

---

<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 May 2021 21:13 UTC](https://discourse.nodered.org/t/ignore-retained-mqtt-messages-at-restart/45458/4 "2021-05-11T21:13:08Z")

</div>

You could use a function node that ignores the first message. Something like

```auto
let pastFirst = context.get("pastFirst") || false
if (!pastFirst) {
  context.set("pastFirst", true)
  msg = null
}
return msg

```

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [12 May 2021 05:37 UTC](https://discourse.nodered.org/t/ignore-retained-mqtt-messages-at-restart/45458/5 "2021-05-12T05:37:18Z")

</div>

If you just want to ignore the retained messages, then a switch node which checks the `msg.retain` flag should suffice.

---

<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:** [11 July 2021 05:37 UTC](https://discourse.nodered.org/t/ignore-retained-mqtt-messages-at-restart/45458/6 "2021-07-11T05:37:36Z")

</div>

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