# Expression tag is slow sometimes

**URL:** <https://forum.inductiveautomation.com/t/expression-tag-is-slow-sometimes/70771>\
**Category:** Ignition\
**Created:** [March 8, 2023, 9:57am UTC](https://forum.inductiveautomation.com/t/expression-tag-is-slow-sometimes/70771 "2023-03-08T09:57:47Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![grzegorz.kaczmarczyk](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/grzegorz.kaczmarczyk/32/24167_2.png) [@grzegorz.kaczmarczyk](https://forum.inductiveautomation.com/u/grzegorz.kaczmarczyk)\
**Post date:** [March 8, 2023, 9:57am UTC](https://forum.inductiveautomation.com/t/expression-tag-is-slow-sometimes/70771/1 "2023-03-08T09:57:48Z")

</div>

Hi,

sometimes expression tags are not fast enough to keep with updating variables. Below I have examples of good and bad case:

GOOD

 ![good](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/6/06dffea2890a6c67d247b2e2bd06c003eaa6f690.png)  
BAD  
 ![bad](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/9/a91e7b191c1ab05968f092d62c5b527d21cc3cef.png)

How the whole procedure looks? We collect data to a string tag, which is an expression tag. It is event driven, so every time one of the variables changes, the whole expression tag updates. After that, writerequest signal tells us, to send the data to the database. As you can see, the writerequest signal arrives with some data, so the question is, isn't it too fast and that could be causing the error? On the other hand, 99% of case, expression tags updates in time.

Ideas that come to my mind right now:

1. Delay the script that sends data to DB. After we receive writerequest, we can wait some time, so the expression tag will have time to update?
2. Change the expression tag execution from event driven to fixed rate?
3. Ask the PLC programmer, if he can send writerequest a little bit late? The problem is, he is from another company they are very restrictive when it comes to signal processing times and will probably not agree to such a solution.
4. Change the polling rate of collected data to 500 ms and the writerequest to 1000 ms? But will it speed up the expression tag?

Thanks in advance for any ideas!

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [March 8, 2023, 10:36am UTC](https://forum.inductiveautomation.com/t/expression-tag-is-slow-sometimes/70771/2 "2023-03-08T10:36:23Z")

</div>

Consider using a gateway tag event script to monitor the writerequest tag, then use system.opc.readValues() to read of the rest of your values. This will avoid the race condition you're seeing.

---

<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:** [March 8, 2023, 10:37am UTC](https://forum.inductiveautomation.com/t/expression-tag-is-slow-sometimes/70771/3 "2023-03-08T10:37:17Z")

</div>

I would also try using the store and forward system to write to the database, if you're not already. Your tag change events should run super quick.

---

<div class="post-metadata">

**Author:** ![grzegorz.kaczmarczyk](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/grzegorz.kaczmarczyk/32/24167_2.png) [@grzegorz.kaczmarczyk](https://forum.inductiveautomation.com/u/grzegorz.kaczmarczyk)\
**Post date:** [March 8, 2023, 11:50am UTC](https://forum.inductiveautomation.com/t/expression-tag-is-slow-sometimes/70771/4 "2023-03-08T11:50:01Z")

</div>

Yes, but how it will affect the expression tag execution? Because we are sending result of expression tag to database.

---

<div class="post-metadata">

**Author:** ![grzegorz.kaczmarczyk](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/grzegorz.kaczmarczyk/32/24167_2.png) [@grzegorz.kaczmarczyk](https://forum.inductiveautomation.com/u/grzegorz.kaczmarczyk)\
**Post date:** [March 8, 2023, 11:50am UTC](https://forum.inductiveautomation.com/t/expression-tag-is-slow-sometimes/70771/5 "2023-03-08T11:50:47Z")

</div>

Same here, we are sending result of expression tag, which is calculated in Ignition, so how will store and forward improve it?

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [March 8, 2023, 11:53am UTC](https://forum.inductiveautomation.com/t/expression-tag-is-slow-sometimes/70771/6 "2023-03-08T11:53:01Z")

</div>

Try doing it all in the script, including the write to the db.

---

<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:** [March 8, 2023, 12:09pm UTC](https://forum.inductiveautomation.com/t/expression-tag-is-slow-sometimes/70771/7 "2023-03-08T12:09:01Z")

</div>

> [@JordanCClark](#):
>
> doing it all in the script

This! Don't mix expression tags with scripts. If you must have a script, replace the operation of the expression with code in the script.

---

<div class="post-metadata">

**Author:** ![grzegorz.kaczmarczyk](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/grzegorz.kaczmarczyk/32/24167_2.png) [@grzegorz.kaczmarczyk](https://forum.inductiveautomation.com/u/grzegorz.kaczmarczyk)\
**Post date:** [March 8, 2023, 2:21pm UTC](https://forum.inductiveautomation.com/t/expression-tag-is-slow-sometimes/70771/8 "2023-03-08T14:21:48Z")

</div>

That sounds quite logical. I have this expression for example:

 ![expression editor](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/2/d297409f79c586cd51425f2fb09a7e7fed25970f.png)

What is the most simple way to rewrite this in script?

---

<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:** [March 8, 2023, 3:00pm UTC](https://forum.inductiveautomation.com/t/expression-tag-is-slow-sometimes/70771/9 "2023-03-08T15:00:37Z")

</div>

Something like this:

```auto
tagPaths = ["[.]SerialNumber","[.]xOK","[.]xNOKProcess","[.]xNOKProduct","[.]Height","[.]HeightMin","[.]HeightMax"]

values = [val.value for val in system.tag.readBlocking(tagPaths)]
finalStr = ""
xSum = 0

for i,val in enumerate(values):
	if i == 0:
		finalString = val + "|"
	elif i < 4:
		xSum += int(val)
	elif i == 4:
		finalString += str(xSum) + "|" + val.replace(".",",") "|"
	else:
		finalString += val.replace(".",",") + "|"

system.tag.writeAsync(["[.]pathToWrite"],[finalString])

```

Maybe not the cleanest way to do it, kind of has a magic number feel to it. The index numbers in the loop are dependent on how you build the tag paths list, there are issues, but this should show you the basics that you would need to accomplish what the expression is doing. You could build the string in a single concatenation operation if desired. I also assumed that all of your tag values are strings since you were doing the integer conversion. If they are not all strings then the conversions may be un-needed.

Note that if you use a gateway tag event script (recommended) then the relative tag paths wont work and you will need to use the full path.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [March 8, 2023, 3:05pm UTC](https://forum.inductiveautomation.com/t/expression-tag-is-slow-sometimes/70771/10 "2023-03-08T15:05:50Z")

</div>

As there is already a race condition, I'd suggest to replace the tag paths with their OPC paths and use system.opc.readValues().

My take on the post processing is a bit different, and may have to change, since the OP hasn't stated the data types.

```python
values = ['123135848413',
		  True,
		  False,
		  False,
		  '10.123',
		  '9.000',
		  '12.000',
		 ]

for i, value in enumerate(values):
	if 1 <= i <= 3:
		values[i] = int(value)
	elif 4 <= i <= 6:
		values[i] = value.replace('.',',')

stringOut = '{}|{}{}{}|{}|{}|{}'.format(*values)
print stringOut

```

Output:

```plaintext
123135848413|100|10,123|9,000|12,000

```

---

<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:** [March 8, 2023, 11:34pm UTC](https://forum.inductiveautomation.com/t/expression-tag-is-slow-sometimes/70771/11 "2023-03-08T23:34:46Z")

</div>

Maybe I misread, but I thought you might have been running INSERT queries from a tag change script, in which case running them via the S+F system would be quicker, I believe

---

<div class="post-metadata">

**Author:** ![grzegorz.kaczmarczyk](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/grzegorz.kaczmarczyk/32/24167_2.png) [@grzegorz.kaczmarczyk](https://forum.inductiveautomation.com/u/grzegorz.kaczmarczyk)\
**Post date:** [March 10, 2023, 7:01pm UTC](https://forum.inductiveautomation.com/t/expression-tag-is-slow-sometimes/70771/12 "2023-03-10T19:01:27Z")

</div>

Hi,

I've started moving the code from expression tags to scripts. Do you have any idea, how to delay the start of the script for 50 ms? The writeRequest tag comes to Ignition in the same moment as one of the data tags and we need to delay the script, so all data is packed. I've tried using timer from threading library, but the script doesn't launch.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [March 10, 2023, 7:21pm UTC](https://forum.inductiveautomation.com/t/expression-tag-is-slow-sometimes/70771/13 "2023-03-10T19:21:48Z")

</div>

[system.util.invokeLater](https://docs.inductiveautomation.com/display/DOC80/system.util.invokeLater) ?

---

<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:** [March 10, 2023, 7:29pm UTC](https://forum.inductiveautomation.com/t/expression-tag-is-slow-sometimes/70771/14 "2023-03-10T19:29:38Z")

</div>

> [@grzegorz.kaczmarczyk](#):
>
> Do you have any idea, how to delay the start of the script for 50 ms?

Short answer, don't. If this is needed then you need to look at the system design.

See good advice here:

> [@Delay in Tag Event Script (Value Changed)](https://forum.inductiveautomation.com/t/delay-in-tag-event-script-value-changed/70666/6):
>
> In general, any delays of any kind are not acceptable in tag value change scripts, or any other tag event, due to the shared execution thread pool. Don't do it. You will bog down your tag infrastructure. Instead, design a state machine, perhaps with a timestamp in a memory tag, that is updated by a regular timer event. Do not use jython time, thread, or sleeping functionality in such events (or anywhere in Ignition, really).

---

<div class="post-metadata">

**Author:** ![grzegorz.kaczmarczyk](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/grzegorz.kaczmarczyk/32/24167_2.png) [@grzegorz.kaczmarczyk](https://forum.inductiveautomation.com/u/grzegorz.kaczmarczyk)\
**Post date:** [March 13, 2023, 8:50am UTC](https://forum.inductiveautomation.com/t/expression-tag-is-slow-sometimes/70771/15 "2023-03-13T08:50:41Z")

</div>

Okay, so we will look to the system design.

Is it better to use Gateway Tag Change script or Tag Value Change script?

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [March 13, 2023, 11:14am UTC](https://forum.inductiveautomation.com/t/expression-tag-is-slow-sometimes/70771/16 "2023-03-13T11:14:00Z")

</div>

Better to use Gateway Tag Change. Tag events have a limited thread pool and queue limit.

---

<div class="post-metadata">

**Author:** ![grzegorz.kaczmarczyk](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/grzegorz.kaczmarczyk/32/24167_2.png) [@grzegorz.kaczmarczyk](https://forum.inductiveautomation.com/u/grzegorz.kaczmarczyk)\
**Post date:** [March 13, 2023, 1:26pm UTC](https://forum.inductiveautomation.com/t/expression-tag-is-slow-sometimes/70771/17 "2023-03-13T13:26:11Z")

</div>

A different kind of question, but what happens, when you poll OPC too fast? Are you losing data? And why?

---

<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:** [March 13, 2023, 1:47pm UTC](https://forum.inductiveautomation.com/t/expression-tag-is-slow-sometimes/70771/18 "2023-03-13T13:47:11Z")

</div>

> [@grzegorz.kaczmarczyk](#):
>
> when you poll OPC too fast?

No, you just don't get the pace you request. If you use a native IA driver, the driver diagnostics will show what pace you are really getting. When there's no free time in the scan, then writes can be delayed significantly more than normal.

When using an external OPC server, you would have to look at its own diagnostics to determine your overload level, if any.
