Garage door with sonoff & tasmota

Hey everyone. I have a question for all you Tasmota experts.

First some background:
I have my garage door set up with a sonoff basic & Tasmota, which can open and close the door when the relay is activated, and sense the door state with a reed switch attached to GPIO14. The open/close mechanism is basically just a toggle on the garage door opener when you bridge two contact points, so I have tasmota set to open the relay 1 second after it is closed. That all works as intended.

Now the problem:
In home assistant, I have a “cover.garagedoor” set up with “platform: mqtt”, but home assistant doesn’t seem to distinguish whether or not the door is already closed when calling “cover.close_cover”. Because the MQTT commands for opening and closing the door are both the same (close the sonoff’s relay), this manifests as me saying “Hey google, close the garage door”, and the garage door then opening.

My question:
Is there a way to make tasmota listen to different MQTT commands under different circumstances? Right now it would listen for “cmnd/GarageDoor/POWER ON” and “cmnd/GarageDoor/POWER OFF”. I would like it to pehaps listen for “cmnd/GarageDoor/OPEN” (but ignore this if GPIO14 is not grounded, i.e., the door is already open) and “cmnd/GarageDoor/CLOSE” (but ignore this if GPIO14 is grounded, i.e., the door is already closed) and then in the background send itself the POWER ON command when required.

Hopefully that all makes sense. I believe there is a way to accomplish this with Tasmota’s rules, but I am far from well-versed in their usage. Can anybody help?

I have a similar set up but I don’t use MQTT. I use zwave stuff. But in the end it is functionally the same.

Here are the code snippets for how I operate my garage doors:

  - alias: Toggle North Garage Door Switch
      platform: state
      entity_id: switch.garage_door_north_operator_switch
      to: 'on'
        seconds: 1
      service: homeassistant.turn_off
      entity_id: switch.garage_door_north_operator_switch

  - platform: template
        friendly_name: 'North Garage Door'
        value_template: "{{ is_state('binary_sensor.garage_door_north_position_sensor', 'on') }}"
          service: script.turn_on
          entity_id: script.open_gdn
          service: script.turn_on
          entity_id: script.close_gdn
          service: switch.turn_on
          entity_id: switch.garage_door_north_operator_switch
        icon_template: "{% if not is_state('binary_sensor.garage_door_north_position_sensor', 'off') %}mdi:garage-open{% else %}mdi:garage{% endif %}"

    alias: Close the North Garage Door
      - condition: state
        entity_id: binary_sensor.garage_door_north_position_sensor
        state: 'on'
      - service: switch.turn_on
        entity_id: switch.garage_door_north_operator_switch
    alias: Open the North Garage Door
      - condition: state
        entity_id: binary_sensor.garage_door_north_position_sensor
        state: 'off'
     - service: switch.turn_on
       entity_id: switch.garage_door_north_operator_switch

Then in my Alexa app I have a routines set up that calls the scripts to open and close the door.

So if I say “Alexa, open the north garage door” it triggers the script to open the door which checks that the door is closed before toggling the garage door operator switch.

And since I use those same scripts in the open_cover/close_cover then that prevents me from, for example, “opening” the door if it’s already open (which would then close it).


Genius in this solution’s simplicity! I love it.


If you feel it’s deserved then give it a “solution” tick mark. That way others see that it’s solved and move on or if they have the same question they can know to check it out.

Also, I had no idea you could have conditions in scripts! This opens up a new world of possibilities to me.

1 Like

Here’s my solution, I based my template off of my previous MQTT cover. I also found I didn’t need the scripts, I could just use the body of the script in the template actions for the new cover. I didn’t need the automation because I configured Tasmota to make the sonoff’s relay open again after 1 second. Renamed my old garage door entity, and named the new one what the old one used to be. No other actions needed to be changed.


- platform: mqtt
  name: garagedoor_mqtt
  command_topic: "cmnd/GarageDoor/POWER"
  payload_open: "ON"
  payload_close: "ON"
  payload_stop: "ON"
  state_topic: "cmnd/GarageDoor/POWER2"
  state_open: "ON"
  state_closed: "OFF"
  optimistic: false
  retain: false

- platform: template
      friendly_name: Garage Door
      value_template: '{{ states.cover.garagedoor_mqtt.state == "open" }}'
      icon_template: '{% if states.cover.garagedoor_mqtt.state == "open" %}mdi:garage-open{% else %}mdi:garage{% endif %}'
        - condition: state
          entity_id: cover.garagedoor_mqtt
          state: 'closed'
        - service: cover.open_cover
          entity_id: cover.garagedoor_mqtt
        - condition: state
          entity_id: cover.garagedoor_mqtt
          state: 'open'
        - service: cover.close_cover
          entity_id: cover.garagedoor_mqtt

Thanks you very much, works like a chram!

I have the same setup but i’m having an issue with recording the sate of the sensor in home assistant. like if i restart home assistant the state of the sensor becomes unknown. then if i set retain to on, the door will open automatically when i restart home assistant. can you please share with us the console command to decouple the relay from the sensor switch and how to retain (only) the sensor status in home assistant?

Do NOT set retain in home assistant. What you want to do is set “SwitchRetain on” in Tasmota.

The retain flag in Home Assistant makes the MQTT topics that HA sends be retained by the broker – that’s the command topics to open or close the door. “SwitchRetain on” on the Tasmota side will make the topics that Tasmota sends for the state of the switch be retained by the broker. This will allow HA to pick up the state of the switch (whether your door is open or closed) on restart.

In my experience, you almost never want to set “retain: true” in HA.

Steve can you explain to noob (me) how to configure tasmota in the way that after 1 second after switch on switch gets off.

Sure. Look up the Tasmota command “PulseTime”.

I have mine set to 10, which means it automatically turns the relay off after 1 second.

but how do I turn it on and then off in 1 second i.e via mqtt? Can I chain commands somehow?

First, set the PulseTime for the device, which you can do via MQTT using the command

cmnd/[Topic]/PulseTime 10

You only have to do this once.

Then, whenever the device is turned on, it will automatically turn off after the delay configured by PulseTime. State information is sent by the device over MQTT on the


topic, so you can tell whenever it’s turned on on off.

Now I get this. PulseTime is a permanent setting. Great, thank you.

1 Like

Awesome! Great solution!

Hate to bring this one back from the dead, but when I set the PulseTime on my Sonoff 4ch Pro R3, it only sets it for the first relay. How do I do that for the other ones? So far I’ve only been able to get the PulseTime setting to work via the console. MQT shows Command Unknown.

I haven’t tried this myself, because I only have one relay on my device, but going with the paradigm of other TASMOTA commands, you could try PulseTime1, PulseTime2, etc.

Yep. It checks out:

You are amazing! That did it! Time to go install this in the garage.

With “cover.west_door_west_door” for door ID"
And “binary_sensor.west_door_switch2”

Where do I put these in the code above?
I will have 3 garage doors to start with and I am not sure if I need 3 copies of each, meaning 1 each cover and MQTT?
Thanks in advance.