Switching on a relay for a time

Hi,

When using a relay how what would you put into the config to turn the relay on for xx amount of seconds. So High for eg 3 seconds then go low?

I need to turn on a device that needs a pulse to switch on and a pulse with switch off.

Either I would use a script with a delay or the new timer component.

With a timer:

timer:
  timer_switch:
    duration: '00:00:03'
automation:
  - alias: Turn on the relay
    trigger:
      - YOUR_TRIGGER
    action:
      - service: timer.start
        entity_id: timer.timer_switch
        data_template:
          entity_id: timer.timer_switch
          duration: '00:00:03'
      - service: switch.turn_on
        entity_id: switch.YOUR_SWITCH
  - alias: Turn off the relay at end of timer
    trigger:
      - platform: event
        event_type: timer.finished
        event_data:
          entity_id: timer.timer_switch
    action:
      - service: switch.turn_off
        entity_id: switch.YOUR_SWITCH

Witch a script:

automation:
  - alias: Turn on the relay
    trigger:
      - YOUR_TRIGGER
    action:
      service: homeassistant.turn_on
      entity_id: script.timed_relay

script:
  timed_relay:
    alias: "Turn on relay and set timer"
    sequence:
      - service: script.turn_off
        data:
           entity_id: script.timer_off
      - service: switch.turn_on
        entity_id: switch.YOUR_SWITCH
      # Set new timer
      - service: script.turn_on
        data:
          entity_id: script.timer_off

  timer_off:
    alias: "Turn off relay after 3 seconds"
    sequence:
      - delay: '00:00:03'
      - service: switch.turn_off
        entity_id: switch.YOUR_SWITCH

But all in all it might not be the most reliable way of sending a pulse, an esp8266 with an mqtt endpoint that handle the trigger and the delay would be more reliable.
Using a switch, you cannot guaranty that it will really be on for 3 seconds.

3 Likes