# Script library variables and reading tag values

**URL:** <https://forum.inductiveautomation.com/t/script-library-variables-and-reading-tag-values/90717>\
**Category:** Ignition\
**Tags:** ignition81\
**Created:** [May 22, 2024, 1:38am UTC](https://forum.inductiveautomation.com/t/script-library-variables-and-reading-tag-values/90717 "2024-05-22T01:38:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [May 22, 2024, 1:38am UTC](https://forum.inductiveautomation.com/t/script-library-variables-and-reading-tag-values/90717/1 "2024-05-22T01:38:07Z")

</div>

I wanted to cache some tag values that are basically set once via query when Ignition starts, cache them inside a script library's variables like this:

```python
TAG1 = system.tag.readBlocking('path')[0].value

```

so then I can refer to that instead of having to read the tag each time I need it in a script

```python
project.util.TAG1

```

However, just recently the GW had a patch update applied and it rebooted, and scripts that were using the value were failing due to TAG1 being None.

I assume that this is because when it rebooted, the tag system wasn't up yet before the scripting system was and before the library had initialised, hence the None value trying to read the tag that wasn't there yet. Is this correct?

What would be the best way to resolve this? For now I'm just going to use a dreaded `while` with `time.sleep` to check if it's null and keep reading it until it isn't 😕  
I figure a sleep at such an early once off init stage isn't going to have an effect

---

<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:** [May 22, 2024, 3:50am UTC](https://forum.inductiveautomation.com/t/script-library-variables-and-reading-tag-values/90717/2 "2024-05-22T03:50:13Z")

</div>

> [@nminchin](#):
>
> What would be the best way to resolve this?

No harm in reading the tag if you need to.

Instead of reading the variable, call a function, the function can simply check that the value isn’t 'None' and return the top level variable value, or if it is, read the tag, set the variables value, and then return.

This way you essentially have the same processing time, but can read the tag if needed.

---

<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:** [May 22, 2024, 11:50am UTC](https://forum.inductiveautomation.com/t/script-library-variables-and-reading-tag-values/90717/3 "2024-05-22T11:50:21Z")

</div>

> [@nminchin](#):
>
> For now I'm just going to use a dreaded `while` with `time.sleep` to check if it's null and keep reading it until it isn't

This is an **extremely** bad idea in the load path of a script module. Don't do it.

And the observed behavior shouldn't be a surprise--tags use event scripts, so scripts must load before tags.

Consider caching as @lrose suggests, but use the query in the script, not the tag.

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [May 23, 2024, 3:51pm UTC](https://forum.inductiveautomation.com/t/script-library-variables-and-reading-tag-values/90717/4 "2024-05-23T15:51:38Z")

</div>

You could always add a timer script to check if your global script variables are None and then run the script if it is None.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [May 30, 2024, 8:09am UTC](https://forum.inductiveautomation.com/t/script-library-variables-and-reading-tag-values/90717/5 "2024-05-30T08:09:01Z")

</div>

I'm implementing something along the lines of @lrose's and @pturmel's suggestions, but I'm stumped at something I found.

If I put this into a script library `project.util`:

```python
TEST = None
TEST2 = None
TEST3 = None
TEST4 = NONE
def setTest():
	global TEST
	TEST = 'Hello'
def getTest():
	setTest()
	return TEST

def setTest2():
	global TEST2
	TEST2 = 'Hello2'
def getTest2():
	project.util.setTest2()
	return project.util.TEST2

def setTest3():
	global TEST3
	TEST3 = 'Hello3'
def getTest3():
	setTest3()
	return project.util.TEST3

TEST4 = None
def setTest4():
	global TEST4
	TEST4 = 'Hello4'
def getTest4():
	project.util.setTest4()
	return TEST4

```

and run from the script console:

```python
print project.util.getTest()
print project.util.TEST
print
print project.util.getTest2()
print project.util.TEST2
print
print project.util.getTest3()
print project.util.TEST3
print
print project.util.getTest4()
print project.util.TEST4

```

I get:

```python
>>> 
Hello
Hello

Hello2
None

None
Hello3

None
None

```

Can someone explain why `print project.util.TEST2` isn't equal to `Hello2`? and why `print project.util.getTest3()` isn't equal to 'Hello3'? and both test 4's result in None?

I normally like to include the full library references to make finding things simpler... but maybe I shouldn't be doing that??

I also think maybe I should be using system.util.getGlobals() instead of module variables

---

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [May 30, 2024, 10:41am UTC](https://forum.inductiveautomation.com/t/script-library-variables-and-reading-tag-values/90717/6 "2024-05-30T10:41:50Z")

</div>

> [@nminchin](#):
>
> system.util.getGlobals()

IMHO system.util.getGlobals() is the best solution to persist data in the gateway scope and avoid reset on script frequent reload

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [May 30, 2024, 11:12am UTC](https://forum.inductiveautomation.com/t/script-library-variables-and-reading-tag-values/90717/7 "2024-05-30T11:12:43Z")

</div>

~~Even using getGlobals has the same issue 😕~~

Nevermind that.. I was just being dumb, it does work, just more annoying in terms of accessing the variables.

I'm still very curious to know why my other tests resulted in what they did though

---

<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:** [May 30, 2024, 11:56am UTC](https://forum.inductiveautomation.com/t/script-library-variables-and-reading-tag-values/90717/8 "2024-05-30T11:56:01Z")

</div>

> [@nminchin](#):
>
> Can someone explain

No, but consider running that test from a button (Vision and Perspective) instead of the script console.
