Ignore retained MQTT messages at restart

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?

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.

2 Likes

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:

[{"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":[]}]

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

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

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

1 Like

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