DataTalk

View Scripts

Extend a view with JavaScript — read and write tags, listen for changes, open components, show dialogs and call services from the built-in script editor.

On this page

When a single command is not enough, a view can run a script. Scripts are useful for operations such as modifying several tags at once, performing calculations, executing conditional logic or implementing custom operator actions.

Script editor

Click Script in the toolbar of the View Editor. The editor area is replaced by a code editor; the View button in its top-left corner returns to the drawing.

Script editor of a view — list of functions on the left, code editor with the init and destroy template on the right

The editor has three parts:

Function list (left) — every function that the view can call, grouped by category. Click a function to read its description in the Info box below the list. Show more… opens a help window with the parameters and an example.

Code editor (right) — a code editor with line numbers and an optional minimap. The Lines and Minimap check boxes in the top-right corner switch them on and off.

Info box — the description of the selected function.

Lifecycle template

A new script starts with two functions:

//called when view is first rendered
this.init = async () => {
  //you can start timer here
  //this.timer=setInterval(async ()=>{},1000);
}

//called when view is destroyed
//free all resources such as timers here
this.destroy = async () => {
  //clearInterval(this.timer);
}

this.init runs when the view is first rendered — start timers and listeners here. this.destroy runs when the view is closed — stop everything you started, so that timers do not keep running after the operator has left the screen.

Available functions

The functions are available on the helper object, for example helper.open(...) or helper.writeTagValues(...).

Help window of the open function — the command and properties parameters and the example helper.open('view.viewID')

CategoryFunctions
LinkedTablegetData — get data from a linked table.
LivereadTagValues, readTagValuesWithStatus, readTagValue — read tags; writeTagValues — write tags; listenForTags — call a function when tag values change; readLiveAlarms, listenForAlarms — read and watch active alarms.
PropertiesgetProperties — read the array of properties (parameters) passed into the view.
UsergetUserName, getUserGroups — the logged-in user and the groups they belong to.
FilesreadFileFromUserData, readBinaryFileFromUserData, readJsonFromUserData, writeFileIntoUserData — read and write files in the userData directory.
ReportsgenerateReport — generate a report.
ScriptsrunScript — run a server-side script.
NotifysendNotification — send a notification to a predefined group of services.
Date&TimesetDateRange, setLiveInterval — set the date range or the live interval for all components of the view that accept it.
Openopen, openParent, openComponent — open views, charts, dashboards and other components; replace what a component shows.
Dialogopen — popup dialog; closeDialog; alertDialog — message box; confirmDialog — returns true if the user accepts.
ElementsgetId — get an element of the view by its ID.
HistoryRead the history of tag values and alarms, and tag statistics.
APIapiGET, apiPOST — call a remote server through a REST API.
SoundplaySound, stopSound — play a sound with a repeat count (or 0 for endless) and stop it.
AIchatWithAI, searchUsingAI — ask the AI assistant, search documents with the AI agent.
UtilitiesformatDate, formatNumber — format values according to a pattern.

Examples

Write tags

writeTagValues writes several tags at once. Tags are given as key: value pairs, where the key is the tag name:

await helper.writeTagValues({ 'PLC1.tag1': 23, 'PLC2.tag2': 'String value', 'PLC1.tag3': [10, 20, 30] });

Read a tag

readTagValue returns the current value of one tag. The tag name has the format connection.tagname:

let value = await helper.readTagValue('Global memory.value');

Help window of readTagValue — parameter tag and the example

Read the view parameters

getProperties returns the parameters that the view was opened with. A parametric script builds the names of its tags from them:

const p = await helper.getProperties();
const loadTag = 'Global memory.' + (p[1]?.value ?? p[1] ?? 'value');   // parameter {2}

Simulate a machine

A script with a timer can play the role of a device while you design a view. The motor simulation from Tutorial — Reusable Motor Faceplate reads the load tag and the run tag from the parameters of the view, moves the load towards a target while the motor runs and writes it back every second:

const load = Number(await helper.readTagValue(loadTag)) || 0;
await helper.writeTagValues({ [loadTag]: Math.round(load + (target - load) * 0.25) });

The script reads the current load and moves it towards a target instead of overwriting it, so buttons that also change the tag keep working.

Open a view with a parameter

helper.open('view.viewID', [{ type: 'number', value: 3 }]);

The viewID is the value copied with Copy ID from the Project tree.

Free your resources

Everything you start in this.init — timers created with setInterval, tag listeners — should be stopped in this.destroy.