Tag event valueChanged() script sometimes firing, sometimes not

Hello all,

I need an advice or at least to be pointed to right direction.

My situation:
I have boolean tag scanned every 1 second. That tag is part of UDT and UDT parameters define correct OPC path. That tag in the UDT has valueChanged() event programmed. In case that boolean tag turns TRUE, script in event valueChanged() should run (the script begins with “if currentValue==True:”).

The basic workflow is as follows:

  • read value in “other tag”
  • in case the value in “other tag” is not populated yet, sleep(0.4)
  • as soon as the value is populated (it always is - sooner or later - when boolean goes True):
    • do some calculations
    • query database (these database queries take =< 30 ms - DB is not the bottleneck)
    • write response back to writable tags

My problem
Sometimes, it seems the script is not fired at all … the first tag turns True, shortly after that, the other tag is populated and script should run … I can tell it has run because I see that values have been written to writable tags.

In case everything goes flawlessly, the whole operation is finished within blink of an eye (from receiving TRUE to writing data back).

In case things go south, the first tag goes True, second tag is populated shortly after that and nothing happens. The whole operation must be finished under 5 seconds, usually takes less then 1 second if everything is OK, but in case of a problem, you can wait 30 seconds and nothing happens.

The script is really trivial, the same goes for database tables with hundreds of records.

I have no idea where that event can get lost.

Many thanks in advance for pointing me to the right direction, right now, I am clueless.

Take care.

Try replacing

if currentValue==True:

with

if currentValue.value:

currentValue is a QualifiedValue object, not the actual value of the tag.

Hello Pturmel,

Thanks for the hint. In reality, I did not copy-paste the code, I wrote it as pseudo-code. The mentioned code section you are mentioning looks like follows:

I am thinking to anonymize the whole code and paste it here.

Yes, we’ll need to see more of the script to get an idea what may be going wrong. If it works sometimes, the problem is not your first line with the if statement. It works, though you could shorten it to the following:

if currentValue.value and not previousValue.value:
   # Do stuff.

Does this mean that when it fails it fails after writing something? If so, point us to the line where the last value that does work is written.

Please post any code with three back ticks (```) on a line above and below to make it readable (or use the preformatted text button in the post editor):
image

1 Like

Hello witman,
below the script that sometimes runs, sometimes does not:

from time import sleep
dbconn = system.tag.read("[~]TAN_Globals/IgnitionRuntimeConnection").value
if  (currentValue.value == True and previousValue.value==False):
	requested_project = system.tag.read("[.]in_projectnumber").value
	
	while requested_project == 0:
		requested_project = system.tag.read("[.]in_projectnumber").value#int(hex(system.tag.read("[.]in_projectnumber_test").value)[2:])
		sleep(0.2)	
		requested_mould = 0 # = int(hex(system.tag.read("[.]in_projectnumber").value)[2:])
	

	data = shared.T07_CONVEYOR.get_data_for_project(requested_project, mouldnumber = 0 )#get_data_for_project(requested_project,requested_mould)
	word9 = shared.T07_CONVEYOR.conveyorize_word9(data[5],data[4],data[3])#conveyorize_word9(data[5],data[4],data[3])
	result = 0
	result += system.tag.write("[.]out_alloy",data[0]) 
	result += system.tag.write("[.]out_wheeldiameter",shared.T07_CONVEYOR.to_conv_format(int(data[1])))
	result += system.tag.write("[.]out_wheelheight",shared.T07_CONVEYOR.to_conv_format(int(data[2])))
	result += system.tag.write("[.]out_wheeltype_template_diameterdrill",word9)  
	if result < 4:
		#pass #TODO: raise an alarm! - writing data failed!
		shared.T07_CONVEYOR.track_event('writing data failed')
	else: 
		system.tag.write("[.]out_data_sent_correctly",True)
		shared.T07_CONVEYOR.track_event('written data sent correctly')
		str_sql_get_id = "SELECT machineid FROM TAN_MAC_CFG WHERE RTRIM(LTRIM(Description)) = (?)"
		result = system.db.runPrepQuery(str_sql_get_id,[system.tag.read("[.]station_name").value],dbconn)
		str_sql_insert = "INSERT INTO [T07_CONVEYOR_QTY]([Counter],[MachineID],[timestamp],[ShiftStartTS],[projectnumber],[mouldnumber]) VALUES ( (?),(?),SYSDATETIMEOFFSET(),(?),(?),(?) )"
		args = [0,result[0][0],system.tag.read("TAN_Globals/MasterClock/ShiftStartTS").value,requested_project,requested_mould]
		tx_insert = system.db.beginTransaction()
		system.db.runPrepUpdate(str_sql_insert,args,dbconn,tx=tx_insert)		
		system.db.commitTransaction(tx_insert)
		system.db.closeTransaction(tx_insert)

I know it calls external methods, e.g. shared.T07_CONVEYOR.conveyorize_word9(…). I am convinced these methods are flawless, they have been run multiple times with various parameters. Never failed.

Does this mean that when it fails it fails after writing something? If so, point us to the line where the last value that does work is written.
No - the whole code either goes through or does not go through at all… To me it looked like the event is not firing despite the fact the tag turned True.

Problem is currently solved by workaround - I am running the script not in valueChanged() in UDT, I rather run the script as gateway event script reacting to tag value change. After the re-design to gateway event script based on tag change, it is significantly better. The script wasn’t run properly only once out of approx. 30 attempts. Attempt means putting an item to production line, which effectively renders the in_ready_to_receive_data tag True (PLC says that).

The system resources can’t be the problem. If the script is fired, it takes less then 150ms from beginning to end (i know that, b/c I am logging events in the script to DB table).

I am however unhappy because I don’t know what is wrong with the UDT valueChanged() not firing. The tag screenshot is taken below:
obrazek
Actually, I don’t know what does the icon next to the tag name mean.

Thank you for the effort, much appreciated.

Check the tag diagnostics on that tag. That icon beside the tag is telling you that the script is failing, due to some exception. It will tell you why the script is failing. The whole code is not “going through”.

2 Likes

Hi Kyle, thank you for your answer. Apparently, I had an issue in code I could not see. I have re-written the code from scratch optimizing some grey areas, now the code is prettier and also faster (SQL-wise). Long story short - I learned what does that icon mean, I have better code than previous version I was not able to troubleshoot. I think it’s a WIN for me and this thread can be locked.

Thank you all for your effort, take care.
SZ