Main

A Local MQTT Dead Man’s Switch for a Sump Pump Monitor

A sump pump monitor that reports “no fault” is not the same as a monitor that is alive. The failure that matters most is silence: the float sensor, the current clamp, or the firmware loop stops reporting, and nothing in the system notices because nothing was ever expected to arrive. A dead man’s switch inverts that expectation. The monitor must prove it is alive on a schedule, and the absence of proof becomes the alarm.

This article describes a local-first design built on MQTT, Home Assistant, and optionally ESPHome. It treats the heartbeat as the primary signal, MQTT last-will messages as a complementary signal, and the alert path as something that must survive a WAN outage. It also concedes where a mechanical timer or a plain float switch is the better tool.

Why silence is the alarm condition

Most sump pump monitors are event-driven. They publish when the pump runs, when water reaches a level, or when a fault flag is set. That design is fine for reporting events, but it cannot distinguish “the basement is dry” from “the sensor cable was cut” or “the firmware task that publishes stopped running.” Both produce the same output: nothing.

A heartbeat changes the question from “did something bad happen?” to “is the monitor still able to tell me anything at all?” That is a weaker claim than “the pump is healthy,” and it is the claim a dead man’s switch can actually support. The monitor does not need to know why it is silent. It only needs to publish on a fixed cadence, and the receiver needs to notice when the cadence stops.

This is also why a dead man’s switch that depends on a vendor cloud is not a dead man’s switch for a sump pump. If the alert path requires an internet connection, a WAN outage and a pump failure look identical from the phone. The local-first stack exists to keep those two failures separable.

How the pieces fit

The design has four parts:

  1. The publisher. The sump pump monitor, or a small ESPHome node attached to it, publishes a short message to a local MQTT topic on a fixed interval. The payload can be a timestamp, a sequence number, or a single character. The content matters less than the arrival.
  2. The broker. A local Mosquitto instance, ideally on the same network segment as Home Assistant. Home Assistant’s MQTT integration requires a broker that supports MQTT protocol version 5; the official Mosquitto broker app does. See the Home Assistant MQTT integration documentation for the broker requirements.
  3. The receiver. A Home Assistant automation with an MQTT trigger on the heartbeat topic. Each heartbeat resets a timer or updates a helper. A separate check raises the alarm when the helper goes stale.
  4. The alert. The Home Assistant Alert integration, which watches a single entity’s state and sends repeating notifications at configurable intervals until the condition clears or the alert is acknowledged.

The key architectural point is that the heartbeat and the timeout are a matched pair. If the publisher sends every 60 seconds and the alert fires after three missed beats, that is a 180-second detection window. Change one number without the other and the switch either fires constantly or never fires. Document both numbers next to the pump.

Step 1 — Define the heartbeat topic and interval

Pick a topic that names the device and the purpose, for example sump/pi/status/heartbeat. Avoid reusing the availability topic that ESPHome uses by default. ESPHome publishes a retained online message to <TOPIC_PREFIX>/status on connect and instructs the broker to publish offline if the connection drops, per the ESPHome MQTT component documentation. That is a connection-level signal, not an application-level one, and mixing the two makes diagnosis harder.

If the publisher is an ESPHome node, a simple interval automation can publish the heartbeat:

interval:
  - interval: 60s
    then:
      - mqtt.publish:
          topic: sump/pi/status/heartbeat
          payload: !lambda 'return to_string(millis());'
          retain: false
          qos: 0

Two cautions apply. First, if the ESPHome device enables MQTT without the native API, the documentation warns that it will reboot every 15 minutes unless reboot_timeout: 0s is set or the api: block is removed. A rebooting publisher produces gaps that look like failures. Second, do not set retain: true on the heartbeat. A retained heartbeat means a new subscriber sees the last beat immediately, which can mask a stale publisher. Retain is appropriate for discovery and availability, not for liveness.

If the publisher is a small script on a Raspberry Pi or similar, the same rule applies: publish on a timer, do not retain, and log the publish call so a hung loop is visible in the device’s own logs.

Step 2 — Reset a helper on each heartbeat

Home Assistant triggers react at a specific moment, not while something is true. The trigger documentation is explicit about this: a trigger for a door opening fires on the transition, not continuously while the door is open. That means the heartbeat automation should do one thing — record that a beat arrived — and a separate mechanism should decide whether the record is too old.

