Best way to save MQTT payload and send it back on request

I have programmed a Particle IOT device which uses MQTT to communicate with Home Assistant.

  • The device publishes data payloads whenever a state-change occurs.
  • The device also publishes a notification when the device reboots after power outages or software updates.

I now need:

  • Home Assistant to always retain a copy of the latest data payload.
  • Home Assistant to return the latest data payload to the device when it receives a reboot notification. (This will permit the device to restore information lost in the reboot.)

How can I best accomplish this?

The data payload is a 300+ character JSON string.

The best of achieving this is to let the broker store the message by sending the message from the Particle IOT device with the retain flag set.

This causes the broker to store the message and send it out whenever a client subscribes to your topic.

If your Particle IOT device subscribes to the topic on startup, it will receive back the last message it sent.

Interesting, I’ll give that a try. Thanks.

Well, I gave it a try, and it worked great.

  • My device now publishes the MQTT message with the “retain” flag set true.
  • When my device reboots, it subscribes to the same topic that it normally publishes.
  • The MQTT processes the new subscription and returns the last message for that topic
  • My device then parses the message payload, restores the data that it lost when it rebooted, and unsubscribes from the topic.

Thank you Graham.

This is an interesting situation. Usually the device itself is the source of truth. By that I mean a device is its own reference of its status.

What you’ve described is a device that, after rebooting, needs to be informed of its previous state. That’s interesting.

If you don’t mind me asking, what is it function? Also, how does it behave if it isn’t informed of its previous state?

I’m guessing a counter value…

A counter with a “300+ character JSON string” for its payload? Must be a really big number!

:wink:

nah - it just has an excessively long name :wink:

This particular Particle “Photon” has controlled my garage doors for the past couple of years. I wrote the code from scratch … to control up to four garage doors. My application exposes a variable named “dStateJSON” and a function named “webButtonPress”. I then wrote an HTML GUI that presents data parsed from “dStateJSON” and presents an HTML button to trigger “webButtonPress” for each garage door. That GUI is displayed below. Note, I only have two garage doors … so only two are listed.

dStateJSON is large because it contains door-state, door-stateTime, door-stateText, door-buttonText, and other data elements that I use for debugging purposes. Of course this gets multiplied by (up to four doors) Particle will expose up to 622 bytes per variable, so I was not forced to be frugal.

When the Photon reboots, dStateJSON disappears. The rebooted Photon refreshes it based on its sensors, but it does not know the correct stateTimes.

I’m now in the process of integrating this application with Home Assistant. The first step was to publish dStateJSON via MQTT. As I did that, I saw the opportunity to address the lost-data problem.

At some point, I hope to create a HA card that looks something like the cell-phone GUI … but I think I need to build up my HA skills before I take that task on.

Thanks for your curiosity, comments, and a good laugh.

Thanks for clarifying the intended application. Nice work on the device!