I'm using an ESP8266 as an IOT device, with MQTT to send status messages. Each device have its own availability topic, to state its online or offline state ( needed for Home Assistant indication on web GUI )

Since multiple devices are present- I was wondering if a lastwill status can be sent for a joint topic to be monitored ( for example :"device_id:offline" )

my relevant part of the code defining a lastwill for devices topic is : mqttClient.connect(deviceName, user, passw, availTopic, 0, true, "offline")

EDIT1: using PubSubClient.h

  • 1
    no, last will is set up when connecting to the broker (as you know), and you can only set one topic with one value - what you can do in home assistant is use that LWT topic for however many "devices" you need - I'm guessing by "devices" you mean multiple devices connected to a single ESP8266 ... actually, reading your question fully, I see you more or less know what to do ... use a "joint" topic – Jaromanda X Jan 4 at 6:30
  • @JaromandaX by multiple devices I meant 10X ESP8266 devices sending different sensor readings. If I was misunderstood - I meant that I want I single topic to recieve all offline notification from all 10 devices ( a topic that will be saved as a log file ). – Guy . D Jan 4 at 6:47
  • oh, so 10 devices sending to the one topic? that makes no sense ... if one is down, then all are considered down? – Jaromanda X Jan 4 at 7:48
  • No. Sorry for my bad English. Each device will identify in its last will message as ‘offline_dev1’ – Guy . D Jan 4 at 8:19
  • so each device will use exactly the same MQTT topic? – Jaromanda X Jan 4 at 8:20

The "Last Will & Testament" facility of MQTT is controlled by the server, not the client.

The topic and payload are specified when the client connects as part of the connection handshaking. When the client disconnects this stored information is published automatically by the server.

The specification only allows for one topic and payload to be set.

Of course, you can specify the same topic for many clients and give each one its own distinct payload, but you can't also have a unique topic for each client at the same time.

This is according to the specification - however that doesn't stop a broker implementing its own extensions to the specification to allow such things - but I know of no such extensions being implemented. But, given the open-source nature of many MQTT implementations (PubSubClient.h and Mosquitto, for example), there is no reason you couldn't extend the protocol to implement your own requirements (at the cost of compatibility with other systems of course).

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.