note; I have all my scripts setup so that the lwt (last will and testament) publishes 'offline' if the scripts die.
-
-
Save matbor/9657897 to your computer and use it in GitHub Desktop.
################################################################ | |
##### 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 |
@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.
Just added a change to allow you to specify the lwt payload in mqttwarn.
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!