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?
For the first tag in the hierarchy (Tag1):
initialChange = True
executionCount = 1
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.
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.
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.