ESPhome relay output pulsing

Hello, I am trying to get a relay output on esphome to pulse on and off for 10 seconds at 500ms on 500ms off then stop. Ive searched everywhere to find out how to do this,
I can get it to work with the HA Automation code below, but it is very long winded.
can someone point me in the right direction to doing this within the ESP switch command itself

description: 
trigger:
  - platform: state
    entity_id:
      - group.alarm_beep
    from: "off"
    to: "on"
condition:
  - condition: state
    entity_id: switch.alarm_on
    state: "on"
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.beep
  - delay:
      milliseconds: 500
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.beep
  - delay:
      milliseconds: 500
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.beep
  - delay:
      milliseconds: 500
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.beep
  - delay:
      milliseconds: 500 
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.beep
  - delay:
      milliseconds: 500
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.beep
  - delay:
      milliseconds: 500 
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.beep
  - delay:
      milliseconds: 500
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.beep
  - delay:
      milliseconds: 500  
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.beep
  - delay:
      milliseconds: 500
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.beep
  - delay:
      milliseconds: 500      
mode: restart```

What are you switching?

A relay may not be the best component for this.

I am wanting to switch a piezo through a relay to beep as an entry/exit delay for an alarm I am slowly building hence the 10 seconds of pulsing

Yeah a relay is inappropriate for that.

A general purpose NPN BJT would be more appropriate. Like this:

IMG_0842

Replace ‘arduino’ with ‘esp’.

Also to play sounds use this:

https://esphome.io/components/rtttl.html

1 Like

I do this in my alarm - just pulse the output for around 200ms once then turn it off. the Piezo will emit a short squeak. Pulse it twice you get a double beep!.
not really the right thing to do to a relay, but it doesn’t happen often and requires no hardware.

I created a switch in ESPHOME called ‘beep’ that pulses it once. That way in HA you can loop to pulse it as many times as you need…