Help with esphome lambda

I am trying to write a controller for my garage door and have run into a problem. To close the door I need to keep the relay switched on until the reed switch closes. To do this I have set up a lambda under the ‘close_action’ case in the esphome garage cover.

The lambda attempts to say:

  1. If the door is not closed, then set the door_state to ‘closing’ and flip the relay on
  2. While the door is closing, keep checking the closed_sensor state. If it flips to true, the door is closed. Set the door_state to closed and flip the relay back off

The first clause seems to work correctly, but when we get into the while loop it either freezes after around 10 loops, or freezes when I close the reed switch. I have to unplug the Sonoff to reset everything and try again.

yaml for esphome:

https://pastebin.com/ckAHyC1q

log output from esphome:

https://pastebin.com/SK6Re0wH]

I have absolutely no idea why this is happening and would really appreciate some help! I am also wide open to other solutions if there is a better way.

Can you post you yaml here? The links aren’t working for me and it makes it easier to help with everything in one place.

You need to have something like this on your binary sensor code:

binary_sensor:
  - platform: gpio
    # ...
    on_state:
      then:
        - if:
          condition:
            binary_sensor.is_on: some_binary_sensor
        - switch.turn_off: door_switch      

Thank you - I ended up doing something similar, inside of the cover. The while loop in my original code was hogging all the resources in esphome, which is why that approach wont work (for any future individual who ends up in the same situation).

close_action:
  # Cancel any previous action
  - switch.turn_off: door_switch
  - delay: 500 ms[wrap="justify"]
  - switch.turn_on: door_switch
  - wait_until:
        binary_sensor.is_on: closed_sensor
       - switch.turn_off: door_switch
1 Like