Skip to content

Instantly share code, notes, and snippets.

@matbor
Last active August 29, 2015 13:57
Show Gist options
  • Save matbor/9657897 to your computer and use it in GitHub Desktop.
Save matbor/9657897 to your computer and use it in GitHub Desktop.
Last Will and Testament Notification Function for mqttwarn. https://github.com/jpmens/mqttwarn

note; I have all my scripts setup so that the lwt (last will and testament) publishes 'offline' if the scripts die.

################################################################
##### Last Will and Testament Notification Function #####
################################################################
#lwt offline message to check for
lwtmsg_offline = 'offline'
#last will and Testaments topics to not suppress
lwttopics2mon = [
'/lwt/testing1', #testing topic
]
#filter to check to see if the device/software is offline
def lwtfilter(topic, message):
for t in lwttopics2mon:
if t in topic:
if message == lwtmsg_offline:
return False #dont suppress the notifcations
return True #suppress the notification
[lwt]
topic = /lwt/#
targets = log:info,pushover:software
filter = lwtfilter()
format = Device: {topic} went ==> {payload} @{_dthhmm}
title = MQTT LWT Notification
@sumnerboy12
Copy link

Have you got the latest version? I added a fix the other day to include {payload} in the built in transform data dict. Previously it was missing, so your original format string would have failed (due to the missing {payload} parameter) and when this happens mqttwarn just falls back to the raw payload for display.

@sumnerboy12
Copy link

BTW - not sure that I fully understand the lwt concept. It is when MQTT clients lose connection correct? Does the broker publish the message itself, once the client connection drops? I noticed that OwnTracks on the wifes iPhone sends them every now and again, but I don't think I have ever seen my HTC One send one.

And do the lwt publishes always contain 'offline' in the payload?

Looks like a handy little use case for mqttwarn, just not sure I fully understand how it all fits together just yet!

@matbor
Copy link
Author

matbor commented Mar 20, 2014

@summerboy12, thx, was one commit behind... forgot to chk that. Have updated the script above to reflect that now

JP's explains lwt the best, here in an old post...

http://jpmens.net/2013/02/25/lots-of-messages-mqtt-pub-sub-and-the-mosquitto-broker/
"Clients can optionally set a Will (in Python before the connect() call). This "last will and testament" (so to speak) is published by the broker when a client disconnects unexpectedly. This can be useful to notify of particular clients' deaths. In other words, if a client issues the following request before connecting, and it dies at some later point, the broker will publish the payload on this client's behalf."

I have most of my scripts setup, so that when they start it publishes to the /lwt/%scriptname$ topic saying tat the script is online and if it dies it publishes offline usng the lwt... that is where the lwt script i made comes in handy.
Dont believe there is a standard as to what the lwt message should be, have seen lots of different versions, 0's and 1's etc, I just use online/offline for my use.

@sumnerboy12
Copy link

Just added a change to allow you to specify the lwt payload in mqttwarn.

mqtt-tools/mqttwarn@1c24e6a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment