MQTT Garage door opener with 2 reed switches on a Wemos D1mini Pro

Hello Everyone,
I need a little direction help please! I have a Wemos D1 mini Pro running Tasmota as a garage door opener, with 2 reed switches, one for open and the other for closed.

OK I have managed to copy, create and change an MQTT cover, add the two reed switches as binary sensors. The arrows on the cover in the UI work fine and open close the door, the sensors also report correctly when the door is either open or closed.

Now what I am trying to get my head around is the display of the sensors. I would like to have below the up,stop and down arrows is just one text line saying the door is either closed, opening, open or closing. the icing on the cake would be for the garage icon to change when its open and closed.

Coming from openhab, I just had a rule that did this easily and displayed the text, but I cant figure it in HA.

Heres my config.yaml bit -:

#Garage door open and close sensors on D1mini
  - platform: mqtt
    name: "Garage Door Open Sensor"
    state_topic: "cmnd/GarageDoorSensor/POWER1"
    payload_available: "Online"
    payload_not_available: "Offline"
    qos: 1
  - platform: mqtt
    name: "Garage Door Closed Sensor"
    state_topic: "cmnd/GarageDoorSensor/POWER2"
    payload_available: "Online"
    payload_not_available: "Offline"
    qos: 1

cover:
  - platform: mqtt
    name: "Garage Door"
    command_topic: "cmnd/GarageDoor/POWER"
    #state_topic: "stat/GarageDoor/POWER"
    payload_open: "ON"
    payload_close: "ON"
    payload_stop: "ON"
    state_open: "open"
    state_closed: "closed"
    value_template: '{{ value_json.status }}'
    qos: 0

and a screenshot of the UI

image

I have looked at loads of the threads on here which have been helpful but I cant figure out how to combine the two sensors with a rule and output the result to the UI.

Could someone kindly point me in the right direction please?

Also was an MQTT cover the best way to go with this?

Cheers

Simon

but I cant figure out how to combine the two sensors

The answer for all things ā€˜combining sensorsā€™ is ā€œTemplateā€. Template sensor combines X sensors into one. Template Cover combines a cover and sensors into a single cover. etc, etc, etc.


Here are some options.

You could use just a single reed sensor and subscribe to that state topic in the MQTT cover. If itā€™s closed, itā€™s not open. If itā€™s not closed, itā€™s open. The little garage door icon will show you the state. And, when itā€™s reporting as ā€˜closedā€™ it wonā€™t let you push the closed button (not that it matters, itā€™s all the same button).

But if you want to use all the sensors, here are 2 more options.

  1. Combine them all into a single Template Cover

Iā€™d rename your mqtt cover to ā€œGarage Door MQTTā€ or something. Weā€™re still going to use that, it will just be in the background.

cover:
  - platform: template
    covers:
      garage_door:
        friendly_name: "Garage Door"
        # If open or closed sensor are on, report that state. 
        # Else....it's probably opening or closing....unless someone stopped it....Then it's neither.
        # To determine if it's opening or closing....figure out which sensor changed last.
        value_template: >-
          {% set op = as_timestamp(states.sensor.garage_door_open_sensor.last_changed) %}
          {% set cl = as_timestamp(states.sensor.garage_door_closed_sensor.last_changed) %} 
          {% if is_state('sensor.garage_door_open_sensor', 'on') %}
            open
          {% elif is_state('sensor.garage_door_closed_sensor', 'on') %}
            closed
          {% elif (op - cl) > 0 %}
            closing
          {% else %}
            opening
          {% endif %}
        open_cover:
          service: cover.open_cover
          entity_id: cover.garage_door_mqtt
        close_cover:
          service: cover.close_cover
          entity_id: cover.garage_door_mqtt
        stop_cover:
          service: cover.stop_cover
          entity_id: cover.garage_door_mqtt

Sooo, if you stop the open or close action half way, this will just report ā€˜openingā€™ or ā€˜closingā€™ forever. Maybe thatā€™s ok?


  1. Combine just the open/close sensors into a single sensor.

Finally, if you want to keep it exactly as you have, with the cover on one line and the state on another, you could just create a template sensor. This sensor will be the exact same value_template as the template_coverā€¦itā€™s just a new sensor instead of a new cover.

