How can Mosquitto know if OH has gone offline?

Hi all,

Been working with OH2 for about a week now and loving it. I’ll try to make it as short and detailed as possible.

I’m trying to acheive the following:

When OH goes offline for whatever reason (reboot, failure etc…) I want my ESP8266 to be aware of that and turn off all the relays for safety purpose.

So far I’ve been able to make it work with Mosquitto. If my RPi3 has a power issue my ESP will detect the deconnection with it’s Broker and turn all the relays off. However, I tried to work with the last will topics/messages of my Things in OH but I haven’t been able to make it.

I know that this way is already taking care of failures but I prefer to have redundancy and find a way to know if OH is down.

I’m also aware that if OH goes offline … well it’s offline so it doesn’t have any way to let you know i.e. You can’t make any code for that situation since there’s nothing to run the code. Therefore I know it has to come from Mosquitto but before being told that my concern is a Mosquitto concern I wanted to ask if any of you had an idea or if there’s already a known solution for that situation.

Thanks for your time.

You need to focus on why you failed to set this up becuase LWT is the way achieve what you want.

In the OH Broker Thing, configure a LWT topic and message. When Mosquitto detects that OH is no longer online (i.e. it doesn’t respond to a heartbeat message exchange) Mosquitto will publish the LWT message to the LWT topic.

I recommend adding the following details for this implementation:

  • use a retained message for the LWT message
  • use something meaningful like “OFFLINE” or “OFF” for the LWT message
  • create a System started Rule to publish “ONLINE” or “ON” to the LWT topic with the retained bit set.

With this, any client that wants to know whether openHAB is online or not just needs to subscribe to the LWT topic. Whether or not openHAB is actually online or not, it will always get a message indicating it’s current online/offline state.

For an example, I do this in my reusable MQTT Event Bus implementation. On the Broker Thing set the “Last Will Message”, “Last Will Topic”, and turn on “Last Will Retain.”

This is the Rule:

Author: Rich Koshak
Implements a Rule to publish ONLINE to a status topic to indicate when this
instance of OH comes online.
Copyright (c) 2019 contributors to the openHAB Scripters project
from core.rules import rule
from core.triggers import when
from configuration import eb_name, eb_br

@rule("Eventbus Online",
      description="Publsih that this instance of OH is now online",
@when("System started")
@when("Thing {} changed to ONLINE".format(eb_br))
def online(event):
    At OH system start, publish "ONLINE" to eb_name/status to indicate that OH
    has come back online. eb_name is defined in configuration and contains the
    unique name for this OH instance. The Broker Thing ID stored in eb_br is
    used. I recommend setting the retained flag to true for this Thing's LWT.
    online.log.debug("Reported eventbus as online")
    actions.get("mqtt", eb_br).publishMQTT("{}/status".format(eb_name),
                                           "ONLINE", True)

Thanks for pointing me in the right direction.

I haven’t been able to receive any last will from OH yet.

Here is the path I take to configure the broker:

OpenHAB2 / Paper UI / Configuration / Things / Mosquitto Broker /

Now for the relevant settings:

Broker hostname/IP = localhost;
QoS = 0;
Retain messages = Enabled;
Reconnect time & Heartbeat = 5000ms (small time for troubleshoot purpose);
Last will message = openhab_offline;
Last will topic = home/openhab/status;

I’m monitoring the topic mentionned above with MQTT.fx and I reboot OH using the command line and so far I haven’t received anything on that topic other than the tests that I sent using publish.

Is there something/a setting that I’m missing?


What version of OH are you running? You must be on 2.5 for the LWT messages to work. There is no longer a “Retained messages” property.

You also need to wait long enough for the MQTT broker to determine that OH is offline. A restart of OH may not take long enough for the LWT to be published.

I’m using the openHAB 2.4.0 Release Build so that might explain why it’s not working for me.

Will take a look at updating it to 2.5.

Thanks for your time.

After monitoring Mosquitto I found out that when I’m stopping OH with the command line, OH sends a disconnect message and therefore the broker knows it’s going offline.

I tried to shut down OH using the kill command but it still sends a disconnect message.

Since my broker is hosted on the same device as my OH server I have no idea on how I’m gonna simulate an unexpected shutdown.

Any idea?

You are correct, clean disconnects from the broker means it will not send the LWT. One option is to use a heartbeat message and monitor it. If you send the date/time in the heartbeat you can determine how long it has been since OH has updated it.

For clean disconnects you can probably use a “System shuts down” trigged rule or, if using scripted automation, a scriptUnload function to publish the OFFLINE message.