Garage door cover with two reed sensors - template question

Hi there,
I am struggeling to get the hang of my garage door.
I’m using two mqtt reed switches to report the positions up (garagerechtsoben_contact) or down (garagerechtsoben_contact).
This works as a charm. But I can’t figure out, how to detect the “rising edge” from a sensor from off to on, and chose the appropiate “closing” or “opening” state.

Can anybody help me?
Thank you!

  - platform: template
    covers:
      garage_door:
        device_class: garage
        friendly_name: "Rechte Garage"
        unique_id: "rechteGarage"
        value_template: >
         {% if is_state('binary_sensor.garagerechtsunten_contact', 'off') %}
           closed
         {% elif is_state('binary_sensor.garagerechtsoben_contact', 'off') %}
           open
         {% else %}
           closing #this bit right here
         {% endif %}
        open_cover:
          - service: switch.turn_on
            target:
              entity_id: switch.shelly_garage
        close_cover:
          - service: switch.turn_on
            target:
              entity_id: switch.shelly_garage

One way would be to make template sensors triggered on state changes and use that in your cover templates. For example, you can access the from and to states on the trigger state object of those binary sensors. If the down binary sensor goes from true to false, it’s opening.

Thank you for your solution!
I did not know about the possibility of template sensors at all, since I’m very new to Homeassistant (switching from fhem).
I was able to put the following sensor together:

template:
  - trigger:
    - platform: state
      entity_id:
        - binary_sensor.garagerechtsoben_contact
        - binary_sensor.garagerechtsunten_contact
    sensor:
      - name: 'rechteGarageStatus'
        unique_id: 'rechteGarage_status'
        state: > 
          {% if (trigger.to_state.state == 'off' and trigger.entity_id == 'binary_sensor.garagerechtsoben_contact') %}
            open
          {% elif (trigger.to_state.state == 'off' and trigger.entity_id == 'binary_sensor.garagerechtsunten_contact') %}
            closed
          {% elif (trigger.to_state.state == 'on' and trigger.from_state.state == 'off' and trigger.entity_id == 'binary_sensor.garagerechtsoben_contact') %}
            closing
          {% elif (trigger.to_state.state == 'on' and trigger.from_state.state == 'off' and trigger.entity_id == 'binary_sensor.garagerechtsunten_contact') %}
            opening
          {% endif %}

In the definition of my garage door I just referenced the calculated state:

cover:  
- platform: template
    covers:
      garage_door:
        device_class: garage
        friendly_name: "Rechte Garage"
        unique_id: "rechteGarage"
        value_template: '{{ (states.sensor.rechtegaragestatus.state) }}'
        open_cover:
          - service: switch.turn_on
            target:
              entity_id: switch.shelly_garage
        close_cover:
          - service: switch.turn_on
            target:
              entity_id: switch.shelly_garage

Which works like a charm. Only on the HomeAssistant startup there is no predefined state. Which I will get to by using an else construct in the sensor.

So many thanks! I marked your post as the solution.

2 Likes

I’m glad it works for you (and welcome to the community)!

Templating is very powerful.

Something else to avoid errors and unknown states during startup is this:

Avoid using states.sensor.temperature.state , instead use states('sensor.temperature') . It is strongly advised to use the states() , is_state() , state_attr() and is_state_attr() as much as possible, to avoid errors and error message when the entity isn’t ready yet (e.g., during Home Assistant startup).

value_template: '{{ states("sensor.rechtegaragestatus") }}'

I think you can probably change one of your if conditions to be the else (probably the closed one; I don’t think logically there are other combinations to cater for).

The template works great, however the current version of HA restores the last state at startup. Setting a default state with an else condition doesn’t change this behaviour. Ideally I want to refresh with the current actual state and not use a default or the previous state.

Example: if garage door is closed when HA is shutdown/restarted, the state will be closed when it starts, even if the garage is open.

So far I haven’t come up with a quick/easy way to automate refreshing the real state. A call to
homeassistant.update_entity with the sensor template’s entity ID doesn’t do it.

Any advice?

How is your device integrated? MQTT like for the OP?

No, using a Shelly Uni device with HA’s built-in integration. This single device provides the switch entity and two sensor entities which I’m using with reed switches for opened and closed states.

After restarting, the sensor entities on the device itself show their correct status. Getting that into the template sensor is where I’m stuck.

