Garage Door Opens on Server Reboot

I recently added a Shelly 1 relay to my garage door opener. At first it was okay, but I wasn’t able to see the state of the garage, which is kind of unsettling if something were to happen, and the garage door were to open in the middle of the day while I’m at the office. So I added a reed switch to the Shelly, and converted over to MQTT. Now everything is working well, the garage door icon in Lovelace shows its state, and everything is good.

Except, whenever I reboot the Home Assistant VM , the garage door opens on its own.

Does anyone know what’s going on? Probably a misconfiguration, but I don’t see it.

I’m using the Mosquitto broker from the HA Add-on store. Initially I had a problem where when I rebooted Home Assistant Core (from the supervisor menu), it wouldn’t know the state of the garage door. But I solved that by adding ‘retain: true’ on both the Shelly 1 and Home Assistant cover configuration.

Any idea why this is happening?

Here is the cover configuration:

# Garage Door
cover:
  - platform: mqtt
    name: "Garage Door"
    position_topic: "shellies/shelly1-garage/input/0"
    command_topic: "shellies/shelly1-garage/relay/0/command"
    availability_topic: "shellies/shelly1-garage/online"
    retain: true
    payload_open: "on"
    payload_close: "on"
    payload_stop: "on"
    position_open: 0
    position_closed: 1
    payload_available: "true"
    payload_not_available: "false"

Here is the Mosquitto configuration

logins: []
anonymous: false
customize:
  active: false
  folder: mosquitto
certfile: fullchain.pem
keyfile: privkey.pem
require_certificate: false

This is what I see after a reboot of the server:

Why is HA sending all those open/close commands? Ideally what happens is HA doesn’t touch the garage door on boot/reboot.

It’s probably an automation you have that does it.

No garage door automatons at all. This wasn’t doing anything on its own before switching to MQTT.

Here’s the culprit:

That instructs Home Assistant to publish a payload to the command_template as a retained message. That means the payload is stored on the broker. Whenever the Shelly device reconnects to the broker it will receive the last stored command.

Fixing this requires two steps:

  1. Remove retain: true from your cover’s configuration then restart Home Assistant.
  2. Purge the retained message from the broker. The way to do it is to publish an empty string to the command_topic. You can use Developer Tools > Services with mqtt.publish or an MQTT client like MQTT Explorer.

Edit: I must have not applied the configuration settings correctly. I repeated the same steps a few times, and whatever combination of restarts I did this time worked. The garage door no longer opens by itself during a reboot. Thank you, @123

Okay, so I think I understand what the issue is, and I can even see that when I subscribe to the command topic (from the MQTT integration), that I instantly receive an “on” command. Which is why the garage door is opening on a reboot, I assume: the Shelly connects to the broker, and immediately gets told to turn on.

Imgur: The magic of the Internet

I edit the configuration.yaml file so that retain is false:

# Garage Door
cover:
  - platform: mqtt
    name: "Garage Door"
    position_topic: "shellies/shelly1-garage/input/0"
    command_topic: "shellies/shelly1-garage/relay/0/command"
    availability_topic: "shellies/shelly1-garage/online"
    retain: false
    payload_open: "on"
    payload_close: "on"
    payload_stop: "on"
    position_open: 0
    position_closed: 1
    payload_available: "true"
    payload_not_available: "false"

and send a payload (with the retain flag on) from the developer tab:

Imgur: The magic of the Internet

I restart Home Assistant Core and I can now see that when I subscribe to the command topic, I don’t receive the “on” command automatically.

So it looks like a success, right?
Unfortunately when I reboot the server, it opens the garage, and when I subscribe to the command topic, I get an initial “on” command once again. Which is the source of this problem.

What is sending an “on” command with the retain flag set to true at boot? Is the Mosquitto configuration doing something weird? I get the problem, I can see the problem, but I don’t know why the suggested fix doesn’t survive a server reboot.

Glad to hear it ultimately worked.

You should know that this is a fairly common issue. In general, don’t set any entity’s retain option to true unless you absolutely want the behavior I described.

When I first got the reed switches, the problem was that upon a server reboot, the state of the shelly would be “unknown.” So I did some skimming through forums, and it turns out you need retain on for the server to know the last state of the relay.

I know now that that means to have retain set on the shelly side, but at the time I just went HAM setting retain on both sides.

Now I think I understand what’s going on with MQTT especially with the retain things so I can be more calculated next time.

Correct. It’s advantageous to have a device’s state stored on the broker. For example, a weather station’s temperature, humidity, rainfall amount, etc or a lock or garage door’s state. After a restart, Home Assistant connects to the broker and gets the retained values.