Rule Script
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.
| Name | Status | Trigger | Protocol | Stats | Actions |
|---|---|---|---|---|---|
| CoAP Receive Convert | Disabled | Data Receive | CoAP | Triggers: 0 | Edit / Enable / Delete |
| HTTP Report: Custom Format | Disabled | Data Report | HTTP | Triggers: 0 | Edit / Enable / Delete |
| MQTT Receive: Object to Array | Disabled | Data Receive | MQTT | Triggers: 0 | Edit / Enable / Delete |
| MQTT Report: Array to Object | Disabled | Data Report | MQTT | Triggers: 0 | Edit / Enable / Delete |
| ModbusRTU Receive Convert | Disabled | Data Receive | Modbus RTU | Triggers: 0 | Edit / Enable / Delete |
| TCP Report: Compact Format | Disabled | Data Report | TCP | Triggers: 0 | Edit / Enable / Delete |
| ModbusTCP Receive Convert | Disabled | Data Receive | Modbus TCP | Triggers: 0 | Edit / 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.
Version Availability
| Edition | Support | Notes |
|---|---|---|
| Lite | ❌ | Script engine not enabled |
| Standard | ✅ | Supports Command Script and RuleScript template conversion |
| Full | ✅ | Full support |
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:
{ "type": "script", "params": { "code": "your script code here" } }Parameters
Built-in Functions
| Function | Description | Example |
|---|---|---|
| gpioWrite(id, val) | GPIO write | gpioWrite('led_d1', 0) |
| gpioRead(id) | GPIO read | var v = gpioRead('key1') |
| pwmWrite(id, duty) | PWM write | pwmWrite('pwm_led', 128) |
| servoWrite(id, angle) | Servo control | servoWrite('servo_01', 90) |
| analogRead(id) | ADC read | var v = analogRead('adc_01') |
| getVar(name, default) | Read global variable | var x = getVar('count', 0) |
| setVar(name, value) | Write global variable | setVar('count', x+1) |
| delay(ms) | Delay (milliseconds) | delay(100) |
| log(msg) | Output log | log('temp:' + temp) |
Built-in Variables
| Variable | Description |
|---|---|
| event.data | Data carried by trigger event |
| event.type | Trigger event type |
| event.source | Trigger event source |
Examples
Conditional Control
var temp = getVar('temperature', 25);
if (temp > 35) {
gpioWrite('fan_relay', 1);
gpioWrite('led_d1', 0);
} else {
gpioWrite('fan_relay', 0);
}Loop Fade
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
| Issue | Possible Cause | Solution |
|---|---|---|
| Script not executing | Syntax error | Check error messages in logs |
| Variables lost | Device restart | Global variables stored in memory only, reset to 0 after restart |
| Execution timeout | Script too long / infinite loop | Limit script complexity, avoid loops |
| Function undefined | Spelling error | Check function name case sensitivity |
