Your First Script
This walkthrough guides you through writing a simple automation script that reads a device, logs data, and reacts to changes.
Prerequisites
- Muxit server running (
node start.js server) - At least one connector configured (the built-in TestDevice works perfectly)
Step 1: Create the Script File
In the Explorer sidebar, click New Script or create workspace/scripts/hello.js manually.
log.info("Hello from Muxit!");Click Run in the editor toolbar. You should see the message in the Output panel.
Step 2: Read a Device Property
The TestDevice connector is always available. Let's read from it:
const test = connector("test-device");
const voltage = test.voltage;
log.info(`Voltage: ${voltage}V`);connector("name") returns a proxy object. Access properties directly (no parentheses). Use function calls for actions.
Step 3: Write to a Device
Use assignment syntax to set a property:
const test = connector("test-device");
test.voltage = 12;
log.info(`Set voltage to 12V`);
const readback = test.voltage;
log.info(`Readback: ${readback}V`);Step 4: Add a Loop
Most useful scripts run continuously. Use script.running to exit the loop when the user presses Stop:
const test = connector("test-device");
while (script.running) {
const v = test.voltage;
const i = test.current;
log.info(`V=${v}V, I=${i}A, P=${(v * i).toFixed(3)}W`);
delay(1000);
}Click Stop in the editor toolbar to end the loop. Stop is a hard-kill — it interrupts V8 mid-instruction, so any code you write after the loop (including finally blocks) is not guaranteed to run. Put setup above the loop and keep each iteration safe to abort.
Step 5: Emit Events
Other scripts and dashboard widgets can react to events you emit:
const test = connector("test-device");
while (script.running) {
const v = test.voltage;
const i = test.current;
emit("reading", { voltage: v, current: i, power: v * i });
log.info(`Emitted reading: V=${v}, I=${i}`);
delay(1000);
}Step 6: Push Data to a Dashboard
Create a virtual store connector to bridge your script to dashboard widgets. First, create workspace/connectors/my-store.js:
export default {
driver: "GUI",
config: {
widgets: [
{ name: "power", type: "gauge", label: "Power", min: 0, max: 100, unit: "W" },
{ name: "status", type: "text", label: "Status", value: "Idle" },
],
},
};Then update your script to write to the store:
const test = connector("test-device");
const store = connector("my-store");
store.status = "Running";
while (script.running) {
const v = test.voltage;
const i = test.current;
store.power = v * i;
delay(1000);
}(Don't try to set store.status = "Stopped" after the loop — Stop is a hard-kill and that line won't run. If you need an idle indicator, have the dashboard read it from script.running or reset it when a script starts.)
Create a dashboard with a gauge widget bound to my-store.power to see it update live.
Step 7: Add Error Handling
Wrap device calls in try/catch for robustness:
const test = connector("test-device");
while (script.running) {
try {
const v = test.voltage;
log.info(`Voltage: ${v}V`);
} catch (err) {
log.error(`Failed to read: ${err.message}`);
}
delay(1000);
}Complete Example
Here's a polished version combining everything:
const test = connector("test-device");
// Setup — put the device in a safe state up front, since Stop won't let
// you clean up after the loop.
log.info("Starting voltage monitor");
test.voltage = 5;
// Main loop — Stop terminates mid-iteration.
while (script.running) {
try {
const v = test.measured_voltage;
const i = test.measured_current;
const power = v * i;
log.info(`V=${v.toFixed(2)}V I=${i.toFixed(3)}A P=${power.toFixed(3)}W`);
emit("reading", { voltage: v, current: i, power });
if (power > 50) {
log.warn("Power exceeding 50W!");
}
} catch (err) {
log.error(`Read error: ${err.message}`);
}
delay(500);
}Next Steps
- See the full Script API Reference for all available globals
- Learn about cross-script communication with
emit()andon() - Build a dashboard to visualize your script's output