These functions are available in every script without any prefix. They control
the script lifecycle, timing, logging and access to device status.
The most important of these is run() β every continuous loop must poll it
so the device can stop the script during configuration updates. See the
product overview for the full lifecycle rules.
Functions
run() β Returns true while the script should keep running; the required condition for every main loop.
waitMS() β Pauses the script for the given number of milliseconds.
print() β Writes values to the device system log / script console.
log() β Writes a message to the persistent system event log β short form with a severity, or extended form with grouping, priority and an attached value.
setEnv() β Sets a device environment / configuration variable at runtime.
now() β Returns the device uptime in seconds; the result carries a .sAgo() method for measuring elapsed time.
datetime() β Returns the current date and time as an object, including the unix epoch.
string() β Converts numbers and objects to strings for logging and transmission.
Returns true while the script should keep running; the required condition for every main loop.
Polls the script scheduler. While the script should keep executing, run()
returns true; when the device needs the script to stop (for example during a
configuration update), it returns false.
Binding your main loop to run() allows the device to stop the script during
configuration updates and prevents unexpected behavior. Your script must exit
within 5 seconds of run() returning false, otherwise it will not be
updated during configuration changes, a device restart will be required, and
objects referenced by the script may become undefined.
Calling run() also syncs the IO variables with your local variables β see
syncIO() for mid-loop synchronisation.
Returns
boolean
true while the script should continue; false when it must shut down.
Example
while(run()){
// monitoring logic here
waitMS(100);
}
// falls through here during a configuration update β exit promptly
Pauses the script for the given number of milliseconds.
Suspends script execution, yielding processor time to the rest of the device
firmware. Use it inside your main loop to set the measurement cadence.
Avoid single waits longer than a few seconds β long blocking waits delay
script shutdown during configuration updates (the 5-second rule). For long
intervals, loop shorter waits bound to run().
Arguments
Name
Type
Required
Default
Description
milliseconds
number
yes
β
How long to pause, in milliseconds.
Example
while(run()){
// take measurements ...
waitMS(5000); // repeat every 5 seconds
}
Writes values to the device system log / script console.
Accepts any number of arguments and writes them to the script console, which
is visible in the on-board web interface. Numbers and objects should be
converted with string() when concatenating into a message.
Arguments
Name
Type
Required
Default
Description
valueβ¦
any
yes
β
One or more values to log. Objects should be wrapped in string() to serialise them.
Writes a message to the persistent system event log β short form with a severity, or extended form with grouping, priority and an attached value.
Unlike print() (console output), log() writes to the internal device event log.
If cloud synchronisation is enabled, the event log is automatically uploaded to the cloud.
The log entries are stored in RAM and therefore have an unlimited write endurance but will also be lost if power to the device is not maintained.
Two forms are supported:
Short form β a message plus a severity constant:
log("Logic Started", alm.info);
Extended form β title, description, a numeric type for grouping, a
priority, and a numeric value attached to the message. This structured form is
particularly useful when the device reports to a Grafana / InfluxDB cloud
backend, where the fields become queryable:
log(
"Title",
"Description / Message",
3, // type, 0β255 β used for grouping alarm/message types
2, // priority, 0β255 β user definable, or use alm.ok / alm.info / alm.warn / alm.crit
23.43 // alarm value β numeric value attached to the message
);
Arguments
Name
Type
Required
Default
Description
title
string
yes
β
Title of the log entry (or the whole message in short form).
description
string
optional
β
Longer description / message body.
type
number
optional
0
Message type, 0β255. Used for grouping alarm/message types.
priority
number | alm constant
yes
β
Priority 0β255 β user definable, or one of alm.ok, alm.info, alm.warn, alm.crit.
value
number
optional
NAN
Numeric value attached to the entry (e.g. the measurement that triggered it).
Example
// Short form
log("Logic Started", alm.info);
// Extended form
log("Pump Fault", "Discharge pressure out of range", 3, alm.warn, 23.43);
Sets a device environment / configuration variable at runtime.
Adjusts device configuration from within a script. Known keys include:
Key
Value
Effect
"led"
"green", "red", β¦
Sets the front-panel status LED color.
"log.clear"
(none)
Clears the system log.
"sys.utc_offset"
number (hours)
Sets the clock's UTC offset, e.g. -8.
"sys.meas_interval"
seconds
Measurement interval.
"sys.sync_interval"
seconds
Server upload / sync interval.
"enc.key"
string
End-to-end encryption passphrase used for communication when encryption is activated. If not set, the device password is used.
A practical use is adapting the measurement interval to communication outages
β see the Variable Measurement Interval example, which stretches the
interval as the measurement queue (__status().mqueue) fills.
Arguments
Name
Type
Required
Default
Description
key
string
yes
β
Environment variable name (see table above).
value
string | number
optional
β
New value. Some keys, like "log.clear", take no value.
Example
setEnv("led", "green"); // status LED
setEnv("sys.utc_offset", -8); // Pacific time
setEnv("sys.meas_interval", 60); // measure every minute
Returns the device uptime in seconds; the result carries a .sAgo() method for measuring elapsed time.
Returns seconds since boot as a floating-point number. Useful for measuring
elapsed time and for timestamping when a wall-clock time is not required (see
datetime() for calendar time).
The returned timestamp also carries a .sAgo() method giving the number
of seconds elapsed since it was taken β convenient for run-time counters and
for alarm conditions with alm.e():
let startTime = now();
// ... later ...
print("Run Time:", startTime.sAgo()); // seconds since startTime
Returns
timestamp
Uptime in seconds. Also provides .sAgo() β seconds elapsed since this timestamp was taken.
Example
let uptime = now();
print('Uptime Seconds: ',uptime);
Returns the current date and time as an object, including the unix epoch.
Returns the device real-time clock as an object. valid indicates whether the
clock has been set. Set the timezone with setEnv("sys.utc_offset", hours).
Converts numbers and objects to strings for logging and transmission.
Serialises a value to a string. Objects are converted to their JSON
representation β this is the standard way to prepare a payload for http.req,
mqtt.publish, udp.send or sd.append, and to make objects printable.