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 |
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.
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!
@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.
@summerboy12, in the mqtwarn.ini, how do you get the payload message to be displayed in the
format
field as well ? I assumed you could just put{payload}
but it doesn't seem to work!for example if I add;
format = Device: {topic} went ==> {payload} @{_dthhmm}
it will return in the log;
Which is the correct payload message, but nothing else is printed.
But now lets say I remove the
{payload}
syntax and just put in anytext in the .ini file;format = Device: {topic} went ==> anytext @{_dthhmm}
it will return in the log;
/lwt/testing1 went ==> anytext @16:45
Any ideas? I have tired both {payload} and {message}