This lesson is part of the Scripting in Ignition course. You can browse the rest of the lessons below.

LESSON LIST

Autoplay Off
Take topic challenge

LESSON

Tag Event Scripts

Description

Learn how to configure a script that runs whenever the value of a Tag changes.

Video recorded using: Ignition 8.0

Transcript

(open in window)

[00:00] In Ignition, sometimes we might want to configure some custom functionality when a tag changes in some way. Ignition offers many built-in ways of handling changes in tag value from alarming to transaction groups to tag history. But when nothing does quite what we need, we can configure a script on a tag that will run under certain conditions. To demonstrate, I've set up two tags here, one is My Tag and another called Last Value they're both memory tags and they both have a data type of String. My goal for this example is simply that when I write to My Tag, the previous value that was written on My Tag is stored to the Last Value tag. Now this is a very simple goal, we could do things that are far more advanced with this script like writing Sequel Queries or doing some advanced logging but this will serve as just a simple demonstration. So to configure my script, I'm going to right-click on My Tag and select Edit Tag and then I'm going to scroll down until I find the Tag Event Scripts property in the Tag Editor, and then once I'm here, I'm just going to click on the little icon here on the right and it will bring up the Tag Event Scripts configuration for the tag. On the left here you can see all of the different events we can configure scripts on, we could for instance, write a script when the quality of our tag changes, or when alarms are triggered, or when they're cleared or acknowledged, but for this example, I really only want to run a script when the value of My Tag changes. So I'm going to select a Value Change script. Within this script, we're provided a number of parameters. We're provided a Tag Object that we can use to get tag properties, a string tag path to the tag, which is useful when we're working with say, a UDT and we don't automatically know our tag path. Next we have the previous value and current value parameters These are pretty straightforward, the previous value is of course the last value on the tag and the current value is the value we're updating the tag to Now just one note on these two parameters, they are actually what's called Qualified Values meaning that they're really objects that have value, quality, and time stamp properties. I'll demonstrate what I mean in just a moment. Next we have initial change, it's worth noting that the Value Changed Event can fire when the tag is first started or reinitialized. So if we don't want those changes to run our script, we can check the value on the initial change flag. We also have a missed events flag which essentially will be true if the events are coming in so fast that we don't have time to handle them all. So now that we've gone over the basics of our parameters it's time to write our script, so I'll click within the function, and the first thing that I want to add in here is a couple checks, it might be tempting to think that the Value Changed Event script will only run when the value on our tag changes. In fact, it also runs when the quality of our tag changes. As such, if we want to prevent this script from firing whenever the quality of our tag changes, we'll need to compare the previous value to the current value. So that's the first thing I'm going to do in my script. So I'm going to say, if previous value and then here's where our Qualified Value comes into play, so I actually need to reference the value as previousvalue.value and then I'm going to do is not equal to currentvalue.value and then let's say I also want to filter on the initial change so this is the initial change I don't want to run the script So I'm going to add a clause to my if statement here and say, and initial change equals equals false So code within this if statement will only run if the value of the tag has changed and it is not the initial change. So I'm going to put all of my code inside this if statement. So remember the functionality that I wanted in this script, whenever the value of this tag changes, I want it to write its previous value to the Last Value tag. We can see now that within this script that's pretty easy to do because we have access to the previous value, so I actually already have the code I'll need to configure this, I'll just paste it in now, and as we can see, it's simple a call to system.tag.writeBlocking which will write a value to the Last Value tag and the value it will write is previousvalue.value This is actually all it takes to configure the functionality I'd like, so now I'm going to come down and hit Commit and then I'm going to click OK and now I want to test it out, so if I come in here and change the value on My Tag, let's just say I change it to Hi and then I hit Enter, we can see that the Tag Event Script is working as expected.

You are editing this transcript.

Make any corrections to improve this transcript. We'll review any changes before posting them.