Skip to main content
Version: Next

Scripts that start on an event

This guide makes four small jobs, each started by something other than a clock time of day. It is for the engineer who wants a tag derived the moment its source changes, a counter ticked every few seconds, a reset that other scripts can ask for, or something done when an alarm goes active. The script engine (the module io.scadex.cron, shown as Scripts) starts jobs from seven kinds of trigger; A daily counter reset with cron covers the cron one.

Prerequisites​

  • The script engine installed and running; the dashboard shows Scripts in the Automation group while it is installed. See Scripts.
  • The engineer role. Viewers can watch jobs and runs but not change or run them.
  • Four memory tags: a number Line1.Level, a number Line1.LevelTimes10, a number Line1.Ticks and a string Line1.LastAlarmName.
  • For the last job, an alarm on Line1.Level with severity 500 or more, made as in From an alarm to a webhook.

Steps​

1. A tag change: derive a tag the moment its source moves​

  1. Open Automation, then Scripts, and choose New job.

  2. Job: Name Level times ten.

  3. When it runs: choose Tag change in Starts. Under Watch, type Line1.Level and press Enter to add it to the list. Runs when has Its value changes ticked, and Once at start stays off.

  4. What it does: Action script, and in Script:

    const { current, previous } = event.value;
    writeTag('Line1.LevelTimes10', Number(current.value) * 10);
    console.log('was', previous === null ? 'nothing' : previous.value);

    event.value holds the change: tagPath, previous and current (each with value, quality and timestamp), initialChange, missedEvents and changed. See Job scripts.

  5. Try it. The gateway makes a sample from Line1.Level as it stands now, with previous the same as current, and shows the write it would make, Line1.LevelTimes10 set to ten times that. Nothing is written.

  6. Create job.

Now write Line1.Level, from Tag browser or with POST /api/v1/runtime/tags/<tag id>/write and a body of { "value": 12 }. Line1.LevelTimes10 follows within a moment. The job does not start again for the write it just made, even if it watched the tag it writes: it recognises the ids of its own runs for a minute.

The same job over the API, POST /api/v1/modules/io.scadex.cron/jobs:

curl -b cookies.txt -H "Origin: https://app.scadex.io" -H "Content-Type: application/json" \
https://app.scadex.io/api/v1/modules/io.scadex.cron/jobs -d '{
"name": "Level times ten",
"trigger": { "kind": "tag-change", "paths": ["[default]Line1.Level"], "on": ["value"], "initial": false },
"action": { "kind": "script", "script": "const { current } = event.value; writeTag(\"Line1.LevelTimes10\", Number(current.value) * 10);" }
}'

on may also hold quality and timestamp. With initial true (Once at start) the job also runs once as it starts listening, for each watched tag that has a value, with initialChange true and previous null.

2. An interval: tick every five seconds​

Make a job named Tick. Starts: Interval. Every: 5, unit seconds. Keeps time: Fixed rate. Script:

writeTag('Line1.Ticks', (Number(tag('Line1.Ticks')) || 0) + 1);

Fixed rate keeps a grid: a run starts at :00, :05, :10 whatever the last run took, and a time that comes while the last run is still going is skipped and counted. Fixed delay waits five seconds after each run ends instead, so runs never overlap and nothing is skipped. An interval is from 250 ms to 24 hours.

Over the API the trigger is { "kind": "interval", "everyMs": 5000, "mode": "fixed-rate" }.

3. A message: a reset anything can ask for​

Make a job named Reset ticks. Starts: Message. Message: reset-counters. The form shows the call to send it, system.util.sendMessage("reset-counters", payload), with a button to copy it. Script:

const { payload, sender } = event.value;
writeTag('Line1.Ticks', 0);
console.log('Reset by', sender, payload === null ? '' : JSON.stringify(payload));

Send the message from any script that runs as an operator or above, such as a screen script behind a button:

system.util.sendMessage('reset-counters', { line: 'Line1' }); // returns 1: one job took it

Or over the API, which needs the engineer role:

curl -b cookies.txt -H "Origin: https://app.scadex.io" -H "Content-Type: application/json" \
https://app.scadex.io/api/v1/modules/io.scadex.cron/messages/reset-counters -d '{ "line": "Line1" }'

The answer is { "delivered": 1 }, the number of switched-on jobs of that name; 0 means nothing answers to it. The job runs on its own task: a script that sends a message does not wait for it. In Try it the form has Sample event for trying it, where you can give the event.value the script sees, for example { "name": "reset-counters", "payload": { "line": "Line1" }, "sender": null }; a trial of a script that calls sendMessage sends nothing and says so in its log.

4. An alarm: do something when it goes active​

Make a job named Note the alarm. Starts: Alarm. Runs when: An alarm goes active ticked, the other two off. Only on tags: Line1.Level. Severity: 500. Script:

const { name, severity, value } = event.value;
writeTag('Line1.LastAlarmName', name);
console.log(name, 'severity', severity, 'value', value);

Push Line1.Level past the alarm's setpoint. When the alarm goes active the job runs; its cause reads Alarm "<name>" active. Clearing it or acknowledging it does not run this job, unless you tick An alarm clears or An alarm is acknowledged. Without a tag under Only on tags every alarm of at least that severity counts.

5. Read what ran​

  • The page's strip shows Last run failed, Running now, Runs, last 24 h and, when no timed run is due, Listening with how many jobs wait for an event. The list shows what each job Starts when; an event job's Next run reads Listening.
  • Select a job. Its panel shows Events received and dropped for an event job, History, and its runs. A run's trigger is tag-change, timer, message or alarm, and its cause says what set it off.
  • The first three jobs keep their runs differently. Level times ten and Tick use the default for their trigger, Keep failed runs only: a run that succeeded is counted in Runs, last 24 h and shown in the strip of Recent runs, but is not a row, so its log is gone. A run that fails is kept with its log. Reset ticks and a run started by hand keep every run. Change this with History under When it goes wrong.
  • GET /api/v1/modules/io.scadex.cron/status counts the jobs per kind in byTrigger, those waiting in listening and the tags watched in watchedTags.

A change that arrives while the job is still running its last waits, up to 100 of them; past that the oldest is dropped, and the next run's event.value.missedEvents says how many.

Where to go next​