How to perform a "long" running action on a button press?

Looks like ESPHome isn’t happy when on_press handler takes 200 ms or more, but I need to generate a short 200ms pulse on GPIO pin

button:
  - platform: template
    id: remote
    name: "Remote"
    icon: "mdi:garage-variant"
    on_press:
      then:
        - lambda: |-
            id(relay_pin_1).turn_on();
            delay(200);
            id(relay_pin_1).turn_off();

This results in warning in the log:

[16:14:52][W][component:214]: Component api took a long time for an operation (0.20 s).
[16:14:52][W][component:215]: Components should block for at most 20-30ms.

Is there a way to implement such a action without upsetting ESPHome?

It’s just a warning, which you can suppress if you want. Your delay will still work.

You can reimplement it as a set of actions instead of a lambda.

    on_press:
      then:
        - switch.turn_on: relay_pin_1
        - delay(200)
        - switch.turn_off: relay_pin_1

From the documentation:

This is a “smart” asynchronous delay - other code will still run in the background while the delay is happening. When using a lambda call, you should return the delay value in milliseconds.

1 Like