Latching a tag value

Is there a way to “latch” a value being written to the database, or use the previous historical value as the “else” part of an if/then/else?

My specific issue is an machine error code value which is zero during normal running conditions. When a non zero error code is present (and the machine stops), I want to maintain this error code value in the tag history even after the error code has been reset to zero. The tag would only reset to zero when the machine restarts, or some other condition that I choose.

Trying to maintain the previous value with an if/then/else creates a circular reference that it doesn’t like. Any suggestions?


You could bring the machine error code in as an OPC SQLTag. Then create a DB SQLTag that will be the “latched” value. Write a simple Gateway-scope tag change script that listens to the raw machine error code. Whenever it changes from zero, write that value into the DB tag.

Not sure how you detect when the machine restarts, but if you have a way to do that, that would be when you zero out the DB (latched) tag.

Thanks, Carl. The scripting to do that is still eluding me.

If I am looking for a state change to write to the DB tag, how can I keep from writing to the tag when the state changes back to zero?

Thanks for the help…quite new to this stuff here…

Ok, so the tag change script would look something like this. You would put your raw tag as the tag path for a new gateway tag change script. You would change “LatchedValue” to be the path to your latched DB tag that you create.

if newValue.getQuality().isGood() and newValue.value != 0: system.tag.writeToTag("LatchedValue", newValue.value)