πŸ“¦ Archived documentation: r1 (committed 2026-07-24) View current documentation β†’

Global Functions

Core functions available everywhere in a script: lifecycle, timing, logging and device status.

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()

run() β†’ boolean

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

waitMS()

waitMS(milliseconds)

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

NameTypeRequiredDefaultDescription
milliseconds number yes β€” How long to pause, in milliseconds.

Example

while(run()){
  // take measurements ...
  waitMS(5000); // repeat every 5 seconds
}

print()

print(value, value2, ...)

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

NameTypeRequiredDefaultDescription
value… any yes β€” One or more values to log. Objects should be wrapped in string() to serialise them.

Example

let speed = scope.getPeak(50, 70);
print("Peak: ", string(speed));
// Peak: {"bin":322,"freq":59.977173,"amp":0.037552}
print("RPM:", (speed.freq * 120 / 10).toFixed(), "rpm");

log()

log(title, [description], [type], [priority], [value])

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

NameTypeRequiredDefaultDescription
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);

setEnv()

setEnv(key, value)

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

NameTypeRequiredDefaultDescription
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

now()

now() β†’ timestamp

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);

datetime()

datetime() β†’ object

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).

Available properties: valid, uptime, epoch, year, month, monthDay, weekDay, hour, minute, second.

Returns

object e.g. {"valid":true,"uptime":1437.05,"epoch":1589247811,"year":2020,"month":5,"monthDay":12,"weekDay":2,"hour":1,"minute":43,"second":31}

Example

print(string(datetime()));
let ts = datetime().epoch;           // unix timestamp
let year = datetime().year;          // e.g. 2020

string()

string(value) β†’ string

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.

The inverse for JSON text is JSON.parse().

Arguments

NameTypeRequiredDefaultDescription
value any yes β€” Number, object or other value to serialise.

Returns

string String / JSON representation of the value.

Example

let payload = { batteryVoltage: 14.3, uptime: now() };
http.req("http://myserver.com/url", string(payload), 1000, false);

__status()

__status() β†’ object

Returns device status: supply voltage, identity, firmware versions and measurement queue level.

Returns a snapshot of device status. Fields include:

Field Meaning
vcc Supply voltage (V).
name, desc, location, contact Device identity strings from configuration.
uid Unique device ID.
hw_v, fw_v, cfg_v Hardware, firmware and configuration versions.
mqueue Measurement queue fill level (percent). Useful for adapting measurement intervals during communication outages.

Returns

object e.g. {"vcc":23.81,"name":"Flexs Vibration Monitor","uid":2585356324,"hw_v":9001,"fw_v":10,"cfg_v":164,"mqueue":0}

Example

print(string(__status())); //  {"vcc":23.814098,"contact":"","location":"","desc":"","name":"Flexs Vibration Monitor","uid":2585356324,"hw_v":9001,"fw_v":10,"cfg_v":164,"mqueue":0}