Tag Event Script

Hello,
I’m trying to populate memory tag (Dataset) dynamically, through tag event script (On OPC tags).
I have written code (shown below) on value change event.

table = system.tag.read("[.]Chart Array").value
SetPoint = system.tag.read("[.]SP1").value
MeasureValue = system.tag.read("[.]Measure1").value

if SetPoint > MeasureValue:
  system.tag.write("[.]Chart Array", system.dataset.setValue(table, 0, "SP", MeasureValue))
elif MeasureValue > SetPoint:
  system.tag.write("[.]Chart Array", system.dataset.setValue(table, 0, "SP", SetPoint))
else:
  system.tag.write("[.]Chart Array", system.dataset.setValue(table, 0, "SP", SetPoint))

system.tag.write("[.]Chart Array", system.dataset.setValue(table, 0, "Measure", MeasureValue))
system.tag.write("[.]Chart Array", system.dataset.setValue(table, 0, "Delta", SetPoint))

If I run this code all together, it only updates the “Delta” column from dataset & if I run each command individually (keeping other as comment), it works fine
I did not receive any error to work on.

Thank you

That’s because you are overwriting any previous values of the dataset with the change to Delta, as all of your setValue() calls are using the originally retrieved dataset. Instead, you should be assigning each setValue() back to your temporary variable (table), then writing that back to your tag.

2 Likes

Thank you for reply, but I did not understand what are you suggesting.
Could you please explain.

Thank you

The setValue() function doesn’t change the dataset you give it, it creates and returns a new dataset with the change you specify. So your variable “table” isn’t changed in your script. Your changes are only being passed to the tag write function. Restructure your code like this:

table = system.tag.read(....)
....
if whatever:
  table = system.dataset.setValue(table, ....)
elif whatever:
  table = system.dataset.setValue(table, ....)

table = system.dataset.setValue(table, ....)
system.tag.write(....., table)
1 Like

Thank you, it worked.:slight_smile: