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.

Example 1: LUA Script
function logsimple()
   dw.log.debug("INFO","Log Message")
end
logsimple();
dw.log.debug("INFO","Log Message2");
Example 2: LUA Hexadecimal to Decimal converter Script

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:

  1. Download the project Project_Lua.dwx
  2. Open the Workbench. To learn about workbench, see Workbench
  3. Expand the node to which you want to import the downloaded project
  4. Right click on the Projects icon and click Import

    Import File Location window opens
  5. Select the downloaded file (Project_Lua.dwx)
  6. Click Select
    Import window appears and to view the dependencies click the expand button.
  7. Click Import
  8. Right click on the imported project and click Start
  9. Click on the Imported Project and you will view the trigger (converHexDec)
  10. Right click and select Start to start the trigger
  11. Right click and select Fire Trigger

    The trigger succeeds and you will see the success count increase.
Trigger insight
  1. Double click on the trigger(converHexDec) to view the trigger definition
    The trigger window appears
  2. Double click on the Execute Lua Script Action
    You will see the following
    1. Lua script
    2. Input Hex String to get converted using the Lua script
    3. Click output to see the device output variable to which the output is written
  3. To view the result, click the Device, go to the Variables tab and click on the output variable to see the output