MQTT LWT design philosphy question

In polishing up some code I started using the MQTT LWT message to notify the flow of dropping connections. The code works and I think I understand the use of the LWT but...... in all examples I have looked at both on this forum and other sites they all list a different topic for the LWT message and normal mqtt topic. In my flows I use a separate node for each MQTT message instead of a generic # filter and look for the topic. I have seen different opinions on this as well with some suggesting that you should only use one MQTT node in node red. That is a different subject but what I an interested in is.... is there some reason to use a different LWT topic from the normal one? IE. - Let's say the ESP8266 sends a normal topic of sensor and then the message, let's say it's a temp sensor so the message is numeric. If the LWT message is for example "sensor bad" why not send it down the same topic so the node red mqtt node can see all normal messages as well as LWT messages?

There is no reason you should not do this if it meets your requirements. Generally, though, it is more convenient to use separate topics. It is unusual for a device only to send a single value. More often there are process values, device status and other information which are published on separate topics.
Even in the simple situation you describe it may be more convenient to use separate topics. Suppose you were sending the values to influxdb, if you have values such as you describe appearing in the stream you would have to filter those out before sending to influx, which would add complexity.

Thank you for the reply. My requirements are fairly simple, turn a relay on or off depending on the vale. No data storage, no data recording, flip a relay. But I do want to know that the sensor is on line. Oh and just to clarify the sensor depending on which one it is sends data every 15 to 30 seconds.

How are you handling the error string in the flow driving the relay? If you have additional code or logic to handle that then it can be removed if you use a separate LWT topic.
What is the advantage of using the same topic?

As of right now the LWT topic is different and I use a separate node. Were I to implement my new knowledge I would use a function node. As it is the question falls under the -- not sure I am going to do it , probably won't, but want to know for knowledge sake category.

So what would be the advantage of using the same topic?

Frankly, I don't see much of an advantage if any. Maybe node count would go down, maybe not. But until I understand all the choices I'm not satisfied I made the right choice.

A function node is has some overheads associated with it, so you would probably have to save several nodes to offset that. Also don't forget the greater bug risk with a function node.

Personally, FWIW, I enjoy multiple MQTT topics. I find it easier to use different node threads for the different purposes. It is easier to develop and debug. I would guess that what you want to run in NR based on a normal set of data vs an error would be totally different. At least it is in most of my use cases.

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