One workable pattern is a timer helper started or restarted by the heartbeat automation. Another is an input_datetime helper updated to the current time on each beat, with a template binary sensor that compares it to now(). The timer approach is simpler to reason about; the timestamp approach leaves a visible record of the last beat on the dashboard.

The heartbeat automation itself is short:

alias: Sump heartbeat received
triggers:
  - trigger: mqtt
    topic: sump/pi/status/heartbeat
actions:
  - action: timer.start
    target:
      entity_id: timer.sump_heartbeat
    data:
      duration: "00:03:00"
mode: restart

The timer duration is the detection window, not the heartbeat interval. If the publisher sends every 60 seconds and the timer runs for 180 seconds, the alarm fires after roughly three missed beats. Setting the timer to 60 seconds would fire on normal jitter.

Step 3 — Raise the alert when the heartbeat is overdue

The Alert integration watches a single entity’s state and sends repeating notifications until the condition clears or the alert is acknowledged. Its repeat option can be a list of numbers, which lets the notification cadence change as the alert continues — for example, a first reminder after 15 minutes, a second after 30, and hourly after that. The Alert integration documentation describes this behavior and the acknowledgement model.

Because the Alert integration only checks whether one entity’s state equals a value, the overdue condition should be expressed as a template binary sensor. The alert then watches that binary sensor:

template:
  - binary_sensor:
      - name: Sump heartbeat overdue
        state: "{{ is_state('timer.sump_heartbeat', 'idle') }}"
        device_class: problem

alert:
  sump_heartbeat:
    name: Sump pump monitor is silent
    entity_id: binary_sensor.sump_heartbeat_overdue
    state: "on"
    repeat:
      - 15
      - 30
      - 60
    can_acknowledge: true
    skip_first: false
    message: "No heartbeat from the sump pump monitor."
    done_message: "Sump pump monitor heartbeat restored."
    notifiers:
      - mobile_app_phone

The done_message is not decoration. It closes the loop: when the publisher comes back and the timer restarts, the alert returns to idle and the recovery message confirms that the silence ended. Without it, the operator has to check the dashboard to know whether the alert is still active.

The acknowledgement behavior matters for a real pump event. If the pump runs during a storm and the monitor’s publishing loop is briefly starved, the alert may fire. Acknowledging it silences the repeat notifications while the operator investigates, and the done message confirms recovery. The alternative — an alert that cannot be acknowledged — turns a transient into an endless stream.

Step 4 — Use last-will messages as a second signal, not the only one

MQTT last-will messages are useful and they are not sufficient. Home Assistant’s MQTT integration supports birth and last-will messages, and the documentation notes that the LWT is sent on both clean and unclean disconnects — a clean shutdown and a crashed or network-isolated client both produce the will message. ESPHome uses this by default: a retained online birth message and an offline will message on the status topic.

The gap is a device that stays connected while its publishing loop hangs. The TCP socket remains open, the broker sees a healthy client, and no will message is sent because the connection never dropped. The heartbeat-timeout design covers that case; the LWT does not. This is a design argument, not a sourced claim about any specific firmware bug — I have not seen a documented case of an ESPHome publish loop hanging while the socket stays open, and the sources describe connection loss rather than application-level hangs.

The practical arrangement is to use both signals for different purposes. The LWT tells you the device disconnected. The heartbeat timeout tells you the device stopped proving it was alive, whether or not it disconnected. The alert should be driven by the heartbeat timeout, with the LWT available on the dashboard for diagnosis.

Step 5 — Test the switch by stopping the publisher

The most common testing mistake is to unplug the broker. That tests the broker, not the dead man’s logic. The correct test is to stop the publisher while leaving the broker and Home Assistant running.

  1. Confirm the heartbeat is arriving. Subscribe to the topic with mosquitto_sub -h <broker> -v -t sump/pi/status/heartbeat and watch for messages at the expected interval.
  2. Stop the publisher. On an ESPHome node, disconnect it from power or disable the interval automation. On a script-based publisher, stop the process.
  3. Wait for the detection window plus one heartbeat interval. Confirm the timer helper goes idle and the template binary sensor turns on.
  4. Confirm the alert fires and the notification arrives.
  5. Acknowledge the alert. Confirm the repeat notifications stop but the alert entity remains in the acknowledged state.
  6. Restart the publisher. Confirm the heartbeat resumes, the timer restarts, the binary sensor turns off, and the done message arrives.

