Automation

Script API

Reference for the nexus.* scripting API - tags, alarms, history, the database, HTTP, secrets and utilities - and the variables each kind of script gets.

Every gateway script, tag script, library module and console run can use the nexus object, plus log, console and shared. Functions marked await return a promise.

nexus.tag

Read, write and browse tags.

FunctionReturns
nexus.tag.read(tagId)The current { tagId, value, quality, timestamp }, or null if the tag has no value
nexus.tag.readValue(tagId)Just the current value, or null
nexus.tag.readAll([tagId, …])read() for several tags at once
await nexus.tag.write(tagId, value)Writes any writable tag: device tags go to the PLC (with scaling removed), memory tags are stored, reference tags write through. Expression tags are read-only
nexus.tag.browse(prefix?)Tags whose path or ID starts with prefix: [{ tagId, path, name, tagType, dataType, engineeringUnits }]
nexus.tag.exists(tagId)Whether an enabled tag with this ID exists
const level = nexus.tag.readValue('plant.t101.level');
if (level > 90) await nexus.tag.write('plant.t101.inletValve', false);

nexus.alarm

FunctionReturns
await nexus.alarm.active()Open alarm events, newest first
await nexus.alarm.ack(eventId)Acknowledges an alarm event, attributed to the gateway rather than a user

nexus.historian

FunctionReturns
await nexus.historian.query(tagId, from, to?)History points [{ ts, value }]. from and to are a Date, an ISO string or epoch milliseconds
await nexus.historian.aggregate(tagId, minutes, fn = 'avg')One value over the last N minutes: avg, min, max, sum, count or last
const avgFlow = await nexus.historian.aggregate('plant.inlet.flow', 60, 'avg');

nexus.db

FunctionReturns
await nexus.db.query(sql, params?)Rows from the gateway’s database. Parameterized SQL ($1, $2…), run in a read-only transaction with a 5-second timeout

nexus.net

FunctionReturns
await nexus.net.http({ url, method?, headers?, body?, timeoutMs? })An HTTP response { status, ok, headers, text, json }. Object bodies are sent as JSON
const res = await nexus.net.http({
  url: 'https://mes.plant.local/api/orders',
  method: 'POST',
  body: { line: 1, count: nexus.tag.readValue('plant.line1.count') },
});
if (!res.ok) log('MES refused:', res.status, res.text);

nexus.secret

FunctionReturns
nexus.secret.get('name')The secret’s value. Throws if there is no such secret. Values are masked (••••••) in logs and console results
nexus.secret.has('name')Whether the secret exists

See Scripting → Secrets.

nexus.util

FunctionReturns
nexus.util.now()Epoch milliseconds
await nexus.util.sleep(ms)Waits, up to 10 seconds
nexus.util.uuid()A random UUID

Logging and shared code

NameUse
log(...values)Writes a line to the script log
console.log / warn / errorAlso go to the script log
shared.<module>.<function>Functions from the script library

Variables by script kind

ScriptVariables
Tag scriptvalue, previousValue, quality, previousQuality, timestamp, tagId, initialChange
UDT template scriptThe tag script variables, plus udt.instance, udt.prefix, udt.member, udt.params
Timer, startup, shutdownNone beyond the API
ScheduledscheduledFor: when the run was due, as an ISO time
Tag changetagId, value, previousValue, quality
Alarmevent ({ id, tagId, priority, state, valueAtActivation, activatedAt, … }), action (activated, acked, rtn, cleared…)
Message handlerpayload, user ({ id, email, role }), view ({ viewId, params } or null), handler
Library moduleexports: export non-function values with exports.NAME = value

Return values

  • Console: return shows a value as the result.
  • Message handler: the return value goes back to the caller. See Message handlers.
  • Other scripts’ return values are ignored.