"Faking" garage door states like closing, opening

Hello,

I have a garage door that I’m controlling with a Shelly 1. Meaning I have a switch I toggle for half a second to open or close the garage and a binary_sensor for a reed switch that tells me if the garage is closed or not.

What I’m trying to do is “fake” the opening and closing states of my cover based on the time elapsed since the binary_sensor or switch state changed. My garage opens and closes in about 21 seconds.

This is my cover config:

cover:
  - platform: template
    covers:
      garage_door:
        friendly_name: "Garage Door"
        value_template: >
          {% set b_ts = now().timestamp() - as_timestamp(states.switch.mqtt_garage_door_button.last_changed) %}
          {% set s_ts = now().timestamp() - as_timestamp(states.binary_sensor.garage_door_sensor.last_changed) %}
          {%- if is_state('binary_sensor.garage_door_sensor', 'on') -%}
            {%- if s_ts < 22 -%}
               opening
            {%- elif b_ts < 22 -%}
               closing
            {%- else -%}
               open
            {%- endif -%}
          {%- else -%}
            closed
          {%- endif -%}
        open_cover:
          service: script.toggle_garage_door
        close_cover:
          service: script.toggle_garage_door
        device_class: "garage"
        unique_id: "garage_door"

I have confirmed the value_template is working fine using the template page in the Home Assistant UI (including when opening and closing). The b_ts is the time elapsed since the switch was triggered and the s_ts the time elapsed since the sensor changed state.

However I have two issues:

  1. When closing the garage the state immediately goes to closed. I have no idea why this happens. While closing, if I evaluate the value_template using the Home Assistant UI it correctly reports closing.
    The reed switch is definitely not closed when this happens.
  2. When opening it takes a full minute change from opening to open. This is expected because the value_template depends on now() which makes it update every minute. I attempted to force a re-evaluation using the toggle_garage_door script by adding a step that updates the cover after 23 seconds:
toggle_garage_door:
  alias: "Open/Close Garage Door"
  sequence:
    - service: switch.toggle
      data:
        entity_id: switch.mqtt_garage_door_button
    - delay: 23
    - service: homeassistant.update_entity
      data:
        entity_id: cover.garage_door

This also didn’t work.

Does anyone have an idea why or how to overcome any of these two issues?

There is an excellent “cover time based” custom component that you could use instead. It should solve your issues. Have a look at: https://github.com/nagyrobi/home-assistant-custom-components-cover-rf-time-based

Thanks for that. It’s not exactly what I was looking for but it lead me to go look at the code for the cover template in GitHub and apparently it doesn’t actually support closing or opening states. Those are valid states, but they are ignored!

It doesn’t even implement the is_opening and is_closing properties described in the cover entity.

I believe I could create my own version of the cover that does that. This would explain issue number 1.

Would love to know how to get around issue number 2. Perhaps I can use one of those time-based cover custom components as inspiration to update the state after a set of time.

One additional consideration: Some garage door automation equipment includes either a magnetic switch or a tilt sensor that indicates whether the door is open or closed. This provides an addition check in case the door doesn’t complete the action. (For example, if something triggers the sensors at the bottom of the door that indicate an obstruction and the door stops closing and re-opens.)
If you have something like that, you can use those to set the open and closed states and set the opening and closing states initially using the garage door sensor only. You can remove the timer completely.

You could create automations to set the value of a input_text.
Not very fancy but will work just fine I believe.

@qoheleth my garage motor does stop if it senses any blockage while running, but I don’t think there’s any way for me to get at that data. At least based on the pins I can connect to.

@Hellis81 that could work yes, it has crossed my mind. I want to try a cleaner approach first.

So far I have created my own custom component based on the Cover Template and augmented that to support closing and opening states. That seems to be working.

Only thing left is have it refresh automatically after a configurable set of time. So when state changes to opening or closing I’ll set a timer to update the entity state.

I’ve only recent started using Home Assistant so I’m not entirely sure how these custom components work (nor am I that experienced in Python…). I’ll try to find another component that does something similar (with a timer) and base it off that.

I’ve uploaded my custom cover here

Seems to be working like I wanted.

