Execute Lua Script
The Execute Lua Script action executes a custom Lua script defined in the action. The Lua script contains scripting code that normally executes inside a Lua function or it contains a mix of scripting code and functions themselves. To execute a Lua function defined in a file that has been uploaded to the staging directory, see Execute Lua Function From File.
Parameter descriptions
Parameter | Description |
---|---|
Lua
|
The custom Lua script is entered
directly in this parameter. This can
contain lines of Lua scripting code or
entire Lua functions. |
Input Variables | Variables that will be passed into
the Lua script when execution of the
script begins. When an input variable
is added using the
Configure... Variables
window, it is also added to the Input
tab. |
Output
Variables
|
Variables that will be returned by the Lua script when execution of the script ends. When an output variable is added using the Configure... Variables window, it is also added to the Output tab. |
Input tab
Parameter | Description |
---|---|
Input
Variables
|
Input variables will appear and can be mapped to variables when Input Variables parameters are added using the Configure... Variables window. |
Output tab
Parameter | Description |
---|---|
Output
Variables
|
Output variables will appear and can be mapped to variables when Output Variables parameters are added using the Configure... Variables window. |
ReturnMessage
|
A return message set by the Lua script. The script can return a numeric code or string. |
ErrorMessage
|
An error message set by the Lua script. |
For additional information on the runtime's support of Lua, see Extending the system using Lua scripting. For a usage example of the Execute Lua Function From File action, see Executing a Lua function from a Lua script file.
function logsimple() dw.log.debug("INFO","Log Message") end logsimple(); dw.log.debug("INFO","Log Message2");
The LUA Script is defined to convert a Hexadecimal value to a Decimal value and write it to a device output variable. To use the sample, do the following:
- Download the project Project_Lua.dwx
- Open the Workbench. To learn about workbench, see Workbench
- Expand the node to which you want to import the downloaded project
- Right click on the Projects icon and click Import
Import File Location window opens - Select the downloaded file (Project_Lua.dwx)
- Click Select
Import window appears and to view the dependencies click the expand button. - Click Import
- Right click on the imported project and click Start
- Click on the Imported Project and you will view the trigger (converHexDec)
- Right click and select Start to start the trigger
- Right click and select Fire Trigger
The trigger succeeds and you will see the success count increase.
- Double click on the trigger(converHexDec) to view the trigger definition
The trigger window appears - Double click on the Execute Lua Script Action
You will see the following- Lua script
- Input Hex String to get converted using the Lua script
- Click output to see the device output variable to which the output is written
- To view the result, click the Device, go to the Variables tab and click on the output variable to see the output