Tinkering with sit/stand desk conversion

I have a sit/stand conversion that sits on top of my desk. Controls are very simple: press and hold button - desk goes up. Press and hold other button - desk goes down.

I have duplicated same functionality with ESP32 and relay module. I can toggle relays on or off from HA.

I have added 2 physical buttons that duplicate same functionality as stock. Press button, LED lights up and relay is triggered on.

I want to add another two buttons that will function as follows: momentary press - LED is on, relay is on. This condition stays for 25 seconds and then LED goes off and relay is turned off.

Here is my code so far:

esphome:
  name: "esp32-desk"

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
 
ota:
 

web_server:
  port: 80

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esphome-Web-0046Cc"
    password: "YvUhUSzJdfL7"

captive_portal:
    
switch:
  - platform: gpio
    pin: GPIO22
    name: "Desk Up relay"
    id: relay1
    inverted: True
    
  - platform: gpio
    pin: GPIO23
    name: "Desk Down relay"
    id: relay2
    inverted: True
    
  - platform: gpio
    pin: GPIO4
    id: up_led
    name: "Desk UP Led"
    
  - platform: gpio
    pin: GPIO18
    id: down_led
    name: "Desk Down Led"


#relay stays ON when button pressed and held down, led lights up

binary_sensor:
  - platform: gpio
    name: 'Switch UP'
    pin: 
      number: GPIO2
      mode: 
        input: True
        pullup: True
      inverted: true
    on_press:
      then: 
        - switch.turn_on: relay1
        - switch.turn_on: up_led
    on_release : 
      then: 
        - switch.turn_off: relay1
        - switch.turn_off: up_led
  
  - platform: gpio
    name: 'Switch Down'
    pin: 
      number: GPIO5
      mode: 
        input: True
        pullup: True
      inverted: true
    on_press:
      then: 
        - switch.turn_on: relay2
        - switch.turn_on: down_led
    on_release : 
      then: 
        - switch.turn_off: relay2
        - switch.turn_off: down_led

Appreciate any help I can get.

You just need to turn on the relay and light, add a delay action, then turn off the relay and light?

So should basically combine this



on_press:
      then: 
        - switch.turn_on: relay1
        - switch.turn_on: up_led
    on_release : 
      then: 
        - switch.turn_off: relay1
        - switch.turn_off: up_led

Roughly like this:

on_press:
      then: 
        - switch.turn_on: relay1
        - switch.turn_on: up_led
        - delay: 25s
        - switch.turn_off: relay1
        - switch.turn_off: up_led
  

I did the delay bit from memory on mobile so check that.

Consider adding interlocking too?

You could potentially just use the same buttons.

Using short presses, long presses, double presses etc to trigger different actions.

Thank you,
That code works just as I wanted. Now that simple actions are working as needed I can read about interlocking and test further.

1 Like