Extending the system using Lua scripting

Lua is a scripting language that is supported by the system as a means to perform custom functions that would not be possible with standard trigger actions. Using the Lua scripting language, you will be able to:

  • Create Lua functions that can be executed from trigger actions.
  • Create Lua functions in a text file that can be executed from trigger actions.
  • Create a new trigger action that becomes available to triggers in the same manner as the standard actions.

The Lua language is extensively documented through online and printed documentation, user groups, and forums where participants share sample scripts for the most common use cases. The following link provides references to many online and printed books regarding Lua:
http://www.lua.org/docs.html

Other information and help resources

  • Lua version 5.1 is used
  • For the official Lua documentation, refer to the Lua 5.1 Reference Manual - http://www.lua.org/manual/5.1
  • To get started using Lua basics programming, go to - http://www.lua.org/pil/#online
    This online guide supports Lua 5.0; however, it is a good guide for basic Lua programming.

Guidance when using Lua to create custom functions

The following guidance is for the deviceWISE application developer who needs to use the Lua scripting language to create custom functions that are not available as standard trigger actions:

  • The Lua scripting language should be used when standard trigger actions do not provide the required function.
  • Your Lua script runs as an extension of the deviceWISE runtime process. Any errors in your Lua script that cause a "crash" will crash the deviceWISE runtime process.
  • As a programming language, use of Lua requires you to understand the environment your application will be executing in and make appropriate accommodations.
    This includes any differences in operating system features (APIs, parameters, behavior) that are accessed by your Lua script.
  • A good practice is to develop, debug, and test your Lua script using Eclipse. Eclipse has a plugin supporting LUA 5.1 and 5.2, allowing you to conveniently debug your functionality. If it does not work cleanly in Eclipse, there is no need to move it into your deviceWISE application.
  • Although Lua has a great reputation when it comes to performance, please consider the Lua performance tips available at https://www.lua.org/gems/sample.pdf.

A simple Lua example

function simple()
    local a = 5
    local b = "hello world"
    dw.log.exception("INFO","Hello Lua")
    dw.log.exception("INFO",b)
    return 0
end
Related topics

Execute Lua Function From File

Execute Lua Script