I’ve tried putting an additional trigger into the template for when HA starts up, but it seems that might get evaluated too early as it makes the state an error condition and the sensor “unavailable” after restart. Changing state on the physical sensor connections causes it to update and reflect the correct state.

I’ll start a new topic for this as it’s really a state issue and not specific to garage covers, plus this topic is already marked solved.

The solution was providing a trigger for HA start event in the sensor template, however, only checking for the open and closed states, because the to_state and from_state aren’t yet populated and produce an error.

That gives you something like this for the sensor template:

template:
  - trigger:
      - platform: state
        entity_id:
          - binary_sensor.garage_door_open
          - binary_sensor.garage_door_closed
      - platform: homeassistant
        event: start
    sensor:
      - name: "Garage Door Status"
        unique_id: "some_unique_id_garage1234"
        state: >
          {% if is_state('binary_sensor.garage_door_closed', 'on') %}
            closed
          {% elif trigger.platform != 'homeassistant' %}
            {% if (trigger.to_state.state == 'off' and trigger.from_state.state == 'on' and trigger.entity_id == 'binary_sensor.garage_door_open') %}
              closing
            {% elif (trigger.to_state.state == 'off' and trigger.from_state.state == 'on' and trigger.entity_id == 'binary_sensor.garage_door_closed') %}
              opening
            {% else %}
              open
            {% endif %}
          {% else %}
            open
          {% endif %}

I’ve since modified my own installation to provide real-time states by monitoring the door shaft, keeping the state change of the magnetic sensors as a backup. This can also easily be made to monitor the motor spindle instead, all using a ready-made rotary Ikea Tradfri sound controller. In total, these are the states also supported by Homekit so it all dovetails nicely there: Open, Closed, Opening, Closing, Stopped.

1 Like

This sounds interesting, are you able to share the code and picture of the hardware installed?

Use of the IKEA E1744 Symfonisk Rotary Controller / remote requires a blueprint.

EPMatt’s Blueprint: 🎮 ZHA, deCONZ, Zigbee2MQTT - Ikea E1744 SYMFONISK rotary remote Universal blueprint - all actions - control lights, media players and more with Hooks

Mandatory FIX for the Blueprint: 🎮 ZHA, deCONZ, Zigbee2MQTT - Ikea E1744 SYMFONISK rotary remote Universal blueprint - all actions - control lights, media players and more with Hooks - #109 by hoffsta

The rotary controller has been wrapped with multiple layers of 1mm EVA foam to add cushioning and increase friction, making sure that it always rotates in response to the garage door shaft.

1 Like
# Garage Door Template from Shelly Uni Switch and Sensors
cover:
  - platform: template
    covers:
      garage_door:
        device_class: garage
        friendly_name: "Garage Door"
        unique_id: "Garage_Door_shellyUNI_170"
        value_template: '{{ states("sensor.garage_door_status") }}'
        open_cover:
          - service: switch.turn_on
            target:
              entity_id: switch.garage_door_button
        close_cover:
          - service: switch.turn_on
            target:
              entity_id: switch.garage_door_button
        stop_cover:
          - service: switch.turn_on
            target:
              entity_id: switch.garage_door_button
        icon_template: >-
          {% if is_state('sensor.garage_door_status', 'closed') %}
            mdi:garage
          {% elif is_state('sensor.garage_door_status', 'closing') %}
            mdi:garage-alert
          {% elif is_state('sensor.garage_door_status', 'opening') %}
            mdi:garage-alert
          {% elif is_state('sensor.garage_door_status', 'error') %}
            mdi:magnet-on
          {% else %}
            mdi:garage-open
          {% endif %}
template:
  - trigger:
      - platform: state
        entity_id:
          - binary_sensor.garage_door_open
          - binary_sensor.garage_door_closed
          - input_text.garage_rotary_state
      - platform: homeassistant
        event: start
    sensor:
      - name: "Garage Door Status"
        unique_id: "Garage_Door_status_shellyUNI_170"
        state: >
          {% if is_state('binary_sensor.garage_door_closed', 'on') and is_state('binary_sensor.garage_door_open', 'on') %}
            error
          {% elif is_state('binary_sensor.garage_door_closed', 'on') %}
            closed
          {% elif is_state('binary_sensor.garage_door_open', 'on') %}
            open
          {% elif trigger.platform != 'homeassistant' %}
            {% if (trigger.to_state.state == 'off' and trigger.from_state.state == 'on' and trigger.entity_id == 'binary_sensor.garage_door_open') %}
              closing
            {% elif (trigger.to_state.state == 'off' and trigger.from_state.state == 'on' and trigger.entity_id == 'binary_sensor.garage_door_closed') %}
              opening
            {% elif is_state('input_text.garage_rotary_state', 'opening') %}
              opening
            {% elif is_state('input_text.garage_rotary_state', 'closing') %}
              closing
            {% endif %}
          {% else %}
            open
          {% endif %}

