Garage door opener with relay and contact sensor

I have a relay that needs to close and open again to move the garage door. When the door is closed it starts moving open, when the relay is activated while moving it stops. When it then activates again it starts moving in the closed direction etc. In short all movement commands are just shortly closing and opening the relay. To make matters worse it does not always open when I give it a command: starts to move, decides that the power is to high and gives up. It might take a couple of retries to actually get it open. So I want to make a script that closes and opens the relay and then monitors a magnetic contact sensor to see if the door is open. If not, retry until it actually opens. On top of that I would like to use the same magnetic contact to report open or close for the door. Here is what I have so far (in scripts.yaml):

garage_deur_open_dicht:
  sequence:
    - alias: Garage deur pulse aan
      service: light.turn_on
      data:
        entity_id: light.garage_deur_opener
    - alias: Garage deur pulse uit
      service: light.turn_off
      data:
        entity_id: light.garage_deur_opener

My relay gets reported by deconz as a light but it is actual just a relay.
The contact sensor is: binary_sensor.openclose_21 which reports ‘on’ when the door is not closed and ‘off’ when it is closed.
I currently call the script to pulse the relay to move the garage door but will not know if it worked until I get eyes on the door or look at the binary_sensor.

This is a significant problem to overcome with just a script. Honestly, you’ll make your life easier by either fixing or replacing this defective device.

If you can’t (or won’t) fix it, then here’s one possible approach:

  • When you command the door to close, start a timer. The timer’s duration is the time needed to close the door (when it works properly).
  • If the timer finishes before the contact sensor indicates the door is closed then we can assume the door failed to close and we have to send another command to close it. The cycle repeats.
  • When the door closes, it stops the timer.

This problem only occurs on opening the door, so it is not so bad. I’ll fix it one day :slight_smile:
In the meantime I would like to learn how to implement this in the script, what you suggests with a timer is fine with me, retry every 5 sec until the door is open works for me.

What you have in mind for this timer is not what I suggested. My suggestion is a timer whose duration is the time it takes the door to open without any failures. The ‘retry’ occurs only if the door is still not open when the timer expires. I believe you are suggesting to simply send ‘open’ commands every 5 seconds.

Whether it’s your approach or mine, this solution cannot be implemented with a single script; it requires a timer and a few automations.

So I guess what I need is a script that is just like I have it (send a close open pulse to the garage door opener).

Then I define a switch in configuration.yaml that looks at the current status of the sensor and if it is still closed 5 sec after the first open pulse, it retries. The number of retries can be tweaked by copying the code block. Sounds like a great use for a while loop (with some time out counters and timer in there) but I gather from other discussions that those are not considered good coding practice.

Can somebody help me get this guy working?

switch:
  - platform: template
    switches:
      garage:
        value_template: "{{ is_state('binary_sensor.openclose_21', 'on') }}"
        turn_on:
          service: script.garage_deur_open_dicht
          delay "00:00:05"
          {% if is_state('binary_sensor.openclose_21', 'off') %}
            service: script.garage_deur_open_dicht
          {%endif%}
        turn_off:
          service: script.toggle
          entitiy_id: script.garage_deur_open_dicht
        icon_template: >-
          {% if is_state('binary_sensor.openclose_21', 'off') %}
            mdi:garage
          {% else %}
            mdi:garage-open
          {% endif %}

Did you run config check? What you posted contains many syntax errors.

Yes I cannot figure out how to fix: can not read a block mapping entry; a multiline key may not be an implicit key at line 55, column 20:

Would you see the idea could work once Syntax is fixed?

How long does it normally take for the door to open when it is working properly? 20 seconds?

The sensor is placed near the closed position of the door so the only thing I can detect is if the door is closed or not. If opening fails and it goes back to its closed position it is within a second or so, the 5 second timeout is plenty (and I can always adjust as needed later on :slight_smile: ) . As soon as it gets over the first hurdle during opening it will always open fully. Closing is never a problem.

And thanks for helping me!

Thanks for the explanation but you overlooked to answer my question: How long does it take to open the door when it is working properly?

I had to go out and time it. slightly under 22 sec. What do you need that number for?

?

For the reason I explained in my very first post …

We don’t appear to agree on the approach to solve this issue so I’ll step aside and allow someone else to assist you. My final contribution is that you should consider using a Template Cover. Good luck!

So here is my working solution:
in configuration.yaml:

  - platform: template
    switches:
      garage_deur:
        value_template: "{{ is_state('binary_sensor.openclose_21', 'on') }}"
        turn_on:
          service: script.garage_deur_open_dicht
        turn_off:
          service: script.1574347312482
        icon_template: >-
          {% if is_state('binary_sensor.openclose_21', 'off') %}
            mdi:garage
          {% else %}
            mdi:garage-open
          {% endif %}

in scripts.yaml:

garage_deur_open_dicht:
  sequence:
  - alias: Garage deur pulse aan
    service: light.turn_on
    data:
      entity_id: light.garage_deur_opener
  - delay: 00:00:01
  - alias: Garage deur pulse uit
    service: light.turn_off
    data:
      entity_id: light.garage_deur_opener
'1574347312482':
  alias: Garage openen
  sequence:
  - data: {}
    service: script.garage_deur_open_dicht
  - delay: 00:00:05
  - condition: state
    entity_id: binary_sensor.openclose_21
    state: 'off'
  - service: script.garage_deur_open_dicht
  - delay: 00:00:05
  - condition: state
    entity_id: binary_sensor.openclose_21
    state: 'off'
  - service: script.garage_deur_open_dicht

The switch calls the second script at open and the first script at close. The first script is just the pulse to toggle the relay and move the door. The second script for opening only just calls the toggle script and then checks the condition to see that the door is still closed. If that is the case it repeats max 3 times.