0

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

asked Jan 4, 2019 at 6:17
11
  • 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 Commented Jan 4, 2019 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 ). Commented Jan 4, 2019 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? Commented Jan 4, 2019 at 7:48
  • No. Sorry for my bad English. Each device will identify in its last will message as ‘offline_dev1’ Commented Jan 4, 2019 at 8:19
  • so each device will use exactly the same MQTT topic? Commented Jan 4, 2019 at 8:20

1 Answer 1

2

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).

answered Jan 4, 2019 at 11:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.