ESPHome switch configuration - HomeBridge

I added a relay to my ESP8266 with following ESPHome yaml:

switch:
- platform: gpio
  id: relay
  pin:
    number: D6
    inverted: False
  restore_mode: ALWAYS_OFF
- platform: template
  name: "Garage Door Switch"
  icon: "mdi:garage"
  turn_on_action:
  - switch.turn_on: relay
  - delay: 1s
  - switch.turn_off: relay

And this is actually working perfectly in Home Assistant! So I have a switch in Home Assistant and if I click it, it will turn on and after a second turn off again. Also the relay will toggle.
But the HomeBridge component shows the switch in HomeKit as a normal switch. Meaning, if I press the switch in HomeKit, the status will go from Off to On and stay on. Even though In Home Assistant it will go off again after 1 sec.

So now I need to manually tap twice on the switch in HomeKit before I can again use it.

How are other people using toggle relays with the HomeBridge link?
Do I need to put the actual toggle in a Home Assistant automation instead of inside the ESPHome config yaml? Any other tips?

I see its late, but why do you use homebridge? Are there advantages or something? I use the HomeKit Component, wich transfers all my entities to HomeKit. Why should I use homebridge, when there is a native support for this in HA?

Totally forgot I put up this question… have it all sorted through HomeKit component also now. :slight_smile:

I have the same problem and haven’t figured that out yet. Could you share your configuration?

hey @VStonkus so I switched to an ESP01S and this is it’s ESPHome config

switch:
- platform: gpio
  name: "Garage Door Switch"
  pin: GPIO0
  inverted: True
  id: relay
  restore_mode: ALWAYS_OFF

After that I created a script in Home Assistant as follows

garage_trigger:
  sequence:
    - service: switch.turn_on
      data:
        entity_id: switch.garage_door_switch
    - delay: '00:00:01'
    - service: switch.turn_off
      data:
        entity_id: switch.garage_door_switch

Thank you. I see you have implemented delay via home assistant automation.
I thought you solved homekit toggle problem, with the automation on esp side.
I’d rather want to have automatic turning off on esp side (as in your previous configuration)
The reasons for that are if network issues occur or HASS end down in the middle of automation. I wouldn’t like to destroy my doorbell leaving in turned on for long time or leave garage opener turned on. Also there might be reduced accuracy for time delay.

haaa I see, hmm no the ‘fix’ for me to get this working in HomeKit, was to bring over the delay to Home Assistant instead of leaving it in the ESPHome config

Hmm @VStonkus maybe moving the delay to Home Assistant was not necessary in my case.
My actual config to bring over the garage switch to HomeKit also consists of a cover.

cover:
  - platform: template
    covers:
      garage_door:
        friendly_name: "Garage door"
        value_template: "{{is_state('binary_sensor.garage', 'on')}}"
        open_cover:
          service: script.garage_trigger
        close_cover:
          service: script.garage_trigger
        icon_template: >-
          {% if is_state('binary_sensor.garage', 'on') %}
            mdi:garage-open
          {% else %}
            mdi:garage
          {% endif %}

So HomeKit will now notice if the garage is open or closed, by not looking at the ESP switch ( relay ) itself but instead using the state of a magnetic door sensor for the validation.
Triggering the open or close of the garage is done by calling the script. So in essence I could move the delay and toggle on to off back to ESP.
But not sure how you could implement said functionality with for instance your door bell. Because in my experience just watching the state of a relay through HomeKit from an ESP will only be correct if no auto toggle is being done with a delay.

Weird fact is still that home assistant reports it correctly. So wondering if maybe the delay is to short/fast for status values being passed through to HomeKit?
Could just be we found a limit in the HomeKit component… maybe worth pinging somebody with knowledge of that component to get some answers?