MQTT Device Trigger config getting lost on restart

Hi! I recently started using home assistant (ver 2022.9.7, docker) and been trying to integrate my existing homemade IoT devices.

One of them is a doorbell, which sends a message saying “beep” to the mqtt topic “doorbell” when you press the button. Implemented in micropython for esp8266.

The process of figuring out which integration to use was awkward. “Button” isn’t it, “binary sensor” isn’t it (needs an “off” message, although i’ve seen people in the forums implement doorbells that way).

Eventually I asked on twitter and a friend told me about mqtt device trigger.

It just has the caveat of not being configurable the normal way.

So I did the initial config like this:

mosquitto_pub -h alarmpi.home -t homeassistant/device_automation/doorbell/config -m '{"automation_type": "trigger", "type": "button_short_press", "subtype": "doorbell", "payload": "beep", "topic": "doorbell", "device": {"identifiers": ["doorbell"], "name": "doorbell", "manufacturer": "dx"}}'

And later modified the code of the device so that it itself sends this same payload after rebooting.

The problem is: this doorbell never reboots, so the config is only sent once. Home assistant restarts a lot more than the doorbell. Every time home assistant restarts (with docker restart or by clicking restart in developer tools), it forgets about the discovered config.

I’d like home assistant to listen to this mqtt topic at all times, otherwise i can’t trust it to implement automations for the doorbell.

Before a restart, the debug dump looked like this:


    "mqtt_debug_info": {
      "entities": [],
      "triggers": [
        {
          "discovery_data": {
            "topic": "homeassistant/device_automation/doorbell/config",
            "payload": {
              "automation_type": "trigger",
              "type": "button_short_press",
              "subtype": "doorbell",
              "payload": "beep",
              "topic": "doorbell",
              "device": {
                "identifiers": [
                  "doorbell"
                ],
                "name": "doorbell",
                "manufacturer": "dx"
              },
              "platform": "mqtt"
            }
          },
          "trigger_key": [
            "device_automation",
            "doorbell"
          ]
        }
      ]
    }

And then it becomes this:

    "mqtt_debug_info": {
      "entities": [],
      "triggers": []
    }

And if I don’t manually intervene, home assistant just stops being aware of the doorbell.

The one integration I set up stays there with a blank “trigger” select, failing silently, and similarly silently populating the field as soon as it receives a config message over mqtt:

This would be pretty easy to solve if i could just define an mqtt listener config statically in a yaml file, but for some mysterious reason device trigger only supports discovery for its configuration.

Solutions I can think of:

  • Spam the mqtt config topic every now and then
    • (with shorter intervals = less risk = more noise over the wire)
  • Figure out what the legacy mqtt triggers were about and if they can still be used
  • Some kind of automation on home assistant to reboot the doorbell
    • (tricky, as this code doesn’t receive incoming mqtt messages and busyloops on reading the gpio)
  • Patch home assistant to persist discovered mqtt triggers across restarts
    • (not familiar with this codebase so it’ll take a lot more effort than i’d like to spend. also the device/entity itself seems to be persisting, just not its triggers, so the problem might not be what i think it is)

None of these seem particularly satisfying, imo.

Publish the discovery payload as a retained message. It will be stored on the broker.

When Home Assistant restarts, it will automatically connect to the broker, subscribe to the discovery topic, and immediately receive the MQTT Device Trigger’s discovery payload (because it’s stored on the broker)).

1 Like

Omg thank you, that’s a beautiful solution. I come from AMQP so i thought retained == durable == surviving broker restarts. It’s actually a fancier thing.

It does get lost if i restart mosquitto so I guess I should figure out how to get persistence going (right now it seems broken because of file write permission issues)

Thank you!

1 Like