Skip to content

Rule Script ​

Overview ​

Rule Script is the script action type in FastBee peripheral execution, supporting simplified JavaScript syntax for custom logic. Suitable for complex condition checks, data calculations, and multi-step control flows.

The initial configuration contains two disabled MQTT examples:

NameStatusTriggerProtocolStatsActions
MQTT Receive: Object to ArrayDisabledData ReceiveMQTTTriggers: 0Edit / Enable / Delete
MQTT Report: Array to ObjectDisabledData ReportMQTTTriggers: 0Edit / Enable / Delete

Scripts are ideal for bundling multiple GPIO, PWM, delay, and variable read/write operations into a single action. Before deployment, save and verify with the rule disabled first, then test manually.

When troubleshooting scripts, identify which stage the failure occurs: save stage, trigger stage, variable replacement stage, peripheral action stage, or resource protection stage. Each stage has different logs and handling methods.

Runtime Availability ​

FirmwareSupportNotes
Release environmentsCompiledCommand Script and RuleScript template conversion are in the unified baseline

The script engine still requires ruleScript and commandScript to be enabled and accepted by FeatureBudget. When inactive, the Web menu is hidden and the API returns the documented unavailable state.

Note: This document focuses on Command Script (GPIO/PWM/delay command sequences) in peripheral execution. For RuleScript (${key} template format conversion during MQTT/Modbus data report/receive), see Protocol Configuration.

Usage ​

Use "type": "script" action in peripheral execution rule actions:

json
{ "type": "script", "params": { "code": "your script code here" } }

Parameters ​

Built-in Functions ​

FunctionDescriptionExample
gpioWrite(id, val)GPIO writegpioWrite('led_d1', 0)
gpioRead(id)GPIO readvar v = gpioRead('key1')
pwmWrite(id, duty)PWM writepwmWrite('pwm_led', 128)
servoWrite(id, angle)Servo controlservoWrite('servo_01', 90)
analogRead(id)ADC readvar v = analogRead('adc_01')
getVar(name, default)Read global variablevar x = getVar('count', 0)
setVar(name, value)Write global variablesetVar('count', x+1)
delay(ms)Delay (milliseconds)delay(100)
log(msg)Output loglog('temp:' + temp)

Built-in Variables ​

VariableDescription
event.dataData carried by trigger event
event.typeTrigger event type
event.sourceTrigger event source

Examples ​

Conditional Control ​

javascript
var temp = getVar('temperature', 25);
if (temp > 35) {
  gpioWrite('fan_relay', 1);
  gpioWrite('led_d1', 0);
} else {
  gpioWrite('fan_relay', 0);
}

Loop Fade ​

javascript
var duty = getVar('duty', 0);
var dir = getVar('dir', 1);
duty += dir * 10;
if (duty >= 255) { duty = 255; dir = -1; }
if (duty <= 0) { duty = 0; dir = 1; }
pwmWrite('pwm_led', duty);
setVar('duty', duty);
setVar('dir', dir);

Troubleshooting ​

IssuePossible CauseSolution
Script not executingSyntax errorCheck error messages in logs
Variables lostDevice restartGlobal variables stored in memory only, reset to 0 after restart
Execution timeoutScript too long / infinite loopLimit script complexity, avoid loops
Function undefinedSpelling errorCheck function name case sensitivity

FastBee | fastbee.cn