# 21 Lua script runner ### Introduction Lua is a powerful, efficient, lightweight scripting language. It has been used in many industrial applications with an emphasis on embedded systems. Lua has a deserved reputation for performance. To claim to be "as fast as Lua" is an aspiration of other scripting languages. Several benchmarks show Lua as the fastest language in the realm of interpreted scripting languages. Lua is fast not only in fine-tuned benchmark programs, but in real life too. Substantial fractions of large applications have been written in Lua. In WCC Lite system Lua is used for extending the functionality of excel configuration adding an interface to the existing signal-linking engine. Provided functions enable to recreate PLC functionality and modify any value with ease. ### Overview ##### Execution types: Lua runner provides 3 execution modes: interval, date, signal, which can be specified in **execution\_type**. **Interval**: executes provided script based on provided time interval in **execution\_parameter**. It uses milliseconds, meaning if 500 value is provided, then the script is executed every 500 milliseconds. **Date:** schedules a script execution based on provided cron expression in **execution\_parameter**. The format consists of 6-7 fields separated by space.
Name | Required | Allowed Values | Allowed Special Characters |
---|---|---|---|
Seconds | Y | 0-59 | , - \* / |
Minutes | Y | 0-59 | , - \* / |
Hours | Y | 0-23 | , - \* / |
Day of month | Y | 1-31 | , - \* / |
Month | Y | 0-11 or JAN-DEC | , - \* / |
Day of week | Y | 1-7 or SUN-SAT | , - \* / |
Year | N | empty or 1970-2099 | , - \* / |
These functions are equivalent to REDIS functions.
Function **save** saves the specified value to flash memory for use after reboot. The same value sub-values apply to execution. ```Lua save(signals.tag1, 50) -- this will save a value to tag1These functions will require significant cpu resources even if the message log level is higher than default and no message is printed.
##### Web interface The web interface is used to see what scripts are running, if there is a script provided, to stop/start a script. After configuring a device with lua runner as protocol, the script runner protocol hub tab will be populated with devices that where configured: [](https://wiki.elseta.com/uploads/images/gallery/2022-10/image-1665406373895.png) Then by pressing the Upload Script button, a script will be available to be selected (the name of the script will be changed to match device\_alias). When uploaded the script will not be started automatically, pressing start will be necessary. [](https://wiki.elseta.com/uploads/images/gallery/2022-10/image-1665408922682.png) After pressing start, the script will be started, if there are errors it will try to start, but after a few attempts, it will stop. [](https://wiki.elseta.com/uploads/images/gallery/2022-10/image-1665409121771.png) Clear all saved values button is used to clear the memory of saved values. Having a lot of values being saved is not healthy for the SD card and faults can happen. Also, script runner process initialization is slowed down when a lot of saved values are used. ### Configuration #### Device configuration parameters:**Parameter** | **Type** | **Description** | **Required | **Default value** (when not specified) | **Range** | |
Min | Max | |||||
name | string | User-friendly name for a device | Yes | |||
device\_alias | string | Alphanumeric string to identify a device | Yes | |||
description | string | Description of a device | No | |||
enable | boolean | Enabling/disabling of a device | No | 1 | 0 | 1 |
protocol | string | Protocol to be used | Yes | lua runner | ||
execution\_type | string | Execution type to be used | Yes | interval, date, signal | ||
execution\_parameter | int, string | Parameters for execution | Yes | interval time in ms, date in cron format, additional signal | ||
queue\_max | int | Maximum execution queue jobs | No | 3 | 0 to disable queue | |
error\_limit | int | Error limit before stoping | No | 3 | 0 to disable | |
keep\_alive\_time\_ms | int | Time to keep the script alive in ms | No | 600000 | 0 to disable |
**Parameter** | **Type** | **Description** | **Required | **Default value** (when not specified) | **Range** | |
Min | Max | |||||
signal\_name | string | User-friendly name for a signal | Yes | |||
device\_alias | string | Alphanumeric string to identify a device | Yes | Must match device\_alias in the device sheet | ||
signal\_alias | string | Unique alphanumeric name of the signal to be Yes used | Yes | |||
iterator | string | Lua table name to which signal is added | No | signals | ||
default\_value | string | Default value for a signal | No | |||
execute | int | Enable signal update trigger to execute script (only available for signal execution mode) | No | 0 | 0 | 1 |
Math (math\_expression, add, multiply, ) for source signals will not work for lua-runner device
### Debugging the script runner service If configuration for script runner is set up, the process will start automatically. If configuration/script is missing or contains errors, process will not start. It is done intentionally decrease unnecessary memory usage. Script runner runs a service called **lua-runner**. If the script doesn't start or does not work correctly, a user can launch a debug session from command line interface and find out what problem is causing it to not work. To launch a debugging session, a user should stop the script from web interface and run **lua-runner** command with respective flags and configuration as in the table given below. Procedure for lua-runner service debugging: - **Step 1**: Script must be stopped through web interface. - **Step 2**: After script is stopped it must be started with the preferred configuration file (JSON files found in /etc/lua-runner, and the name corresponds to device\_alias) and a debug level 7: **lua-runner -c /etc/lua-runner/device\_alias.json -d7 -e.** - **Step 3**: Once the problem is diagnosed normal operations can be resumed by starting the script through web interface.