sensor:
  platform: template
  sensors:
    friendly_name: "Garage Door State"
      value_template: >-
        {% set op = as_timestamp(states.sensor.garage_door_open_sensor.last_changed) %}
        {% set cl = as_timestamp(states.sensor.garage_door_closed_sensor.last_changed) %} 
        {% if is_state('sensor.garage_door_open_sensor', 'on') %}
          open
        {% elif is_state('sensor.garage_door_closed_sensor', 'on') %}
          closed
        {% elif (op - cl) > 0 %}
          closing
        {% else %}
          opening
        {% endif %}

Now just use this new template sensor in place of your other ones.

1 Like

Hey, Thanks for all your time and help Jim. I will give these all a try and get to grips with how they work.
I take your point on the Stop command with the state. I may try and get my head around a timer that works out if the door has taken to long to open or close then sends an alert some howā€¦whoa there lets walk before I runā€¦

Thanks again will post an update when I egt it going but I need some some sleep my brains friedā€¦

Cheers
Simon

Hey, So just a follow up for others if they need help with a config like this. This is what I ended up with for now, I went for your last option Jim.

sensor:
  - platform: template
    sensors:
      garage_door_state:
        friendly_name: "Garage Door State"
        value_template: >-
          {% set op = as_timestamp(states.binary_sensor.garage_door_open_sensor.last_changed) %}
          {% set cl = as_timestamp(states.binary_sensor.garage_door_closed_sensor.last_changed) %} 
          {% if is_state('binary_sensor.garage_door_open_sensor', 'on') %}
            open
          {% elif is_state('binary_sensor.garage_door_closed_sensor', 'on') %}
            closed
          {% elif (op - cl) > 0 %}
            closing
          {% else %}
            opening
          {% endif %}
          
cover:
  - platform: mqtt
    name: "Garage Door"
    command_topic: "cmnd/GarageDoor/POWER"
    #state_topic: "stat/GarageDoor/POWER"
    payload_open: "ON"
    payload_close: "ON"
    payload_stop: "ON"
    state_open: "open"
    state_closed: "closed"
    value_template: '{{ value_json.status }}'
    qos: 0

I did change the code slightly but now works very well. I have even managed to install Alexa media player and get a notification when the door is open and closed. Thanks again for all your help.

Simon

1 Like

Hi @Crumpy10
thanks for your great example. actually i will setup my garage new and would like to use your code. my garage door motor needs only a short impulse with the relay. so if i want to open the garage door i need to switch the relay to ON and back to OFF. do you have a smart idea how to do this? now it looks like thisā€¦ Automation?

cover:
  - platform: mqtt
    name: "Garage Madame"
    command_topic: "garage/cmnd/sensor29"
    state_topic: "garage/stat/sensor29"
    payload_open: "0,on"
    payload_close: "0,on"
    payload_stop: "0,on"
    state_open: "open"
    state_closed: "closed"
    value_template: '{{ value_json.status }}'
    qos: 0
- platform: template
  sensors:
    garage_door_state:
      friendly_name: "Garage Madame Status"
      value_template: >-
        {% set op = as_timestamp(states.binary_sensor.garagewemospro_switch1.last_changed) %}
        {% set cl = as_timestamp(states.binary_sensor.garagewemospro_switch2.last_changed) %} 
        {% if is_state('binary_sensor.garagewemospro_switch1', 'on') %}
          open
        {% elif is_state('binary_sensor.garagewemospro_switch2', 'on') %}
          closed
        {% elif (op - cl) > 0 %}
          closing
        {% else %}
          opening
        {% endif %}

br Dave

1 Like

Hey, no problem.
So it depends on the hardware you are using. I am using a Wemos D1Mini running Tasmota.
In Tasmota I think it was Power settings and Pulsetime that you need to look at.
This is how I did it. My door opener also requires a pulse on to open and close the door.
Have a look here if you decide to use Tasmota-:

Hi @Crumpy10
thanks for thisā€¦ yes i m using already Tasmota for this projectā€¦

Great solution for a cover with 2 reeds, like I have. But my gate sometimes gets stuck midway, so Iā€™m trying to add an ā€œerrorā€ state to the sensor. It should be like this: if the gate is in the opening or closing state for longer than X seconds (whatever your gate takes to fully open or close), then the sensor reports error (instead of opening or closing).

Can you think of a way to do that with this template? Thanks!

I cant think of how to do it off hand (I am still learning!) but I would probably start by looking at doing an automation that looks at your two reed sensors and if they are both in the same state for a given time then send a notification that the gate may be jammedā€¦