Tag Change Script - initialChange behaviour

In my Gateway Tag Change Event Scripts I use

if not initialChange:

to prevent the script running after a script change or tag change. And it works well for this. If I don’t use this, the script will run when it shouldn’t.

However, if the gateway is restarted, it also prevents the script running the first time its triggered. In this case I do want it to fire, and this is causing problems. Subsequent triggers run fine after this.
For example, if the gateway is restarted and 4 hours later the event is triggered for the first time since the restart, the initialChange line will prevent it from running.

How do I prevent it running on a script change or tag change, and allow it to run the first time it’s triggered after a gateway restart?

Note: This is on version 8.0.6

1 Like

Sounds like a bug.

I just tested in on version 8.0.11 and it behaves the same. I’ll contact support and see what they think.

pretty sure this has always worked in this manner.

1 Like

Not sure if this is a bug or not. Still talking to support about it.
But in the meantime, to get around this issues the following works:

  1. Create an additional memory tag to store the last value of the trigger.
  2. Use this to test if it is the same as the trigger value, instead of using the initialChange flag.

E.g. instead of this:

if newValue.value == 1: 
	if not initialChange:
		#[run code here]

use this:

# read memory flag
tagPath = '[default]MemoryFlag'
flag = system.tag.readBlocking([tagPath])[0].value
 
if newValue.value == 1: 
	if newValue.value != flag:
		#[run code here]

# update memory flag		
if newValue.value == 1 and flag == 0:
	system.tag.writeAsync([tagPath],[1])
elif newValue.value == 0 and flag == 1:
	system.tag.writeAsync([tagPath],[0])

This works in all cases I’ve tested and is how I expected the initialChange flag to work. But it requires a bit more effort.

1 Like

Does the same bug/nuance exist in 7.9.xx?

I make it a practice to read through this group and use advice like David’s in my scripts, as a “good practice” (regardless of whether a bug bit me or not).

Hi filipg,

Not sure if this happens in 7.9 but I suspect it does.
You can test it by creating this test event:

Each time you toggle the TestTag from 0 to 1, all three prints should appear in the wrapper.log.
If you clear the TestTag, restart the gateway, then set the TestTag to 1: if it has this problem then you will only see print 1 and 2. If there is no problem you should see print 1,2 and 3.