Tag change gateway events initialChange and executionCount

I have two tags configured under gateway Tag change event,
Both the tag values are changed at same time.
Sometimes after reseting the gateway or saving the project, even if there is a change in both the tags the script is executed only once, after that if there is no change in project or gateway is not resetted the script is firred twice.
Correct me or please give your thoughts on this

When a project is saved, or the gateway is restarted/reset, the Tag Change Script behaves differently because of two key variables:

  • initialChange

  • executionCount

What happens after a project save or gateway restart?

  1. For the first tag in the hierarchy (Tag1):

    • initialChange = True

    • executionCount = 1

  2. For the second tag (Tag2):

    • initialChange = False

    • executionCount = 2

This means:

  • When executionCount is 1 (as with Tag1 right after restart), the main logic is skipped.

  • For Tag2, executionCount is 2, so the logic runs normally.


:white_check_mark: In short:
After a restart/save, the first tag in the hierarchy is treated as an initial change with executionCount = 1, so I think condition prevents its logic from running. Subsequent tags and all future runs behave normally because executionCount increments beyond 1.

here is the script

if initialChange:
	pass
elif executionCount <= 1:
    pass
else:
	
 some logic here
			```

I am currious what you are actually trying to achieve with the execution count. I have never needed to worry about this.

Any time Gateway scripts are restarted, the execution count will be reset to 0.

See:

Specifically:

Whenever you make changes to your project that affect scripts—like updating your project library or modifying event scripts—all the event scripts, including timers, scheduled scripts, and tag change scripts, are restarted. Once restarted, your executionCount will be reset to 0.

This means the first trip through the scirpt initialChange will be True and executionCount will be 1, the second trip through initialChange will be False, and executionCount will be 2.

Any project save where any Gateway Events are changed, this script will be skipped on the first execution.

I am pretty confident this is how it works if the event is only triggered by a single tag, I am not as certain how additional trigger tags interact. What I expect is that since events are queued, and these would be triggered by subscription at essentially the same time, that value of these flags would be the same across both tag events. That may not be the case though, and I haven't done any testing to confirm. (See Kevin's response below).

For what it's worth (not much) I would write the logic like this:

if not initialChange and executionCount > 1:
    #some logic here

I find it more readable, really either is fine and both work the same.

executionCount is incremented at the end of script or at the starting of the script?

You'll want to check your change triggers too. Options are value, quality, and timestamp. I usually only use value

I have only configured it to 'value'

1 Like

I think the docs are wrong/misleading.

It's intentional that initialChange is true when executionCount == 1.

executionCount increments before the script is executed, you can think of it as "this is the Nth execution".

In fact, initialChange is assigned as:

long executionCount = execStats.executionCount.incrementAndGet();
boolean initialChange = executionCount == 1

So if initialChange and if executionCount <= 1 are logically equivalent.

How do these values intereact with multiple trigger tags set?

The list being able to contain many tags is just a convenience. They aren't any kind of atomic unit. The script is the thing being executed and counted, regardless of what tag triggers it.

One tag will end up being the first execution / initial change. That's all there is to it.

1 Like

Definately not how I thought it worked.

Also, seems like it would result in potential false triggers with many triggers configured.

Would you say, that if your script must only be triggered on true changes, then best practice would be to have an event per trigger?

For gateway tag change event scripts that may be what's necessary.

edit: let me double check all this and make sure the execution count isn't per tag path. I might be wrong.

Ok I was wrong:

| I [Project                       ] [14:37:19.469]: Restarting gateway scripts... project=Test81, collection=Test81
| tagPath=[default]Tag1 initialChange=1 executionCount=1
| tagPath=[default]Tag2 initialChange=1 executionCount=1

So what I said about executionCount being 1 when initialChange is true remains, but it's per tag path, not per script.

Now I'm not even sure what this thread was about.

3 Likes