Aqara door sensor state changing erroneously in HA but not in Aqara app

I have been fighting with this for a few days. I have my garage door set up with a Shelly 1 controlling it and two Aqara sensors to determine open/closed/opening/closing state. The Aqara sensors are connected to an E1 Hub. I have one door sensor that is closed when the door is down and one that is closed when the door is up. (I realize the redundancy, but I want to be able to tell if the door is not fully open or closed. This also lets me determine opening/closing.)

The actual garage door state is being determined by this entry in template.yaml:

# Garage Door sensor
  - trigger:
    - platform: state
      entity_id:
        - binary_sensor.garage_door_down
        - binary_sensor.garage_door_up
    sensor:
      - name: 'garage_door'
        unique_id: 'garage_door'
        state: > 
          {% if (trigger.to_state.state == 'off' and trigger.entity_id == 'binary_sensor.garage_door_up') %}
            open
          {% elif (trigger.to_state.state == 'off' and trigger.entity_id == 'binary_sensor.garage_door_down') %}
            closed
          {% elif (trigger.to_state.state == 'on' and trigger.entity_id == 'binary_sensor.garage_door_up') %}
            closing
          {% elif (trigger.to_state.state == 'on' and trigger.entity_id == 'binary_sensor.garage_door_down') %}
            opening
          {% endif %}

The above code works well to determine the door state from the sensors, but the problem I’m having is that when I operate the door, the Aqara sensors (in HA) will have multiple erroneous state changes. When I watch in the Aqara app, I can see some very quick bounces on the sensors, but once the door gets moving or is fully stopped, the states are stable. In HA, the states will bounce during initial movement for about 0.5-2 seconds, then they may randomly change seconds after the door stops moving, only to change back to the correct state 30 or more seconds later.
So what I see in my dashboard is the garage door state doing the following when opening (underlined states are erroneous):
closed->opening->closed->opening->open->closing->…~30 seconds later…->open. Similar behavior when closing.

I also tried “debouncing” the sensors by using a for: section in my trigger. That seemed to cut out some of the quick bouncing during initial movement, but made no difference with the long duration erroneous states.

So, at this point I think I can tell if my garage door is open or closed (assuming it has been a few minutes since it was operated), but I can’t trust its state during or shortly after operating it.

This is my first use of Zigbee sensors, and I wasn’t totally confident in the way I integrated them. I used the Shelly integration for the Shelly 1 and the Aqara hub is integrated as a Homekit device. Maybe I didn’t do the sensor integration correctly? I think the problem lies there, since the sensors seem to behave properly in the Aqara app.

Any suggestions are appreciated.

UPDATE:
After more testing, it is even stranger than I thought. I made a mod to de-bounce the actual sensor inputs, so that cleaned up the garage door state bouncing on initial movement, but there was still strange state changes well after the door had stopped. Upon looking at the data closer, it appears that after the door stops moving, at the next 47 second mark after the minute, one or both of the Aqara sensors will toggle their state for exactly one minute. This only happens to the sensor in Home Assistant, the sensors in the Aqara app do not show this behavior.

To be clear, close the door at 17:02:12, I will see the state change from open->closing->closed, then at 17:02:47 one or both of the door sensors will toggle so the door state will change to open, opening, or closing. Then at 17:03:47, the sensors will toggle back and the door state will return to closed.

I am totally confused. Does anyone have any idea what is going on?