Google Assistant and Alexa not taking door state into account

I have a gate and garage door set up through ESP home with magnetic sensors to detect the door state. With the cover template in ESPHome, the doors show up correctly in Home Assistant and the correct buttons are disabled depending on whether the door is open or closed.

However, when I use Google Assistant or Alexa, they ignore whether the door is open or closed. So when I say “close the garage”, it will open it if the garage is already closed. Is this a known issue or am I doing something wrong?

The following is my ESPHome configuration for my gate. The garage is pretty much the same.

switch:
  - platform: gpio
    pin: 16
    id: gate_relay
    name: "Gate Remote Relay"
    inverted: false
    internal: true

binary_sensor:
  - platform: gpio
    pin:
      number: 12
      inverted: false
      mode:
        input: true
        pullup: true
    name: "Gate door sensor"
    id: gate_sensor
    internal: true
    
cover:
  - platform: template
    device_class: gate
    name: "Gate"
    id: gate
    lambda: |-
      if (id(gate_sensor).state) {
        return COVER_OPEN;
      } else {
        return COVER_CLOSED;
      }
    open_action:
      - switch.turn_on: gate_relay
      - delay: 1s
      - switch.turn_off: gate_relay
    close_action:
      - switch.turn_on: gate_relay
      - delay: 1s
      - switch.turn_off: gate_relay

I solved my own problems. Posting my solution for anyone interested. I modified the open_action and close_action to use lambda to check for the sensor state before executing.

binary_sensor:
  - platform: gpio
    pin:
      number: 12
      inverted: false
      mode:
        input: true
        pullup: true
    name: "Gate door sensor"
    id: gate_sensor
    internal: true
    
cover:
  - platform: template
    device_class: gate
    name: "Gate"
    id: gate
    optimistic: false
    lambda: |-
      if (id(gate_sensor).state) {
        return COVER_OPEN;
      } else {
        return COVER_CLOSED;
      }
    open_action:
      then:
        - lambda: |-
            if (!id(gate_sensor).state) {
              id(gate_relay).turn_on();
            } else {
              ESP_LOGD("main", "Gate already open");
            }
    close_action:
      then:
        - lambda: |-
            if (id(gate_sensor).state) {
              id(gate_relay).turn_on();
            } else {
              ESP_LOGD("main", "Gate already closed");
            }
1 Like