Thanks for all the info. What config/code is setup for input_text.garage_rotary_state and does it track the exact door position or it outputs opening/closing?

Yes, that text input contains only the English state value of the door and it’s populated by the Blueprint I mentioned. In fact it’s the only action I specify in the blueprint for each state it can report.

It was an easy way to reference the output of the blueprint from the YAML definition.

Thank you.

Have any of you, but especially @kirijanker or @HVR88, tried adding this type of setup to HomeKit? I know that you can add a garage gate controller to HomeKit but I understood from other topics that people were missing the “opening”/“closing” statuses. Thanks to your tips, I managed to get the status in HA but there are several entities that handle all of this, right? One is the switch that controls the gate and the other one keeps the status. Is it possible to combine this and get all of it into HomeKit?

Edit: I checked and it actually works perfectly!

Another question though. Do you guys use tasmotized devices for your setup? I would like to use a sonoff and add two magnetic sensors but I cannot manage. I managed to add one but do not understand how to add a second one. I guess it is more of a Tasmota question but I thought I would try here.

No. If you want the serious DIY approach as has been discussed, use a Shelly Uni - it’s the best device for the job.

BUT… If you have a suitable opener, I just found out about a ready-to-go device this past week that pulls a lot more info. RATGDO (RAT Garage Door Opener) ratGDO

It works with virtually any residential Chamberlain or Liftmaster manufactured garage door opener and also offers basic support for other brands which use dry contacts to control the door.

It can control the door and report back the actual status of the door (closed, opening, open, closing). For Security + 2.0 door openers, the obstruction sensors, light, wireless lockout and even motion sensing are also integrated.

Had I known about this device earlier, I’d never have bothered with the Shelly Uni or the IKEA rotary encoder.

It made more sense to me to just make a boolean sensor called garage was open. Then I check that in the template and have two automations to change it when it opens and closes.

value_template: >
  {% if is_state('binary_sensor.garage_door_closed_sense', 'on') %}
    closed
  {% elif is_state('binary_sensor.garage_door_open_sense', 'on') %}
    open
  {% else %}
    {% if is_state('input_boolean.garage_was_open', 'off') %}
      opening
    {% else %}
      closing
    {% endif %}
  {% endif %}

and

alias: 4 Garage Was Open Set
description: ""
trigger:
 - platform: state
   entity_id:
     - cover.garage_door
   to: open
condition: []
action:
 - service: input_boolean.turn_on
   data: {}
   target:
     entity_id: input_boolean.garage_was_open
mode: single

Thank you for your reply. I just ordered a Shelly Uni. Do you use it with ESPHome or Shellys original firmware? I asked about using tasmota but you suggested a different hardware as well so I am wondering if your advice is mainly not to use Tasmota or not to use Sonoff SV or both?

I am not sure what happened but I am getting dozens and dozens of “device unavailable” messages in HA for my Sonoff SV. The log in the console for Tasmota gives me rc -2 error. It might be weak wifi signal because I get readings of -79 to -75. At the same time, I feel like I never had those issues before. My router is maybe 2.5 m away from the Sonoff mounted in a waterproof box on the wall. There is a direct line of sigh between the router, through the window to Sonoff so it feels crazy that the signal would be still too weak but I get the signal strength I get.

I tried to implement your solution with homeassistant event: start but I still get a notification that the gate was opened and then closed every time the Sonoff becomes unavailable and then available again. Any suggestions on what I could do? It happens every 5-10 minutes. Long term I need to solve the wifi signal I guess.

I use the stock Shelly firmware and the native HA integration for Shelly. Every Shelly device I have works very well with the integration, including very fast responses and never go offline. I only don’t suggest their smart outlets - all 4 of mine died in less than a year.

I can’t help with Tasmota firmware as I don’t use that on anything. ESPHome I use on my mini-split air conditioner and it works well. I have some Sonoff switch modules, howver none are WiFi as I prefer to stick to Zigbee for those. The only WiFi stuff I use is from Shelly, apart from my Homepods, phones and computers and such.

1 Like