If the alert does not fire, the diagnostic path is ordered: check the broker log for the heartbeat topic, check the Home Assistant automation trace for the MQTT trigger, check the timer helper state, and check the template binary sensor. Each step isolates one layer. The automation documentation describes traces as the way to see which trigger started a run, which is the fastest way to confirm whether the heartbeat automation fired at all.

Graceful degradation when the broker or Home Assistant is down

A dead man’s switch built on MQTT inherits MQTT’s failure modes. If the broker is down, no heartbeats arrive and the alert fires — which is correct, but the message should say so. If Home Assistant is down, the alert cannot fire at all, and the operator needs a separate signal that Home Assistant itself is alive.

Three practical measures reduce the ambiguity:

  • Keep the broker local. A Mosquitto instance on the same network segment as Home Assistant means a WAN outage does not stop heartbeats. The trade-off is that a local network failure now looks like a pump failure; the alert message should name the monitor and the topic so the operator can distinguish the two.
  • Watch the broker’s own availability. Home Assistant’s MQTT integration publishes birth and will messages on homeassistant/status by default. A separate automation can notify when the MQTT integration itself goes offline, which separates “the broker is down” from “the pump monitor is silent.”
  • Do not route the alert through a cloud dependency. A local notification path — the Home Assistant companion app on the local network, or a local siren — keeps the alert working when the WAN is down. The trade-off is reach: a local-only alert does not reach a phone that is off the network. For a sump pump, the local siren is the more reliable of the two.

Mosquitto’s own configuration affects this. The allow_anonymous option defaults to false unless no listeners are defined, in which case it is set to true but connections are only allowed from the local machine, per the mosquitto.conf manual. A broker that accepts anonymous connections from the network is a liability; a broker that requires credentials is the baseline. Persistence settings affect retained messages and queued messages, not the heartbeat itself, because the heartbeat is not retained.

When a mechanical timer or a plain switch is the better choice

This design assumes a monitor that can publish to MQTT and a Home Assistant instance that is already maintained. If either assumption is shaky, the dead man’s switch adds a failure mode rather than removing one. A mechanical timer that sounds an alarm if it is not reset, or a plain float switch wired to a local buzzer, has fewer moving parts and no broker. For a sump pump in a basement with no other smart-home infrastructure, the plain switch is the more reliable engineering choice.

The MQTT design earns its complexity when the monitor already exists, when the operator already runs Home Assistant, and when the goal is to catch the specific failure of a silent-but-powered monitor. In that setting, the heartbeat-timeout pattern is a small addition to an existing stack, and the diagnostic path is short because the broker and the automation are already visible.

Maintenance: keep the pair in sync

The heartbeat interval and the detection window are a matched pair, and the most likely future failure is a maintainer changing one without the other. Write both numbers on a label next to the pump, and write them in the automation’s description. If the publisher is moved to a different device or a different firmware version, re-run the stop-the-publisher test before trusting the switch again.

If the broader goal is to audit the small systems that quietly run a household, the same discipline applies: list the monitors, list what each one proves, and list what happens when each one goes silent. The audit procedure for small systems is a useful companion to this article for that reason.

FAQ

Why not just use the ESPHome availability topic as the alarm?

Because availability reflects the MQTT connection, not the publishing loop. A device that stays connected while its heartbeat stops will still show as online. The heartbeat timeout covers that case; the availability topic does not.

Should the heartbeat be retained?

No. A retained heartbeat means a new subscriber sees the last beat immediately, which can mask a stale publisher. Retain is appropriate for discovery and availability messages, not for liveness.

What happens if the broker goes down?

The heartbeat stops arriving and the alert fires. That is the correct behavior, but the message should name the monitor and the topic so the operator can tell a broker outage from a pump monitor failure. A separate automation watching the MQTT integration’s own status helps separate the two.

How many missed beats should trigger the alert?

That is a design choice, not a sourced standard. Three missed beats is a common starting point because it tolerates normal jitter without a long detection window. The number should be chosen against the pump’s risk profile and documented next to the pump.

Does the Alert integration support a different notification cadence over time?

Yes. The repeat option can be a list of numbers, so the first reminder can come sooner than later ones. The Alert integration documentation describes this behavior.