I run into a similar issue a year ago. I also wanted to use the cover in HomeKit, but turns out the HA cover component, as you said, did not support opening and closing states, only open and closed. I wonder if your implementation, when used with HA HomeKit controller, gives HomeKit opening and closing values. My goal is to implement a fully working HomeKit garage cover integration with a Shelly 2.5 and 2 reeds (or 2 Shelly 1 and 2 reeds) like it’s done with this firmware or via MQTT. This is the one I’m using now and it works great. Two reeds give you real open and closed status and can also give HomeKit a jammed alert if the door does not close in more than X seconds. Let me know what you think.

2 Likes

I think you’d have to make a few changes. IIRC my code assumes 1 reed that’s active for the closed position, and “opening” state is “faked” with a timer (configurable).

So to take advantage of the two reeds there would have to be some changes. You would not need a timer in this case.

Would you be able to share your config utilising this custom cover?

I see one of your issues was solved in Core recently enough (template covers can now support opening/closing states):

It’s cool that was merged. Still not enough to replace my custom cover though.

Here’s my config:

cover:
  - platform: pcc
    covers:
      pcc_garage_door:
        friendly_name: "Garage Door"
        value_template: "{{ is_state('binary_sensor.garage_door_sensor', 'on') }}"
        open_cover:
          service: switch.toggle
          data:
            entity_id: switch.mqtt_garage_door_button
        close_cover:
          service: switch.toggle
          data:
            entity_id: switch.mqtt_garage_door_button
        travelling_time_down: 22
        travelling_time_up: 22
        device_class: "garage"
        unique_id: "pcc_garage_door"
1 Like

I tried to use your repo but HA complained that the manifest had no version key.
More info here:

When you get a chance, it would good if you could add this to your repo.

Thanks

Malcolm

Thanks for mentioning that. I wasn’t aware. I don’t have time to look at this right now, but a Pull Request is welcome.

Edit: Never mind, I just realized it was really just a “version” field. I just added it.

1 Like

Hey man, thanks for the script!! I’d like to use the same setup as yours, I uploaded your custom cover to homeassistant, and used your same config. Still it closes after n° seconds, but when opening it remains “in opening state”.

I tried to create a binary sensor with default close value but still, no luck:

binary_sensor:
  - platform: template
    sensors:
      garage_door_sensor:
        value_template: 'off'

Could you try to take a look at it, please? I opened an issue in your github

Thanks!!

Has anyone using this component upgraded to core-2022.2.x yet?
I’ve hit a problem - logs show the following:

Logger: homeassistant.components.cover
Source: custom_components/pcc/cover.py:152 
Integration: Cover (documentation, issues) 
First occurred: 10:22:50 (1 occurrences) 
Last logged: 10:22:50

Error while setting up pcc platform for cover
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 249, in _async_setup_platform
    await asyncio.shield(task)
  File "/config/custom_components/pcc/cover.py", line 128, in async_setup_platform
    async_add_entities(await _async_create_entities(hass, config))
  File "/config/custom_components/pcc/cover.py", line 103, in _async_create_entities
    PCCCover(
  File "/config/custom_components/pcc/cover.py", line 152, in __init__
    super().__init__(
TypeError: __init__() missing 1 required positional argument: 'hass'

It’s not obvious how to fix this, because it was working well for me.

It looks like CoverTemplate has changed from the version the code was based on
(https://github.com/home-assistant/core/blob/c41ec6f941252300cb4d0e50cbb2896ff28b95da/homeassistant/components/template/cover.py).
This version has a hass 1st parameter in the super.init. When I modify the pcc cover.py to match , the error goes away and my garage door is back working.

I’ll create an issue on the pcc GitHub for this.

1 Like

I came up with my own solution using 2 magnetic reed switches on a D1 mini and HA’s native cover template.

image

1 Like

Thanks to @pfandrade for the piece of code, it’s working for my Shelly 1 with one reed switch to get away from MyQ.
For the case of opening to open not occurring immediately, I added this automation:

- id: change_garage_door_state

  alias: Change Garage Door State

  trigger:

    - platform: state

      entity_id: cover.garage_door

      to: 'opening'

  action:

    - delay: '00:00:13'

    - service: homeassistant.update_entity

      entity_id:

        - cover.garage_door

Would you mind sharing where you found the garage icons?

See KNX User Forum Icon Set

You can never have too many icons!

I just wish KNX had vacant garage icons to match!

1 Like