Turn light or switch on and off 3 times consecutively with script or sequence

Hello all, Im sure this is an easy automation but I cannot seem to find it anywhere how to do this properly. Any help is greatly appreciated. Thank you.

My system:
Home Assistant 0.84.6
Sonoff Basic module flashed w/ Tasmota 6.4.0

What I want to achieve:
I want to run an automation; script or a sequence that when the sonoff switch is turned on (or off) it waits or pauses 1 second then turns off, waits or pauses 2 seconds then turns on, waits or pauses 1 second then turns off, waits or pauses 2 seconds then turns on, waits or pauses 1 second then turns off.
Pretty much I want the switch to turn on and off but wait 1 or 2 seconds in between each toggle. I need the switch to be on 3 times in total.

This is what I have tried with no success…
- id: ‘1545808863578’
alias: Turn Truck On
trigger:
- platform: state
entity_id: switch.sonoff1
to: ‘on’
from: ‘off’
condition: []
action:
service: script.turn_on
entity_id: script.turn_on_truck

   - script:
      turn_on_truck:  
         alias: "Turn on TRUCK"
           sequence:
            - delay:
                 seconds: 1
            - service: switch.turn_off
                 entity_id: switch.sonoff1

I have a script set up so that after 25 minutes I get a flashing light to give me a 5 minute warning that the light is about to be turned off. You can use something similar to do what you want:

  comp_light_control:
    alias: Computer Room Light Control Script
    sequence:
      - delay: '00:25:00'
      - service: light.turn_off
        data:
          entity_id: light.comp_light_template
      - delay: '00:00:01'
      - service: light.turn_on
        data:
          entity_id: light.comp_light_template
      - delay: '00:00:01'
      - service: light.turn_off
        data:
          entity_id: light.comp_light_template
      - delay: '00:00:01'
      - service: light.turn_on
        data:
          entity_id: light.comp_light_template
      - delay: '00:05:00'
      - service: homeassistant.turn_off
        data:
          entity_id: input_boolean.bool_1

Then just call the script from an automation.

- id: '1545847390275'
  alias: Truck on
  trigger:
  - entity_id: switch.truckon
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - data: {}
    service: script.1545847295069

Above, That is my automation…yaml file
Below, is my scripts.yaml
‘1545847295069’:
alias: Tuck On
sequence:
- delay: 00:00:05
- data:
entity_id: switch.truckon
service: switch.turn_off
- delay: 00:00:05
- data:
entity_id: switch.truckon
service: switch.turn_on
- delay: 00:00:05
- data:
entity_id: switch.truckon
service: switch.turn_off
- delay: 00:00:05
- data:
entity_id: switch.truckon
service: switch.turn_on

Here is a picute of my script.yaml file. Im doing something wrong but I cannot figure it out. Does any one have any suggestions?34%20AM

How do I call the script from an automation? I added my automation code in a reply

You call scripts and automations by the alias (with spaces replaced by underscores).

Like this:

- id: '1545847390275'
  alias: Truck on
  trigger:
  - entity_id: switch.truckon
    from: 'off'
    platform: state
    to: 'on'
  action:
  - service: script_turn_on
    entity_id: script.tuck_on

Also notice that you called your script alias “Tuck On” not “Truck On”. You might want to fix that in the script and then in the automation. Unless that’s what you intended…

Hey finity, Thank you for your response. I have been at this for the last 5-6 hours and I can not get the switch to toggle between on and off. Here is what I have on my automations/scripts .yaml files.
07%20PM 26%20PM

Are you using two different switches in this operation? One called “truck_on” & one called “truckon”. That is the way you have it in your screen shots above: “switch.truck_on” in the automation and “switch.truckon” in the script.

If that’s correct and is actually how you have it set up then ignore the rest of this post and we’ll have to try to figure out some other problem…

But if you are really trying to use the same switch in the trigger for the automation as you are trying to control in the script I think you may need to do something a little different for this to work.

Every time the script turns off the switch and then turns it back on again the automation will trigger but since the script isn’t finished yet I’m rally not sure what will happen then.

I think you should create an input boolean to use to trigger the whole sequence.

input_boolean:
  truck:
    name: Truck Control
    # these next two things are optional
    initial: off
    icon: mdi:car

Then in your automation use the boolean to trigger the script:

- id: '1545847390275'
  alias: Truck on
  trigger:
  - platform: state
    entity_id: input_boolean.truck
    to: 'on'
  action:
  - service: script_turn_on
    entity_id: script.truck_control

Then in the script:

truck_control:
  alias: Truck On
  sequence:
    - service: switch_turn_on
      entity_id: switch.truck_on
    - delay: '00:00:05'
    - service: switch.turn_off
      entity_id: switch.truck_on
    - delay: '00:00:05'
    - service: switch.turn_on
      entity_id: switch.truck_on
    - delay: '00:00:05'
    - service: switch.turn_off
      entity_id: switch.truck_on
    - delay: '00:00:05'
    - service: switch.turn_on
      entity_id: switch.truck_on    
    - service: homeassistant.turn_off
      entity_id: input_boolean.truck

Also, for future use please don’t use screen shots to post your code in the forum. use the instructions at the top of the page or like this:

Hey Finity,

Thank you SOOOO much for your help man you lead me in the right direction. I finally found my issue and I got it to work, In the script I had to change the file to a different on/off command. Here is my new script file.

truck_control:
  alias: truck script
  sequence:
  - data:
      payload: 'ON'
      topic: cmnd/truckswitch/power
    service: mqtt.publish
  - delay: 00:00:01
  - data:
      payload: 'OFF'
      topic: cmnd/truckswitch/power
    service: mqtt.publish
  - delay: 00:00:02
  - data:
      payload: 'ON'
      topic: cmnd/truckswitch/power
    service: mqtt.publish
  - delay: 00:00:01
  - data:
      payload: 'OFF'
      topic: cmnd/truckswitch/power
    service: mqtt.publish
  - delay: 00:00:02
  - data:
      payload: 'ON'
      topic: cmnd/truckswitch/power
    service: mqtt.publish
  - delay: 00:00:01
  - data:
      payload: 'OFF'
      topic: cmnd/truckswitch/power
    service: mqtt.publish
  - service: homeassistant.turn_off
    entity_id: input_boolean.truck
1 Like