Button that can be pressed only after 5 seconds again

Hey, i want a Button that can be pressed only after 5 seconds again. But i don’t how to modify my code.

Here is my code:

button:
  - platform: template
    name: "Phase switching"
    id: switchphase_switch
    icon: "mdi:sync"
    on_press:
        then:
          if:
            condition:
                lambda: |-
                  if ((id(outlet_state).state == 0xA1) || (id(outlet_state).state == 0xE0) && (id(outlet_state).state != 0xC2)) {
                    return true;
                  } else {
                    return false;
                  }
            then:
              - output.turn_on: switchphase
              - delay: 200ms
              - output.turn_off: switchphase
# Example configuration entry
binary_sensor:
  - platform: gpio
    pin: D2
    name: ...
    filters:
      - delayed_off: 5s

try debounce. Turn it on and can’t be turned off for 5 seconds

Probably no way to prevent button toi be pressed.
Can prevent actions to be executed on press.

Start the script with just delay inside.
And while it is_running - do not execute actions.

Smoething ike this:

button:
  - platform: template
    name: "Phase switching"
    id: switchphase_switch
    icon: "mdi:sync"
    on_press:
        then:
          if:
            condition:
              - lambda: |-
                  return (
                  ((id(outlet_state).state == 0xA1) || (id(outlet_state).state == 0xE0) && (id(outlet_state).state != 0xC2))
                  && !id(switchphase_delay).is_running()
                  );
            then:
              - script.execute: switchphase_delay
              - output.turn_on: switchphase
              - delay: 200ms
              - output.turn_off: switchphase

script:
  - id: switchphase_delay
    mode: restart
    then:
      - delay: 5s
1 Like

@Masterzz
i tried your solution but can’t compile :frowning:

i need only a impulse on the output

on_multi_click:
- timing:
    - ON for at most 1s
    - OFF for at most 1s
    - ON for at most 1s
    - OFF for at least 0.2s
  then:
    - logger.log: "Double Clicked"
- timing:
    - ON for 1s to 2s
    - OFF for at least 0.5s
  then:
    - logger.log: "Single Long Clicked"
- timing:
    - ON for at most 1s
    - OFF for at least 0.5s
  then:
    - logger.log: "Single Short Clicked"

If you used multi click then you could set it to be off for at least 5 seconds. Could use 2nd click to do nothing or just delay few seconds.

Just forgot () when using is_running. Try now.

Not sure on_multi_click can be used with button.

I was assuming there was a physical momentary button attached to the ESP device. You would set up a binary sensor in ESPhome

binary_sensor:
  - platform: gpio
    name: "momentary_button"
    pin:
      number: 16
      mode: INPUT_PULLUP
      inverted: True
    on_multi_click:
    - timing:
        - ON for at most 1s
        - OFF for at most 1s
        - ON for 0.5s to 1s
        - OFF for at least 0.2s
      then:
        - whatever

Something like that. But the OP didn’t give any more details. So you could be right that this wouldn’t help if it’s all being done in HA. The more details the OP gives the more people are likely to give an opinion. Could the answer just be as simple as…

willpower

thx
must i start the script at any point?
Because it don’t work.

I am using only the button in HA.

I want to control a output pin. But the output should only go to high, if my conditions are met. My conditions “outlet_state” is read every 5s over RS485.

If you’re only using the button in HA, then there’s a very simple solution.

Set up an automation which does whatever you want it to do, then add a delay of 5s at the very end of the actions. Make sure your automation is set to mode: single and max_exceeded: silent.

What this should do is mark the automation as still running for 5s after the action is performed. The single mode will not allow the automation to be retriggered until those 5s are over & the automation is marked as finished. The silent option is there so you don’t get spammed with errors in the log.

1 Like

Yes, add this before of after output toggling.

Thanks :slight_smile: now it is working