Tag Event Script 'Value Changed' firing twice?

Hey folks,

7.9.7 x64
Java 1.8.0_144
Windows 7

We have a boolean notification tag from the PLC that, on value change, triggers a SQL query.

The script is executing, and the query is correct. The problem I am experiencing is that the script appears to run 2x every time the tag changes. I’ve confirmed that it is only changing one time in the PLC, hoping someone can spot something in my script causing the issue:

if not initialChange:
	DB= "Tank_Temp"
	now = system.date.now()
	t_stamp = system.date.format(now, "yyyy-MM-dd HH:mm:ss")
		
	Mode = system.tag.read("[.]Mode").value
	B_SP = system.tag.read("[.]Bottom_SP").value
	Delta_T = system.tag.read("[.]Delta_T").value
	TankNum = system.tag.read("[.]Tank_Number").value
	Blend = system.tag.read("[.]Blend").value
	BT = system.tag.read("[.]Bottom_Temp").value
	TT = system.tag.read("[.]Top_Temp").value
		
	Quality = system.tag.read("[.]PV Notification Tag").quality
	PrevValue = previousValue.value
	CurValue = currentValue.value
		
	updateAudit = "INSERT INTO TTC_Audit 
		(Mode, Bot_Setpoint, Delta_T, Bot_Temp,
		Top_Temp, TankNum, t_stamp, User, Note, Blend, PrevValue, CurValve) 
		VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
	system.db.runPrepUpdate(updateAudit, 
		[Mode, B_SP, Delta_T, BT, TT, TankNum, t_stamp, 'PanelView', 
		'Tank Settings', Blend, PrevValue, CurValue], DB)

Cheers,
Andrew

It will execute on both the rising edge and falling edge of a boolean unless you check for it.
I always check for both not initial change and current value does not equal previous value.

Thanks for the reply.

The bool will transition to true and remain there until the next change, when it will transition to false.
As such, executing on both rising and falling edges is necessary, is it not?

I added the Prev != Current, that’s a good check. But I am still unsure as to why it is firing twice on a transition.

Resolved:

The tags in question is part of a UDT. So I disabled the script the UDT and made a new tag independent of the UDT to test the script. Still getting 2. So I disabled that tag (now no script active to trigger the query) and toggle the bit - and now one query ran (shouldn’t be any).

Ended up deleting that UDT member, the extra tags, and toggling it from the PLC. Still getting 1 query every state change.

Restarted the gateway and the problem went away. I saved in between every edit, restarted several times, but it appears as though the bug was at the gateway level.

Not quite sure what was driving the issue. In case anyone stumbles upon this same problem.

Cheers,
Andrew

You most likely managed to capture old code in an event listener object so it wouldn’t get garbage collected. See this thread for some suggestions.

Phil,

Very interesting.

Just to be clear, in this solution you reccomend I move the script from the tag events (attached to the udt) to the gateway scripting location, under tag change script?

Thanks,
Andrew

Yes. Tag Events, while conveniently attached to the tags themselves, are in gateway global scope and can only call shared scripts. This means that tag event functions cannot share code (d-r-y) without edits disrupting all projects in the gateway.
Gateway Tag Change Events can respond to all of the same changes as Tag Events, but are part of a project. Edits to project scripts only restart that project, leaving other projects undisrupted. Also, a project can be completely disabled in the gateway – that’s been handy.

1 Like

I can confirm I have this issue and restarting the gateway fixed it straight away. Thanks