Tag event write to file

I am trying to implement a tag event script under the Value Changed event, where the tag value and timestamp are written to a .txt file whenever the value changes. I have the following code below under def valueChanged for a tag called tag1.

system.tag.write("[.]tag2", currentValue.value)
filename = "C:/Users/dsui/Documents/export.txt"
if filename != None:
	system.file.writeFile(filename, str(currentValue.value), 1)
	system.file.writeFile(filename, '\n', 1)
	system.file.writeFile(filename, str(currentValue.timestamp), 1)
	system.file.writeFile(filename, '\n', 1)

Whenever I change the of tag1 in the tag browser, I don’t see anything changes to the export.txt file. The script will successfully write the value of tag1 to tag2.

I also implemented the script below within the script console, and am able to write to the .txt file.

filename = “C:/Users/dsui/Documents/export.txt”
if filename != None:
tag1 = system.tag.read(‘Test/tag1’).value
time = system.tag.read(‘Test/tag1’).timestamp
system.file.writeFile(filename, str(tag1), 1)
system.file.writeFile(filename, ‘\n’, 1)
system.file.writeFile(filename, str(time), 1)
system.file.writeFile(filename, ‘\n’, 1)

TagEvents fire from the gateway scope- do you have the Ignition service running under a user account with access rights to the c:\users directory?

Try modifying the path to c:\temp\export.txt and if the tagevent is able to write to that location.

If you must write to protected directories you’ll need to give the service a user account:

1 Like