Widget Script Mode
Drive a value widget with a short JavaScript snippet instead of the Builder form.
Overview
Value widgets on the Monitoring Dashboard normally read one telemetry key and show it as-is. Script mode replaces that with a short JavaScript snippet that receives the device's data and returns what the widget displays. Use it when the value needs a calculation, a lookup across several keys, or formatting the Builder cannot express.
Script mode is available on the Numeric Metric, Value, Text, Status, Gauge Indicator and Bar Indicator widgets. It is enabled per Partner by Xyte; contact support to turn it on.
Switching a widget to Script mode
- Open the widget in Models > Monitoring Dashboard and pick Script at the top of the form.
- The editor opens with a script generated from the current Builder settings, so the widget renders the same until you change something.
- Edit the script. The preview on the right re-runs it as you type, against the Sample data below it.
- Click Save.
Reset to template regenerates the script from the Builder settings and discards your edits. Switching back to Builder keeps the script stored but stops running it.
What the script receives
The script body runs as a function with these read-only bindings:
| Binding | Contents |
|---|---|
state | The device's latest reported Telemetry, as an object. |
config | The device's Configuration. |
details | The device's details. |
space | The configuration of the Space the device is in. |
For an offline device, state holds the last known state. A missing key reads as undefined, so guard for it (state.temperature ?? 0).
What the script returns
Return an object. The keys depend on the widget type:
| Widget | Return value |
|---|---|
| Numeric Metric, Value, Text | { title, value } — value is the text to display. |
| Status | { title, value, color } — value is the status text; color is a theme key such as teal.6 or a hex colour. |
| Gauge, Bar Indicator | { title, value, label } — value is the number that drives the arc or bar; label is the text shown. |
title is optional and overrides the widget title. A missing or null value shows N/A. A script that throws shows N/A with an error hint; the message is shown in the editor preview.
Example — a Numeric Metric that shows power in kilowatts with one decimal:
const watts = state.power_w;
if (typeof watts !== 'number') return { value: 'N/A' };
return {
title: 'Power',
value: new Intl.NumberFormat('en-US', { maximumFractionDigits: 1 }).format(watts / 1000) + ' kW',
};Example — a Status widget that combines two keys:
const ok = state.online && state.door_closed;
return {
value: ok ? 'Secure' : state.online ? 'Door open' : 'Offline',
color: ok ? 'teal.6' : 'red.6',
};Sample data
The preview needs data to run against. Expand state, config, details or space under the preview and paste a representative JSON object, for example a device's last Telemetry payload. The sample is remembered per Model in your browser; it is never sent to devices or Customers.
What the script can and cannot do
Scripts run inside a locked-down sandbox in the Customer's browser. The sandbox has no network, storage or DOM access and cannot change anything outside the script. Available globals: Intl, Math, Number, String, JSON, Array, Object. Date and Math.random() are disabled so that a widget always renders the same for the same data.
A script that runs for more than one second is stopped and the widget shows N/A. A script that is stopped twice is disabled until the page is reloaded. Scripts are limited to 32 KB.
Updated 20 days ago
