# Tag event valueChanged() script sometimes firing, sometimes not

**URL:** <https://forum.inductiveautomation.com/t/tag-event-valuechanged-script-sometimes-firing-sometimes-not/31152>\
**Category:** Ignition\
**Tags:** ignition79, scripting\
**Created:** [January 8, 2020, 4:45pm UTC](https://forum.inductiveautomation.com/t/tag-event-valuechanged-script-sometimes-firing-sometimes-not/31152 "2020-01-08T16:45:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![szcz](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@szcz](https://forum.inductiveautomation.com/u/szcz)\
**Post date:** [January 8, 2020, 4:45pm UTC](https://forum.inductiveautomation.com/t/tag-event-valuechanged-script-sometimes-firing-sometimes-not/31152/1 "2020-01-08T16:45:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [January 8, 2020, 5:05pm UTC](https://forum.inductiveautomation.com/t/tag-event-valuechanged-script-sometimes-firing-sometimes-not/31152/2 "2020-01-08T17:05:35Z")

</div>

Try replacing

```auto
if currentValue==True:

```

with

```auto
if currentValue.value:

```

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

---

<div class="post-metadata">

**Author:** ![szcz](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@szcz](https://forum.inductiveautomation.com/u/szcz)\
**Post date:** [January 9, 2020, 5:31am UTC](https://forum.inductiveautomation.com/t/tag-event-valuechanged-script-sometimes-firing-sometimes-not/31152/3 "2020-01-09T05:31:44Z")

</div>

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:

 ![obrazek](https://us1.discourse-cdn.com/inductiveautomation/original/2X/e/e69688ff4294b26ae50b319c7bfeb74313e9d10c.png)

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

---

<div class="post-metadata">

**Author:** ![witman](https://avatars.discourse-cdn.com/v4/letter/w/d2c977/32.png) [@witman](https://forum.inductiveautomation.com/u/witman)\
**Post date:** [January 9, 2020, 7:16am UTC](https://forum.inductiveautomation.com/t/tag-event-valuechanged-script-sometimes-firing-sometimes-not/31152/4 "2020-01-09T07:16:30Z")

</div>

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:

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

```

> [@szcz](#):
>
> I can tell it has run because I see that values have been written to writable tags.

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](https://us1.discourse-cdn.com/inductiveautomation/original/2X/d/d1950b19c5a57affd0b0f2d452255d556595bc7a.png)

---

<div class="post-metadata">

**Author:** ![szcz](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@szcz](https://forum.inductiveautomation.com/u/szcz)\
**Post date:** [January 9, 2020, 4:08pm UTC](https://forum.inductiveautomation.com/t/tag-event-valuechanged-script-sometimes-firing-sometimes-not/31152/5 "2020-01-09T16:08:37Z")

</div>

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

```auto
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](https://us1.discourse-cdn.com/inductiveautomation/original/2X/1/1f7102a5ba3b33c793d5881bc6d5bb990d041900.png)  
Actually, I don’t know what does the icon next to the tag name mean.

Thank you for the effort, much appreciated.

---

<div class="post-metadata">

**Author:** ![Kyle\_Chase](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kyle\_Chase](https://forum.inductiveautomation.com/u/Kyle_Chase)\
**Post date:** [January 9, 2020, 4:27pm UTC](https://forum.inductiveautomation.com/t/tag-event-valuechanged-script-sometimes-firing-sometimes-not/31152/6 "2020-01-09T16:27:54Z")

</div>

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”.

---

<div class="post-metadata">

**Author:** ![szcz](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@szcz](https://forum.inductiveautomation.com/u/szcz)\
**Post date:** [January 14, 2020, 6:36pm UTC](https://forum.inductiveautomation.com/t/tag-event-valuechanged-script-sometimes-firing-sometimes-not/31152/7 "2020-01-14T18:36:14Z")

</div>

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
