Need help with relay control

Ok, so ive been going at this one for a while now and cant find a clean, easy way to achieve this.

My setup is a esp32 controlling a relay. I have the esp32 connected and all GPIOs mapped and the relay can be controlled via on/off via an entity.

I need to trigger the relay for 5 seconds on an OFF event. So when I turn on a switch or button in my dashboard, the button should remain off, but the relay must pulse for 5 seconds.

When the switch is turned on, the button changes state to ON and the relay pulses twice for 5 seconds. (5 seconds active, off, 5 seconds active, off).

Is there an easy way to do this? esp32 yaml directly or going to need some fancy automations and helpers/timers etc?

Thanks its driving me nuts!

Could work with an template

switch:
  - platform: template
    id: pulse_relay_1
    name: Pulse Relay 1
    turn_on_action:
    - switch.turn_on: relay_1
    - delay: 5s
    - switch.turn_off: relay_1
    - delay: 1s 
   # etc...
    - switch.turn_off: pulse_relay_1
  - platform: gpio  
    id: relay_1
    internal: true
    # ... 

Does this go in the configuration.yaml?

This goes into your esphome yaml
The Automation will directly run on the esp once you trigger the template switch entity

Right, I’ll give it a shot.

Thanks

Any other ideas I can try?

Your example works well for the 5 second pulse for the off command.

Now I need a on command to pulse the relay twice for say 3 or so seconds, but two pulses.

Thanks

I ended up with this

switch:
  - platform: template
    id: pulse_relay_1
    name: BusHeaterOff
    turn_on_action:
    - switch.turn_on: busheaterOff
    - delay: 3s
    - switch.turn_off: busheaterOff
    - delay: 1s 
   # etc...
    - switch.turn_off: pulse_relay_1
  - platform: gpio 
    pin: 33
    id: busheaterOff
    internal: true
    # ... 
    
    
  - platform: template
    id: pulse_relay_1_1
    name: BusHeaterOn
    turn_on_action:
    - switch.turn_on: busheaterOn
    - delay: 3s
    - switch.turn_off: busheaterOn
    - delay: 1s 
    - switch.turn_on: busheaterOn
    - delay: 3s
    - switch.turn_off: busheaterOn
    - delay: 1s 
   # etc...
    - switch.turn_off: pulse_relay_1_1
  - platform: gpio 
    pin: 33
    id: busheaterOn
    internal: true
    # ... 

This is the reason why i put “# etc” in there :wink:

switch:
  - platform: template
    id: pulse_relay_1
    name: BusHeaterOff
    turn_on_action:
    - switch.turn_on: busheaterOff
    - delay: 3s
    - switch.turn_off: busheaterOff
    - delay: 1s 
    - switch.turn_on: busheaterOff
    - delay: 3s
    - switch.turn_off: busheaterOff
    - switch.turn_off: pulse_relay_1

This would pluse for 3 sek two times.
Does this help?

Thanks, its working.

Do you know a way to stop the GPIO pins changing state if devices reboot.
Right now this is driving a heater, and if there is a power cut the heater will turn on. It seems each time the pi or esp32 reboot, the relay states change, even if returning to the default state, if the relay is inactive on a power down, the relay will energize and become active before the esp reads the codes and then deactivates the relay again. This would be enough to cause my heater to turn on.

Is there a way to avoid this?

Thanks

Restore mode
GPIO Switch — ESPHome

Awesome, does this work for the RPI GPIO pins too?

Thanks

Most likely not since it ESP-Home software

Is there a way to do this for HA and the PI itself too?