Trouble getting Value Changed event to fire

Hi all,

I have a tag that has some other tags inside of it. On one of the inner tags, I have a Value Changed event script that I am expecting to fire whenever the tags value is modified. In order to test, I have created a shared script that writes to the tag in question. After running the script, I can see that the tags value has changed but I am unable to get my script to fire. I have placed a print statement at the start of the script but I have yet to see output. Can someone please point me in the right direction?

Thanks in advance,

Israel

Is the shared script containing the called function in your Gateway Scripting Project?

Yes, it is. I call it using the Script Console. I have placed print statements inside of it also. I can see the output. I can see that the tag value has changed. But the event script does not fire after the writing to the tag. When this goes to production the tag type will be changed to an OPC tag. In the meantime, I have temporarily changed it to a Memory tag for testing purposes. I am using Ignition 8.0 and system.tag.writeAsync() to write to the tag. I am not sure if this helps or not but after writing to the tag, I tried to read from it using system.tag.readAsync(). When I print the results, they print as “None”.

This will likely not give you the results you want, system.tag.writeAsync() merely sets the write in process then moves on to the next line. So when you call system.tag.readAsync() you are not guaranteed that the write has occured.

Can you share your script? Even a redacted version would help us help you.

Yes, code below.

def send_data():
	
	print "Inside of send_tcp trying to send"
	
	data = "<TESTDATA><DATETIME> 2014-12-20 4:42:30 </DATETIME></TESTDATA>\r\n"
	print "Sending this data: " + data
	
	newTime = system.date.now()
	print "this is the new time -> " + str(newTime)
	system.tag.writeAsync(["[default]Folder1/Folder2/Outer_Tag/InnerTag1"], [newTime])
	system.tag.writeAsync(["[default]Folder1/Folder2/Outer_Tag/InnerTag2"], [data])
	
	writtenData = system.tag.readAsync(["[default]Folder1/Folder2/Outer_Tag/InnerTag1"])
	writtenDate = system.tag.readAsync(["[default]Folder1/Folder2/Outer_Tag/InnerTag2"])
	
	print ".........The ValueChanged should have fired off by now........."
	
	print "This data has been read from the InnerTag1 tag"
	print writtenData
	print "This date has been read from the InnerTag2 tag"
	print writtenDate 
	print "done"

The .readAsync() function doesn’t return tag data. You need to use readBlocking() for that. (And it would be a list of QVs, not a single value.) And since you are writing asynchronously above that, you are unlikely to have the writes finished by the time you read. You might want to spend some time with the user manual pages for these scripting functions. ):

Okay thank you, I will do that. Can you please tell me if I should expect the Value Changed event to fire when I write to the tag using script?

Don't do this:

system.tag.writeAsync(["[default]Folder1/Folder2/Outer_Tag/InnerTag1"], [newTime])
system.tag.writeAsync(["[default]Folder1/Folder2/Outer_Tag/InnerTag2"], [data])

Do this instead:

system.tag.writeAsync(['[default]Folder1/Folder2/Outer_Tag/InnerTag1','[default]Folder1/Folder2/Outer_Tag/InnerTag2'],[newTime,data])

Same goes for reading tags as well.

Oh also, as @pturmel pointed out, taking a look at the manual entries for these functions would probably be beneficial

https://docs.inductiveautomation.com/display/DOC81/system.tag.writeAsync

Yes, the ValueChanged tag event should fire from any change, weather that change is manual entry in the designer, a script, or an OPC Read.

1 Like

Consider testing your valueChange script by just using the tag editor to set different values. Later, work on your script to write to the tag.

1 Like

Also, keep in mind that tag events (and most any others) run in their own threads in the gateway, from a queued thread pool. Even when using writeBlocking, you can’t be sure that a following read will happen after the corresponding tag change script.