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 numberLine1.LevelTimes10, a numberLine1.Ticksand a stringLine1.LastAlarmName. - For the last job, an alarm on
Line1.Levelwith 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
-
Open Automation, then Scripts, and choose New job.
-
Job: Name
Level times ten. -
When it runs: choose Tag change in Starts. Under Watch, type
Line1.Leveland press Enter to add it to the list. Runs when has Its value changes ticked, and Once at start stays off. -
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.valueholds the change:tagPath,previousandcurrent(each withvalue,qualityandtimestamp),initialChange,missedEventsandchanged. See Job scripts. -
Try it. The gateway makes a sample from
Line1.Levelas it stands now, withpreviousthe same ascurrent, and shows the write it would make,Line1.LevelTimes10set to ten times that. Nothing is written. -
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,messageoralarm, 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/statuscounts the jobs per kind inbyTrigger, those waiting inlisteningand the tags watched inwatchedTags.
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
- Scripts for every trigger, the limits and the routes, including
startupandshutdownjobs. - Job scripts for what
eventholds for each trigger. system.utilforsendMessage.- Alarms and From an alarm to a webhook to send an alarm out of the gateway.