# Delay multiple events in button component script-editor

**URL:** <https://forum.inductiveautomation.com/t/delay-multiple-events-in-button-component-script-editor/40286>\
**Category:** Ignition\
**Created:** [November 4, 2020, 12:43pm UTC](https://forum.inductiveautomation.com/t/delay-multiple-events-in-button-component-script-editor/40286 "2020-11-04T12:43:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![andreas.saetran](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@andreas.saetran](https://forum.inductiveautomation.com/u/andreas.saetran)\
**Post date:** [November 4, 2020, 12:43pm UTC](https://forum.inductiveautomation.com/t/delay-multiple-events-in-button-component-script-editor/40286/1 "2020-11-04T12:43:39Z")

</div>

Hi,

Is there any possibilities to make a time delay between multiple events for button handlers?  
In the example below, I need the event in line 8 run a few milliseconds after line 7.

 ![Skjermbilde 2020-11-04 133937](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/5/f5723de8569237d18b92408905be56e7c53054f6.jpeg)

Also interested to know other possibilities for setting different Properties to certain values in a form off sequence.

Brgs  
Andreas S,

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [November 4, 2020, 1:13pm UTC](https://forum.inductiveautomation.com/t/delay-multiple-events-in-button-component-script-editor/40286/2 "2020-11-04T13:13:01Z")

</div>

Probably the simplest method would be to utilize the invokeLater function.

```auto
value=1
event.source.parent.Parameters.STATUS.UPD_BLK=value

def setStatus(comp = event.source.parent, newValue = value):
    comp.Parameters.STATUS.VALUE = value

delay = 200 #delay in ms
system.util.invokeLater(setStatus,delay)

```

---

<div class="post-metadata">

**Author:** ![andreas.saetran](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@andreas.saetran](https://forum.inductiveautomation.com/u/andreas.saetran)\
**Post date:** [November 4, 2020, 2:54pm UTC](https://forum.inductiveautomation.com/t/delay-multiple-events-in-button-component-script-editor/40286/3 "2020-11-04T14:54:26Z")

</div>

Thanks Irose!

Is it possible to define a value to be the inverse of a property parameter?  
Referring to the example below, could value3 be set to be the inverse of “Parameter.STATUS.VALUE”?

 ![Skjermbilde 2020-11-04 155040](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/7/27b18765b16fdb238b38c3ca2fbdf826f04df19f.jpeg)

Brgs  
Andreas S,

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [November 4, 2020, 3:22pm UTC](https://forum.inductiveautomation.com/t/delay-multiple-events-in-button-component-script-editor/40286/4 "2020-11-04T15:22:55Z")

</div>

Of course! Just use the not keyword

```auto
value3 = not event.source.parameter.STATUS.VALUE

```

Though I would probably refactor you code to make it a bit more readable.

```auto
parent = event.source.parent

#You can use True and False in place of 0 and 1 to set boolean values
parent.Parameter.STATUS.SELECTED = True 

def setManMode(comp = parent,value = True):
    comp.Parameter.STATUS.MAN_MODE = value

#You can use the keyword Not to invert a boolean value.
def setStatus(comp=parent, value = not parent.STATUS.VALUE):
    comp.Parameter.STATUS.VALUE = value

def resetManMode(comp=parent,value=False):
    comp.Parameter.STATUS.VALUE = value

def resetSelected(comp=parent,value=False):
    comp.Parameter.STATUS.SELECTED = False

delay1 = 250
delay2 = 500
delay3 = 750
delay4 = 1000

system.util.invokeLater(setManMode,delay1)
system.util.invokeLater(setStatus,delay2)
system.util.invokeLater(resetManMode, delay3)
system.util.invokeLater(resetSelected,delay4)

```

On a side note, I’m not sure what it is that you are trying to accomplish, but it seems to me that this is an awful lot of hoops to jump through. I would be asking if it’s really needed to work all of this complexity into a button click. Would it perhaps be better handled by what ever controller is attached to this process? Generally sequences like what this one appears to be are better handled at the device level, in my opinion. Of course there is a lot of this picture I’m not seeing.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [November 4, 2020, 3:25pm UTC](https://forum.inductiveautomation.com/t/delay-multiple-events-in-button-component-script-editor/40286/5 "2020-11-04T15:25:23Z")

</div>

> [@lrose](#):
>
> Would it perhaps be better handled by what ever controller is attached to this process? Generally sequences like what this one appears to be are better handled at the device level, in my opinion.

Concur. If it really needs a sequence that has to be in Ignition, then a state machine driven by a gateway timer script is the reliable choice.

---

<div class="post-metadata">

**Author:** ![andreas.saetran](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@andreas.saetran](https://forum.inductiveautomation.com/u/andreas.saetran)\
**Post date:** [November 4, 2020, 3:49pm UTC](https://forum.inductiveautomation.com/t/delay-multiple-events-in-button-component-script-editor/40286/6 "2020-11-04T15:49:14Z")

</div>

Yes, this sequence is needed to be handled by Ignition.  
The background for this project is replacing a old ABB Advant scada system that has all these special sequences build-in or handled in other ways.

Pturmel, do you have an example for what you are suggesting?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [November 4, 2020, 4:56pm UTC](https://forum.inductiveautomation.com/t/delay-multiple-events-in-button-component-script-editor/40286/7 "2020-11-04T16:56:29Z")

</div>

Your state machine logic would look something like this, in a script module:

```auto
# Boolean, Integer, Boolean, Date
myTagPaths = ['[default]Some/Tag/Folder/'+x for x in ['SomeInput', 'State', 'SomeOutput', 'SomeTimestamp']]

def myStateMachine():
	# Get current values
	inp, state, outp, ts = [x.value for x in system.tag.readBlocking(myTagPaths)]
	now = system.date.now()

	# Milliseconds:
	elapsed = now.time - ts.time

	# Python has no switch statement--use if/elif/else instead
	if state == 0:
		# Idle state, perhaps. Start when input is true
		if inp:
			# Start pulse 1
			system.tag.writeBlocking(myTagPaths[1:], [1, True, now])
	elif state == 1:
		if elapsed > 500:
			# End pulse 1
			system.tag.writeBlocking(myTagPaths[1:3], [2, False])
	elif state == 2:
		if elapsed > 750:
			# Start pulse 2
			system.tag.writeBlocking(myTagPaths[1:3], [3, True])
	elif state == 3:
		if elapsed > 1250:
			# End pulse 2
			system.tag.writeBlocking(myTagPaths[1:3], [4, False])
	else:
		# reset when both done pulsing and input returns to false
		if not inp:
			system.tag.writeBlocking(myTagPaths[1:3], [0, False])

```

The above generates two half-second output pulses a quarter second apart when an input turns on.  
Call it from a gateway timer script that executes every quarter second.

Naturally, if you do much of this (as you indicate), you’ll want to place the patterns in a constant array and pass parameters to the function to select various collections of tag paths. A single timer script could call such a function for multiple combinations and patterns.

---

<div class="post-metadata">

**Author:** ![MMaynard](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mmaynard/32/3366_2.png) [@MMaynard](https://forum.inductiveautomation.com/u/MMaynard)\
**Post date:** [November 4, 2020, 5:19pm UTC](https://forum.inductiveautomation.com/t/delay-multiple-events-in-button-component-script-editor/40286/8 "2020-11-04T17:19:26Z")

</div>

I personally would use the SFC module for this.
