Gateway script tag change- triggered twice

I have a gateway script (Tag change)that it's triggered by a expression tag, doing a insert into a SQL, but it insert double in the data base

Tag change scripts are fired not just when the value changes, but also the quality. I would add a check to make sure that the value has changed:

if previousValue.value != currentValue.value:

Gateway Tag Change events can be configured so that only the Value will trigger the event.

@Hector_Barroso
Please also include a preformatted version of your script and not just a screen snip.

Insure that you have unchecked the Quality, and TimeStamp triggers in the Tags tab.

A few other things:

  1. This should be delegated to a Library script, and the change script itself should be a single line calling that function.
  2. If the tag triggering this event is the same one as the value which is being used in the script, then there is no need to read it's value. It is handed to you in the event as currentValue.value
  3. Each time the event is triggered, it will have a new scope for all of the variables declared within it. So, trigger will always be 1 and only 1 when line 10 is executed.

Your gateway event script should look something like this:

someScriptLibraryName.someFunction(currentValue, initialChange)

someFunction would then look like:

def someFunction(currentValue, initialChange):
    if not initialChange and currentValue.value != '':
        RepetitiveMES.POI.POI_Configuration()

NOTE: I removed all of your logger scripting as it was obvious that it was just for trouble shooting this issue, and I didn't feel like retyping everything.

1 Like

Gateway Tag Change events can be configured so that only the Value will trigger the event.

Huh, never noticed that!

This should be delegated to a Library script

I only recently learned of this best practice. Is it true for all gateway event scripts, or just Tag Change?

All. It avoids the weird Legacy Scoping issues that occur in Gateway Scripts.

It is recommended to do this for all Events, Transforms, and Property Change scripts as well (though that is for maintainability). It's just generally nice to have a single place to look for and manage scripts.

4 Likes

Do you have any other project defined in Ignition that also use the same script? Generally we save a project as a backup and continue to work on the current version. In such cases the gateway scripts get duplicated and saved in these projects. When a tag change is detected by the gateway, the corresponding gateway scripts get executed in all backup projects not only the one which is open because the database is common between all projects! So you will see the action of the scripts running in all projects.

It happened to me few times and I couldn't figure out what's going on! Then I disabled the scripts in all backup projects and resaved them all except in the project that is currently open and the problem was solved!

Check if you have similar situation.

3 Likes

This is why I moved all my Gateway Events scripts into their own project. When I inherited some projects from my predecessor, I thought it would be wise to clone them so I could work on a development version without breaking anything on the live site, not realizing all the duplicate activity I was unleashing that wreaked havoc on the database...

Just added the recomendation, but for some reason it is still duplicate de values on the SQL data base

tag = system.tag.readBlocking('[MQTT Engine]U9/MFL0100/ProcessData/Order')[0].value
logger=system.util.getLogger('MQTT MFL01')
logger.info('Value Change')
trigger = 0
#logger.info(executionCount)
if not initialChange:
	logger.info('Real Value initialChange')
	trigger += 1
	logger.info(previousValue.value)
	logger.info(currentValue.value)
	if previousValue.value != currentValue.value:
		if tag != '':
			if trigger == 1:
				logger.info('Real Value Start')
				RepetitiveMES.POI.POI_Configuration()
				logger.info('Real Value End')

image