# Making a gateway timer script dynamic

**URL:** <https://forum.inductiveautomation.com/t/making-a-gateway-timer-script-dynamic/64858>\
**Category:** Ignition\
**Created:** [October 5, 2022, 6:12pm UTC](https://forum.inductiveautomation.com/t/making-a-gateway-timer-script-dynamic/64858 "2022-10-05T18:12:59Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [October 5, 2022, 6:13pm UTC](https://forum.inductiveautomation.com/t/making-a-gateway-timer-script-dynamic/64858/1 "2022-10-05T18:13:00Z")

</div>

I have a gateway timer script that I want to make dynamic. I have 219 lines in the script that I want to shrink down. I use the same script below for each piece of equipment. What is the best way to go about it.

```auto
#Equipment 10891
		if system.tag.read("[default]Moblie/Downtime10891State").value == 1:
			system.tag.write("[default]Moblie/Downtime10891State", 0)
			system.tag.write("[default]Moblie/Downtime10891", 0)
			system.tag.write("[default]Moblie/Downtime10891Timer", 0)
			system.tag.write("[default]Moblie/Downtime10891State", 1)
		else:
			system.tag.write("[default]Moblie/Downtime10891", 0)
			system.tag.write("[default]Moblie/Downtime10891Timer", 0)

```

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [October 5, 2022, 6:15pm UTC](https://forum.inductiveautomation.com/t/making-a-gateway-timer-script-dynamic/64858/2 "2022-10-05T18:15:08Z")

</div>

Is the 10891 the only thing that changes, if so what is the range?

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [October 5, 2022, 6:18pm UTC](https://forum.inductiveautomation.com/t/making-a-gateway-timer-script-dynamic/64858/3 "2022-10-05T18:18:49Z")

</div>

Assuming it is you can do something like:

```auto
#Equipment 10891
      for x in range(10891, 10895):
		if system.tag.read("[default]Moblie/Downtime"+x+"State").value == 1:
			system.tag.write("[default]Moblie/Downtime"+x+"State", 0)

```

---

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [October 5, 2022, 6:29pm UTC](https://forum.inductiveautomation.com/t/making-a-gateway-timer-script-dynamic/64858/4 "2022-10-05T18:29:53Z")

</div>

I see what you are saying, but the numbers are not in order. They skip around. I have 1744, 1767, 1790, 1792, 1802, 1835, 1836, 1837, 1838, 1839, 1840, 1841, 1842, 1846, 1848, 1850, 1851, 1855, 10888, 10891.

---

<div class="post-metadata">

**Author:** ![kgamble](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kgamble/32/8019_2.png) [@kgamble](https://forum.inductiveautomation.com/u/kgamble)\
**Post date:** [October 5, 2022, 6:53pm UTC](https://forum.inductiveautomation.com/t/making-a-gateway-timer-script-dynamic/64858/5 "2022-10-05T18:53:17Z")

</div>

You could always just provide an ordered list of equipments.

```auto
myEquipment = [1744, 1767, 1790, 1792, 1802, 1835, 1836, 1837, 1838, 1839, 1840, 1841, 1842, 1846, 1848, 1850, 1851, 1855, 10888, 10891]

for x in myEquipment:
    if system.tag.read("[default]Moblie/Downtime"+x+"State").value == 1:
        system.tag.write("[default]Moblie/Downtime"+x+"State", 0)

```

One comment though, depending on your version, `system.tag.read/write` are deprecated and you should be using `system.tag.readBlocking/writeBlocking` for this use-case.

That will also take a list of tags, so you could technically make this more performance (but maybe harder to read) with the following:

```python
myEquipment = [1744, 1767, 1790, 1792, 1802, 1835, 1836, 1837, 1838, 1839, 1840, 1841, 1842, 1846, 1848, 1850, 1851, 1855, 10888, 10891]

# Create a list of all the equipment with a list comprehension
tagPaths = ["[default]Moblie/Downtime%sState" % equipment for equipment in myEquipment]

# Read all of the states at once
states = system.tag.readBlocking(tagPaths)

# Create a list of tagpaths for each one that has a matching state of 1
validEquipment = [tagPath for tagPath, state in zip(tagPaths, states) if state.value == 1]

# write all of those tags to 0, with a list comprehension to create a list of 0s the same length as validEquipment
system.tag.writeBlocking(validEquipment, [0 for _ in validEquipment])

```

EDIT: I don't know why, but the syntax highlighting in these snippets is broken for me, so maybe for you too. If so here is a code-snap

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/f/ff2080f2cd4469bead9d69a739ab7a088ec90f8b.png)

---

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [October 14, 2022, 7:08pm UTC](https://forum.inductiveautomation.com/t/making-a-gateway-timer-script-dynamic/64858/6 "2022-10-14T19:08:05Z")

</div>

when I try this it gives me a error ( no viable alternative at input 'system') I assume it has something to do with the else statement

```auto
myEquipment = [1744, 1767, 1790, 1792, 1802, 1835, 1836, 1837, 1838, 1839, 1840, 1841, 1842, 1846, 1848, 1850, 1851, 1855, 10888, 10891]
		
		for x in myEquipment:
			if system.tag.read("[default]Moblie/Downtime"+x+"State").value == 1:
				system.tag.write("[default]Moblie/Downtime"+x+"State", 0)
				system.tag.write("[default]Moblie/Downtime"+x+", 0)
				system.tag.write("[default]Moblie/Downtime"+x+"Timer", 0)
				system.tag.write("[default]Moblie/Downtime"+x+"State", 1)
			else:
				system.tag.write("[default]Moblie/Downtime"+x+", 0)
				system.tag.write("[default]Moblie/Downtime"+x+"Timer", 0)

```

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [October 14, 2022, 8:37pm UTC](https://forum.inductiveautomation.com/t/making-a-gateway-timer-script-dynamic/64858/7 "2022-10-14T20:37:14Z")

</div>

Remove the +" after the x in the 3rd line of your if statement and in the 1st line of your else statement

---

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [October 14, 2022, 9:03pm UTC](https://forum.inductiveautomation.com/t/making-a-gateway-timer-script-dynamic/64858/8 "2022-10-14T21:03:18Z")

</div>

I get the same error

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [October 14, 2022, 9:09pm UTC](https://forum.inductiveautomation.com/t/making-a-gateway-timer-script-dynamic/64858/9 "2022-10-14T21:09:47Z")

</div>

There's another of the same error in the first line after "else:"

Also make sure your tabs/indentation are all correct. Not sure if the extras are from pasting to the forum or are really present in your script.

If it still doesn't work show us the newly edited script and the full error, which should include some hint as to what line it's occurring on.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [October 14, 2022, 9:16pm UTC](https://forum.inductiveautomation.com/t/making-a-gateway-timer-script-dynamic/64858/10 "2022-10-14T21:16:36Z")

</div>

You can test your code in the Script Console by making a few modifications.

```auto
myEquipment = [1744, 1767, 1790, 1792, 1802, 1835, 1836, 1837, 1838, 1839, 1840, 1841, 1842, 1846, 1848, 1850, 1851, 1855, 10888, 10891]
value = 1
for x in myEquipment:
	if value == 1:
		print("[default]Moblie/Downtime" + x + "State", 0)
		print("[default]Moblie/Downtime" + x + ", 0)
		print("[default]Moblie/Downtime" + x + "Timer", 0)
		print("[default]Moblie/Downtime" + x + "State", 1)
	else:
		print("[default]Moblie/Downtime" + x + ", 0)
		print("[default]Moblie/Downtime" + x + "Timer", 0)

```

This gives error `SyntaxError: no viable alternative at input 'print' (<input>, line 9)`

Fixing the `+ "` problems gives you this:

```auto
myEquipment = [1744, 1767, 1790, 1792, 1802, 1835, 1836, 1837, 1838, 1839, 1840, 1841, 1842, 1846, 1848, 1850, 1851, 1855, 10888, 10891]
value = 1
for x in myEquipment:
	if value == 1:
		print("[default]Moblie/Downtime" + x + "State", 0)
		print("[default]Moblie/Downtime" + x, 0)
		print("[default]Moblie/Downtime" + x + "Timer", 0)
		print("[default]Moblie/Downtime" + x + "State", 1)
	else:
		print("[default]Moblie/Downtime" + x, 0)
		print("[default] bMoblie/Downtime" + x + "Timer", 0)

```

which generates the error, `Traceback (most recent call last): File "<input>", line 7, in <module> TypeError: cannot concatenate 'str' and 'int' objects`

This, in turn, can be fixed by converting `x` to a string.

```auto
myEquipment = [1744, 1767, 1790, 1792, 1802, 1835, 1836, 1837, 1838, 1839, 1840, 1841, 1842, 1846, 1848, 1850, 1851, 1855, 10888, 10891]
value = 1
for x in myEquipment:
	if value == 1:
		print("[default]Moblie/Downtime" + str(x) + "State", 0)
		print("[default]Moblie/Downtime" + str(x), 0)
		print("[default]Moblie/Downtime" + str(x) + "Timer", 0)
		print("[default]Moblie/Downtime" + str(x) + "State", 1)
	else:
		print("[default]Moblie/Downtime" + str(x), 0)
		print("[default]Moblie/Downtime" + str(x) + "Timer", 0)

```

---

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [October 14, 2022, 9:43pm UTC](https://forum.inductiveautomation.com/t/making-a-gateway-timer-script-dynamic/64858/11 "2022-10-14T21:43:17Z")

</div>

```auto
myEquipment = [1744, 1767, 1790, 1792, 1802, 1835, 1836, 1837, 1838, 1839, 1840, 1841, 1842, 1846, 1848, 1850, 1851, 1855, 10888, 10891]
		
		for x in myEquipment:
			if system.tag.read("[default]Moblie/Downtime"+x+"State").value == 1:
				system.tag.write("[default]Moblie/Downtime"+x+"State", 0)
				system.tag.write("[default]Moblie/Downtime"+x+", 0)
				system.tag.write("[default]Moblie/Downtime"+x+"Timer", 0)
error on this line>	system.tag.write("[default]Moblie/Downtime"+x+"State", 1)
			else:
				system.tag.write("[default]Moblie/Downtime"+x+", 0)
				system.tag.write("[default]Moblie/Downtime"+x+"Timer", 0)

```

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [October 14, 2022, 9:45pm UTC](https://forum.inductiveautomation.com/t/making-a-gateway-timer-script-dynamic/64858/12 "2022-10-14T21:45:20Z")

</div>

You still have a stray quote on one of the lines in the `if` block.

---

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [October 14, 2022, 9:54pm UTC](https://forum.inductiveautomation.com/t/making-a-gateway-timer-script-dynamic/64858/13 "2022-10-14T21:54:38Z")

</div>

I tried to remove it but the error keeps moving around

---

<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:** [October 14, 2022, 10:13pm UTC](https://forum.inductiveautomation.com/t/making-a-gateway-timer-script-dynamic/64858/14 "2022-10-14T22:13:39Z")

</div>

Break every string concatenation operation into multiple parts on separate lines to help make it obvious where your problems are. Or use string interpolation/formatting operations to avoid concatenation.

If you're really desperate, paste from an external editor into Ignition's editor one line at a time, paying attention to the red margin markers for syntax errors.

Or go home, have a beer/beverage of choice, and try again after the frustration dissipates.

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [October 14, 2022, 10:19pm UTC](https://forum.inductiveautomation.com/t/making-a-gateway-timer-script-dynamic/64858/15 "2022-10-14T22:19:10Z")

</div>

> [@justinedwards.jle](#):
>
> after the x in the 3rd line of your if statement

> [@justinedwards.jle](#):
>
> and in the 1st line of your else statement

`system.tag.write("[default]Moblie/Downtime"+x+", 0)`  
should be this:  
`system.tag.write("[default]Moblie/Downtime"+x, 0)`

I can recreate the problem and fix it by deleting the +"

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/5/9/5956b042dbd26dfa1413e8fa6850f798d958b46e.png)

It really does go away when I delete the +" from those two lines

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [October 17, 2022, 9:39am UTC](https://forum.inductiveautomation.com/t/making-a-gateway-timer-script-dynamic/64858/16 "2022-10-17T09:39:27Z")

</div>

You're writing 1 to the `State` tag, then 0 immediately after. Why ?

The only difference between the 2 branches is that the `DowntimeXTimer` and `DowntimeX` writes are surrounded by this state change. What is it supposed to do ?

---

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [October 19, 2022, 12:17pm UTC](https://forum.inductiveautomation.com/t/making-a-gateway-timer-script-dynamic/64858/17 "2022-10-19T12:17:57Z")

</div>

This setup keeps track of the downtime for mobile equipment for each month and the data gets reset at the end of the mouth but If the state 1(meaning down) then the state 1 gets set again to continue to add up the time for the next month.

Here is a screen shot of the setup. There are 4 tags for each piece of equipment. Downtime1793(this is stored in a database for each hour the equipment is down), Downtime1793Display(this is your display time in hours,minutes,seconds), Downtime1793State(this is the multistate indicator for status, Downtime1793Timer(this keeps the time). There scripts involved as well like the one I am trying to make better by stream lining it.  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/d/1df3087b186b6a4ec5180fb32cef05a1959e5f72.png)

---

<div class="post-metadata">

**Author:** ![gfeiten](https://avatars.discourse-cdn.com/v4/letter/g/cdc98d/32.png) [@gfeiten](https://forum.inductiveautomation.com/u/gfeiten)\
**Post date:** [October 19, 2022, 1:35pm UTC](https://forum.inductiveautomation.com/t/making-a-gateway-timer-script-dynamic/64858/18 "2022-10-19T13:35:22Z")

</div>

Did you ever get your code working?

Transitor's post walks you through exactly what you need to do:

- fix the two stray quotes
- convert x to a string in your tag writes

---

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [October 19, 2022, 3:58pm UTC](https://forum.inductiveautomation.com/t/making-a-gateway-timer-script-dynamic/64858/19 "2022-10-19T15:58:32Z")

</div>

I believe this will work if I am understanding it correctly

```auto
myEquipment = [1744, 1767, 1790, 1792, 1802, 1835, 1836, 1837, 1838, 1839, 1840, 1841, 1842, 1846, 1848, 1850, 1851, 1855, 10888, 10891]

	for x in myEquipment:
		if system.tag.read("[default]Moblie/Downtime" + str(x) + "State").value == 1:
			system.tag.write("[default]Moblie/Downtime" + str(x) + "State", 0)
			system.tag.write("[default]Moblie/Downtime" + str(x), 0)
			system.tag.write("[default]Moblie/Downtime" + str(x) + "Timer", 0)
			system.tag.write("[default]Moblie/Downtime" + str(x) + "State", 1)
		else:
			system.tag.write("[default]Moblie/Downtime" + str(x), 0)
			system.tag.write("[default]Moblie/Downtime" + str(x) + "Timer", 0)

```
