I have OPC Tags (TCP Drivers) that add the incoming data into a MS SQL Table every time the Value Changed Tag Event fires. This works great 99% of the time. Sometimes, the value will get written to the database again though. This definitely happens any/every time I go into the Tag Event Value Changed screen, and end up clicking OK, or Apply (regardless if I made any changes or not). Now I understand why it’s doing it at these times, but what I’m wondering is how can I clear the tag’s value after my code executes? I’m already checking to see if currentValue == None at the very beginning. But how can at the end of my code, I set currentValue to None so that it can’t execute again unless the value truly did change? I’ve tried several methods of writing to the tag itself, and nothing seems to error out, but nothing seems to work either. I’m obviously reading the “Message” field from the OPC Item Path. I need to be able to clear out the “Message” field I’m guessing.
No, it's really not. In almost all cases, especially when networks are involved, tags and storage registers should only ever be written by one source. In your case, the proper decision on whether the value changed is to compare to what is in the database, not what was in the tag for the previous event.
In such cases, I use a function in a script module to do the comparison, and the first time it is called, it reads the last value from the database. Which it then stores in a module-global dictionary, which is updated after each actual DB insert. This approach works regardless of quality changes, DB connection failures, gateway restarts, and even script module edits.