Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [mosquitto-dev] Trouble with wills and retain, not sure where

You MUST set the LWT before connecting, the payload, topic, qos and retained status are all included in the CONNECT packet and will not be changed if set after connection.

So moving it to the on_connect callback really won't work.

The mosquitto log implies the client closed down cleanly so will not trigger the LWT. Do you have a signal handler in the python code that is closing the connection? If so this should be manually sending the offline message before exiting.

Do you see different behaviour if you kill it with -9 (you may have to wait 1.5 the keepalive)?


On 23/12/2021 17:57, Greg Troxel wrote:

I've been using mosquitto (two instances, bridged) with two Home
Assistants as the main listeners, and a variety of publishers for a long
time with good results.  I'm cleaning up some python code for public
consumption, that polls a UPS (ups-nut) and reports once a minute or
promptly if the data is interesting (e.g. power failure), and this led
me to really look at what's going on.

I'm runing mosquitto 2.0.12 on NetBSD 9 amd64, and py-paho-mqtt 1.6.1 on
python 3.8.

My python program uses py-paho-mqtt and tries to set a will to set a
particular to OFF, and sets that topic to ON on connect (payload values
to align with Home Assistant binary mqtt sensor).

I used to call will_set before connecting, and noticed that if I killed
the program, there was no update (watching with mosquitto_sub on the
same machine as the broker).  So I moved it to the on_connect callback.
I found the docs unclear, that's really a paho question.

My variable topic is "/sensor/foo/ups-bar".

I also noticed that the online message was coming through retain=False,
so I added the middle line for debugging.

         self.client.will_set(topic + "/online", payload="OFF", qos=2, retain=True)
         self.client.publish(topic + "/online", payload="ON", qos=1, retain=False)
         self.client.publish(topic + "/online", payload="ON", qos=2, retain=True)


On connect I get (mix of syslog and mosquitto_sub, minor name
redaction):

Dec 23 12:41:09 mosquitto[836]: 1640281269: New client connected from [ipv6addr-of-ups-box] as ups-bar (p2, c1, k60, u'mqttuser').
{"tst":"2021-12-23T12:41:09.641631Z-0500","topic":"sensor/foo/ups-bar/online","qos":1,"retain":0,"payloadlen":2,"mid":30,"payload":ON}
{"tst":"2021-12-23T12:41:09.798029Z-0500","topic":"sensor/foo/ups-bar/online","qos":2,"retain":0,"payloadlen":2,"mid":31,"payload":ON}

I then see my every-minute json dictionary of UPS info.

On killing the script I get:

Dec 23 12:49:52 mosquitto[836]: 1640281792: Client ups-bar closed its connection.


So:

   I'm seeing retain 0/False on both publishes of online, but qos 1/2
   comes through as expected.

   I am not seeing the will be executed on disconnect.

I have also noticed other sensors failing to have the will work when
they go offline (power, network), but I need to go back and check/update
their (nodemcu) code.

I know wills used to work for me, but I don't remember when.


This is a long way of asking: Is anybody else on 2.0.12 seeing wills be
correctly executed?  Seeing problems?  Any other advice?

(I'll try to look into this harder, of course, but already I'm
suspicious there's a bug not in my code.)

Thanks,
Greg


_______________________________________________
mosquitto-dev mailing list
mosquitto-dev@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/mosquitto-dev


--
https://www.hardill.me.uk/wordpress
https://github.com/hardillb
https://about.me/hardillb
https://flickr.com/photos/hardillb/
https://keybase.io/hardillb


Back to the top