Appearance
Tag Change Scripts
Last updated
A change script runs when one tag's value or quality changes, like a tag event script in other SCADA systems. It is an ordinary script with a Tag Change trigger: it runs in the script sandbox, and is edited, enabled, versioned and logged on the Scripts page like any other.
Creating one from the tag
- Open the tag's settings: Devices → the device → Manage Tags → pencil, or click the tag in the Tag Browser.
- Under Change script, click Create change script (admin and editor only).
- The script editor opens on a new script named
<tag path> change(with a number added if that name is taken), with the tag already chosen, Least time between runs (ms) 250, and a starter line that logs the change. - Write the script, turn it on with the toggle in the toolbar (new scripts start disabled) and Save.
The tag's settings then list every Tag Change script that watches it, linking to each, with disabled next to any that is off. A Tag Change script made on the Scripts page appears there too.
What the script sees
| Field | Meaning |
|---|---|
context.currentValue (also context.value) | The tag's latest value, as text. |
context.previousValue | Its value before this change; null if none was known. |
context.quality, context.previousQuality | good, bad or uncertain, now and before. |
context.valueChanged, context.qualityChanged | Which of the two changed. Both can be true. |
context.initialChange | true on the first run since the trigger started (the script was saved or enabled, or the appliance started). Its previousValue is the value known when the trigger started. |
context.coalesced | How many further changes were merged into this run (0 for a single change). |
context.deviceId, context.tagPath, context.changedAt | Which tag, and when its latest change happened. |
Bursts of changes
Every run is a separate sandboxed process, so a tag that changes many times a second would otherwise start a run for each change. Instead:
- at most one run of the script is in progress at a time;
- runs start at least Least time between runs (ms) apart (default 250, at least 100; set on the script's Trigger tab);
- every change in between is merged into one following run, which sees the latest value, the value before the burst as
previousValue, and the count incoalesced.
Intermediate values are not replayed. If you need every value, record the tag's history instead.
javascript
if (context.valueChanged && context.currentValue === 'true' && !context.initialChange) {
log('Cycle started on', context.tagPath);
}