Skip to main content
Skip table of contents

Lua Script

The "Lua Script" logic element provides you with the option to use scripts generated in Lua in the context of the LOGIKEDITOR.

Contents


Inputs and outputs

Inputs

Outputs

  • OUT 1-...
    A maximum of 20 outputs are possible.


Configuration

image-20250713-184038.png

Inputs

Set up to 20 inputs through which the script can be started and provided with data. You can decide what will happen when a value is received for every input.

  • Don’t start script

  • If script active, don't start

  • If script active, restart now

  • If script active, start later

If the script is to be started, all of the values available at the inputs are determined and made available to the script start. If the input is configured so that the script is to be restarted later, the invocation is prepared with the values which are available at that moment, before then being run at the specified moment in time.

A maximum of five (5) script invocations can be queued for a later start. The sixth invocation would displace the first in the queue and occupy fifth place in the queue.

It is not possible to transfer new input values to a script which has already started.

Outputs

You can write to up to 20 outputs from the script. This occurs as soon as you assign it in the script. The order is retained.


Script

Scripts are authored in the Lua programming language. In addition to the available methods, you can also access the full standard library of Lua and of BAB TECHNOLOGIE.

This documentation does not provide information about the syntax or grammar of the Lua programming language. If you require a general introduction to the use of Lua, please use one of the many help pages which are on offer on the internet. (Example search term: "Lua in 15 minutes")

In the script, the “BT” library is available to you which offers you the following options:

Reading and writing of inputs and outputs

"BT:getInValue()"

Read out the value of a logic element input.

LUA
local in1 = BT:getInValue('in1')

"BT:sendValue()"

Write a value on an output. In general, it is only a good idea to send numbers, boolean and texts. The logic element would try to process other data (e.g. Lua tables) as text.

LUA
local in1 = 1 
local in2 = 2

BT:sendValue('out1', in2 * in1)
BT:sendValue('out2', in1 / in2)

Reading and writing of saved values (properties)

"BT:getProperties()"

Loads a Lua table in which you can save values which can be reused in the case of subsequent invocations

"BT:saveProperties()"

Saves the previously loaded Lua table.

LUA
local p = BT:getProperties() 
p["foo"] = 42 
BT:saveProperties()

It is important to use the loaded Lua table. This is the reference which is saved.

HTTP

"BT:httpGet()"

Carry out simple Http GET queries. The use of https:// is also possible. As second parameter, you can also provide a Lua table with Http headers.

LUA
local authToken ="veryPrivateData" 
local header = {}
header["Client-Id"] = authToken
header["Accept"] = "application/json" 

local getDaten = BT:httpGet(http://api.remote-service.de/data, header)

"BT:httpPost()"

Carry out simple HTTP POST queries. The use of https:// is also possible. As second parameter, provide the data which is to be sent as text or as a Lua table (is converted into JSON). As a third parameter, you can also provide a Lua table with HTTP headers.

LUA
local authToken = "veryPrivateData"
local header = {}
header["Client-Id"] = authToken

local payload = {}
payload["foo"] = 23
payload["bar"] = "foobar"
local postResult = BT:httpPost(http://api.remote-service.de/data, payload, header)

if postResult.status ~= 204 then
	BT:log("Got wrong resultcode:"..postResult.code)
	BT:exit()
end

Both invocations return a Lua table with the following contents:

  • body

  • response

  • code

  • header

  • status

JSON

"BT:encodeJSON()"

A Lua table is transformed into the JSON format.

"BT:decodeJSON()"

A text in the JSON format is transformed into a Lua table.

LUA
local t = {}
t["foo"] = "bar"

local json = BT:encodeJSON(t)
t = BT:decodeJSON(json)

Date and Time

BT:time()

Is identical to os.time().

LUA
-- For the current time
local t = BT:time()

BT:date()

Is identical to os.date().

LUA
local d = BT:date("*t", 906000490)

General

  • "BT:sleep()"
    The script is stopped for a period of x milliseconds

LUA
BT:sleep(500) -- 1/2 second
  • "BT:exit()"
    At this point, the script is ended.

  • "BT:log()"
    The script sends a log input to the EIBPORT. This is written in the log of the YaLE service and can be invoked in the LOGIKEDITOR via System/Download system logs.
    Please note that the merging of the logs after clicking can take a while to occur. Clicking again does not accelerate the process, but slows it down.

LUA
BT:log("All good")
  • "BT:getTrigger()"
    Provides the triggering input.

LUA
local trigger = BT:getTrigger()
if trigger == "in2" then 
-- do things 
end
  • "BT:getUUID()"
    Provides the unique ID of the logic element.

Limitations

It is not possible to integrate additional libraries in this logic element (e.g. via require('bibliothek')). It is only possible for the standard library and the functions which are made available by BAB to be used. This also applies to subsequently loaded text code (e.g. via loadstring('code')).

A script has a maximum duration of 10 seconds. At this point, the script is ended by EIBPORT without query.

Please note that many scripts, or scripts which are arithmetically complex, have a major impact on the performance and speed of the EIBPORT.


Script error

If the script fails during running because of an error, you will receive a push notification on the dashboard.

///

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.