Esphome relay action for specific time

Hello,

I have some shutters which I do control by double relays.

At the moment I do use “delay” to automatically switch the relay off, after 30sec, otherwise it would be allways on until change of action.

switch:
  - platform: gpio
    id: open_relay
    name: open ${device_name}
    pin: 12
#    interlock: [close_relay]
  - platform: gpio
    id: close_relay
    name: close ${device_name}
    pin: 14
#    interlock: [open_relay]


  # external switches
binary_sensor: 
  - platform: gpio
    id: open_button
    pin:
      number: 4
      inverted: true
    on_click:
    - min_length: 50ms        # Short press
      max_length: 500ms
      then:
        - switch.turn_off: close_relay      
        - switch.turn_on: open_relay
        - delay: 30s                  # maximum time for open action plus some reserve
        - switch.turn_off: open_relay
    - min_length: 1000ms      # Medium press / long
      max_length: 5000ms
      then:
        - switch.turn_off: open_relay

  - platform: gpio
    id: close_button
    pin:
      number: 5
      inverted: true     
    on_click:
    - min_length: 50ms        # Short press
      max_length: 500ms
      then:
        - switch.turn_off: open_relay     
        - switch.turn_on: close_relay
        - delay: 30s                  # maximum time for close action plus some reserve
        - switch.turn_off: close_relay        
    - min_length: 1000ms      # Medium press / long
      max_length: 5000ms
      then:
        - switch.turn_off: close_relay
      
  - platform: gpio
    id: stop_button
    pin:
      number: 13
      inverted: true
    on_click: 
      then:
        - switch.turn_off: close_relay
        - switch.turn_off: open_relay

Probably you see, that I am a bloody beginner and that this ends up in a problem. Then after a few up/stop/down actions in a short timeframe it will send as well many stop actions, as well when they are not wanted.

now my question is:
Could someone please tell me a other possibility for a time limited action? (Maybe something with lambda?)
Or a possibility to reset the delay-timer, when the same action or a other action is triggered?

thanks in advance

(To control the shutter with “cover component” is no option for me. I did allready try this here, but the timing is absolutely useless if you do not open/close completely all the time.You end up with shutters stopping anywhere, but not where you want them)

The delay is fine, I’m not sure any other way is particularly better. The way I do it is to expose a number slider to let me dictate the delay and use a lambda to use that value in my delays, as such:

  - platform: template
    name: "Pump Duration"
    optimistic: true
    min_value: 0
    max_value: 99
    step: 1
    id: "pump_duration"
    restore_value: true
    initial_value: 40   
    icon: "mdi:timeline-clock"    

Then I utilize that in the operation:

  - platform: gpio
    icon: mdi:water-pump
    id: relay
    name: Water Pump
    pin: 
      number: D1 
      mode: OUTPUT    
    on_turn_on:
    - delay: !lambda 'return (int(id(pump_duration).state) * 1000);'
    - switch.turn_off: relay
1 Like

HI @CO_4X4 ,

thanks a lot for your help.
I will try it with the “lambda code” and check, if there is any difference in the behavior of keeping the delayed stop-action after selecting “open/close” a few times in short time. But I´m afraid it stays the same.
How would this look as simple as possible only with a value directly in the lambda itself?

Another question:
Is there a command to interrupt “delay”?

Somebody on discourd gaved me the hint to put the actions to a script. And with the script parameters and actions ( mode: restart and stop.action) it is possible to reset and interrupt the delays.

finally it looks like this:

Summary

` # relays
switch:

  • platform: gpio
    id: open_relay
    name: open ${device_name}
    pin: 12
    interlock: [close_relay]
  • platform: gpio
    id: close_relay
    name: close ${device_name}
    pin: 14
    interlock: [open_relay]

script:

  • id: open_script
    mode: restart
    then:
    - switch.turn_off: close_relay
    - switch.turn_on: open_relay
    - delay: 10s
    - switch.turn_off: open_relay

  • id: open_halt_script
    then:
    - switch.turn_off: open_relay

  • id: close_script
    mode: restart
    then:
    - switch.turn_off: open_relay
    - switch.turn_on: close_relay
    - delay: 10s
    - switch.turn_off: close_relay

  • id: close_halt_script
    then:
    - switch.turn_off: close_relay

  • id: stop_script
    then:
    - switch.turn_off: close_relay
    - switch.turn_off: open_relay

external switches

binary_sensor:

  • platform: gpio
    id: open_button
    pin:
    number: 4
    inverted: true
    on_click:

    • min_length: 50ms # Short press
      max_length: 500ms
      then:
      - script.execute: open_script
    • min_length: 1000ms # Medium press / long
      max_length: 5000ms
      then:
      - script.execute: open_halt_script
      - script.stop: open_script
      - script.stop: close_script
  • platform: gpio
    id: close_button
    pin:
    number: 5
    inverted: true
    on_click:

    • min_length: 50ms # Short press
      max_length: 500ms
      then:
      - script.execute: close_script
    • min_length: 1000ms # Medium press / long
      max_length: 20000ms
      then:
      - script.execute: close_halt_script
      - script.stop: open_script
      - script.stop: close_script
  • platform: gpio
    id: stop_button
    pin:
    number: 13
    inverted: true
    on_click:
    then:
    - script.execute: open_halt_script
    - script.execute: close_halt_script
    - script.stop: open_script
    - script.stop: close_script`