Send offline data over MQTT out?

Hey all,

I'm using the Node-Red Add-On on Home Assistant to poll and send via mqtt (to a remote mqtt broker) various sensor data. Now, if the internet goes down for whatever reason, the connection between the remote mqtt broker and node red will break. Once they're connected again (ie. internet is up), the mqtt out node will continue sending live data and I would have missed out on data that it tried sending when the internet was down. Is there any way to avoid this sort of data loss? Can the mqtt out node save the data someplace when mqtt disconnects and then resend the same once connection is established? Is there any in-built setting that allows this?

Thanks

This flow by @colin may help

Thanks - I'm actually struggling with that. I imported the MQTT example and tried following instructions, it's repeatedly sending the same payload over and over again - not sure what I might be doing wrong.

For the flow to work it is necessary to set the mqtt topic in each message, not in the mqtt node. Are you doing that? I need to add that to the docs.

1 Like

Oh, I do not have the topic specified in any message. The "Create JSON Payload" function node looks like this :

I'm not sure how or where on the node I must mention the topic flow1/temperature.

Thanks for your help.

In your function node add one more line

msg.topic = "flow1/temperature"

Put it just before return msg.

In your mqtt node remove flow1/temperature from the Topic field.
You might want to give this node a meaningful Name because without the Topic filled in it will just be labelled mqtt out.

Connect a Debug node to the output of the function, set to display the complete message object.
This will let you check that msg.topic is right, and if Home Assistant and the function are repeatedly sending the same data.

1 Like

It worked!!

Thanks and big thank you to you @Colin for creating this.

I went through the node's description page really carefully this time and figured out a much simpler alternative for my particular use case, where I don't have to specify the topic in the message:

The "MQTT Complete Check" (linked to "Send to MQTT") simply passes the JSON to the "Send OK if Send Complete" change node (if the delivery was successful) which in turn passes an "OK" to the Guaranteed Delivery node. If it doesn't pass an OK within fail timeout, the Guaranteed Delivery node assumes the MQTT payload failed to send and queues it. This node is super handy.

The context store I'm using is "default", which in Home Assistant, I believe, won't persist after a restart, unfortunately. Once "offline" data is sent, will it auto delete from memory? Or will it stay?

Thanks a lot for your help!

I am away from home at the moment, so can't check your flow, but if you have simplified the flow then you may have missed something important.