Relay no longer be activated for X time

Hi guys!
I would like to program a D1 mini + relay like this (if it’s possible):
when the relay is activated (momentary 1 s), it must no longer be activated for 5 minutes.
After 5 minutes, the entity can be clickable and activated again.

Possibly I would like to do everything within ESP Home, and only display the button on the Home Assistant (and possibly the on/of status)

I need this to automate a coffee machine, which has only one button:

  • if pressed when it is off > it turns on the machine.
  • if pressed a second time when on > it dispenses the coffee.

Thanks to anyone who helps me with this project

It is possible to program a D1 mini and relay to behave in the way you described using the ESPHome firmware. ESPHome allows you to define custom automations and conditions within the firmware, so you could create an automation that turns on the relay for 1 second when it is activated, and then prevents the relay from being activated again for 5 minutes.

As an example of how this could be implemented using ESPHome:

esphome:
  name: my_coffee_machine
  platform: ESP8266
  board: d1_mini
  on_boot:
    - wifi.connect:
        ssid: !secret my_wifi_ssid
        password: !secret my_wifi_password
  relay:
    - pin: D1
      id: coffee_machine_relay
  binary_sensor:
    - platform: gpio
      pin: D2
      id: coffee_machine_button
      on_press:
        - relay.turn_on: coffee_machine_relay
        - delay: 1s
        - relay.turn_off: coffee_machine_relay
        - delay: 5m
        - id: coffee_machine_button_lock
          then:
            - if:
                condition: not
                - binary_sensor.is_off: coffee_machine_button
              then:
                - relay.turn_on: coffee_machine_relay
                - delay: 1s
                - relay.turn_off: coffee_machine_relay

In this example, the coffee_machine_button binary sensor is used to detect when the coffee machine’s button is pressed. When the button is pressed, the coffee_machine_relay is turned on for 1 second, then turned off again. This simulates pressing the coffee machine’s button.

After the relay is turned off, a 5 minute delay is added using the delay action. During this time, the coffee_machine_button_lock ID is used to prevent the relay from being activated again, even if the button is pressed. After the 5 minutes have elapsed, the coffee_machine_button_lock ID is removed, and the coffee machine’s button can be pressed again to activate the relay.

You can then use Home Assistant to display the state of the coffee_machine_button binary sensor, and control the coffee_machine_relay from the Home Assistant UI.

1 Like

Thank you! I’ll try it as soon as I can. :wink:

1 Like

Hi, unfortunately this configuration is completely wrong, it can’t even be loaded on D1 mini because it generates errors

The error is in the “if” condition, but I can’t fix it

switch:
  - platform: gpio
    pin: 1
    id: coffee_machine_switch
binary_sensor:
  - platform: gpio
    pin: 2
    id: coffee_machine_button
    on_press:
      - switch.turn_on: coffee_machine_switch
      - delay: 1s
      - switch.turn_off: coffee_machine_switch
      - delay: 300s
      - id: coffee_machine_button_lock
        then:
          - if:
              condition: not
              - binary_sensor.is_off: coffee_machine_button
          - then:
              - switch.turn_on: coffee_machine_switch
              - delay: 1s
              - switch.turn_off: coffee_machine_switch

Problem is in this part:

      - id: coffee_machine_button_lock
        then:
          - if:
              condition: not
              - binary_sensor.is_off: coffee_machine_button
          - then:
              - switch.turn_on: coffee_machine_switch
              - delay: 1s
              - switch.turn_off: coffee_machine_switch