Need to set momentary switch in ESPHome

So I have a ESP-01 Relay Board with the below code working. However I need to change this so that the switch is momentry for 800ms then turns off again. Any ideas?

uart:
  baud_rate: 9600
  tx_pin: GPIO1

switch:
   - platform: template
     name: "WIFI Relais EIN/AUS"
     turn_on_action:
      - uart.write: [0xA0, 0x01, 0x01, 0xA2]
     turn_off_action:
      - uart.write: [0xA0, 0x01, 0x00, 0xA1]
     optimistic: true

I found this code online but no idea how to get it into the above.

# Example configuration entry
switch:
  - platform: gpio
    pin: 25
    id: relay
    name: "Gate Remote"
    icon: "mdi:gate"
    on_turn_on:
    - delay: 500ms
    - switch.turn_off: relay

Please can anyoen help at all.

Thank you, yes this is where i got the 2nd bot of code from.

However im struggling how to edit my first code to do this.

So you need to change 500 to 800.

You could just do this:

switch:
   - platform: template
     name: "WIFI Relais EIN/AUS"
     turn_on_action:
      - uart.write: [0xA0, 0x01, 0x01, 0xA2]
      - delay: 800ms
      - uart.write: [0xA0, 0x01, 0x00, 0xA1]
     turn_off_action:
      - uart.write: [0xA0, 0x01, 0x00, 0xA1]
     optimistic: true

Sorry, think you missunderstand.

I need to modify the first code that controls the relay via serial, to work like a momentry switch as shown in the 2nd code.

Sorry if I wasnt clear.

Oh really that simple :slight_smile:

I will give that a go :slight_smile:

switch:
   - platform: template
     name: "WIFI Relais EIN/AUS"
     turn_on_action:
      - uart.write: [0xA0, 0x01, 0x01, 0xA2]
     turn_off_action:
      - uart.write: [0xA0, 0x01, 0x00, 0xA1]
     optimistic: true
     id: relay
     on_turn_on:
     - delay: 800ms
     - switch.turn_off: relay

A button could be an alternative to a switch here.
Optionally with the actions located under a script.

It’s documented. What I posted is documented. Just use the documentation.

1 Like

A button would be a better fit in a dashboard.

switch:
   - platform: template
     id: relay # no name makes this internal only and not exposed to home assistant
     turn_on_action:
      - uart.write: [0xA0, 0x01, 0x01, 0xA2]
     turn_off_action:
      - uart.write: [0xA0, 0x01, 0x00, 0xA1]
     optimistic: true

button:
  - platform: template
    name: "Gate Remote" # exposed to Home Assistant
    on_press:
      - switch.turn_on: relay
      - delay: 800ms
      - switch.turn_off: relay
2 Likes

You’re right. But the point is that the posted fix should work.

2 Likes

Thanks all,

You are all amazing, it is working perfect.

I have one more question guys.

How do I now trigger this button every 2 hours? Do I need to set an automation? Or is it done in the ESP code?

Self fixing, found it in automations :slight_smile:

1 Like