Logo Online Help www.anyviz.io

Scripts

For local calculations or simple automation tasks, scripts can be added. These scripts are programmed in the Lua programming language and can access all tags.

Lua Quick Start

Lua is a lightweight, easy-to-learn scripting language.

The language is minimalist but very flexible and is ideal for automation tasks.

Further documentation

-- A simple function that adds two numbers function add(a, b) return a + b end result = add(5, 7) print("The result is: " .. result)

Accessing Variables

With tags, you can read and write all variables of all connections. Access is via the connection name followed by the symbolic name (e.g. tags["ModbusConnection"]["Temperature"]). For some devices, local IOs can be accessed. These are available under Device.

-- Read temperature via Modbus temp = tags["ModbusConnection"]["Temperature"] -- Turn on fan if temperature exceeds 25°C tags["Device"]["ioman.gpio.dio0"] = temp > 25

Input Variables

If values from other cloud adapters or from AnyViz are needed for the script logic, these can be requested via the requirenumber(), requireboolean(), and requirestring() functions. In AnyViz, a writable symbol is then available. For example, a setpoint can be specified from AnyViz. Alternatively, the value of another tag can serve as an input variable for the script via tag source.

level = requirenumber("Water level tank") -- Get water level via the cloud pumpIsOn = tags["Modbus"]["Pump"] -- Get current pump status if pumpIsOn then -- Turn off pump when 400cm is reached tags["Modbus"]["Pump"] = level < 400 else -- Turn on pump when below 300cm tags["Modbus"]["Pump"] = level < 300 end

Return Values

A script can return a value or alternatively a table. All return values are available as symbols in AnyViz. This allows, for example, local calculations of one or more tags.

-- Input values flow = tags["OPC-UA"]["volume flow"] -- m³/h temp_supply = tags["OPC-UA"]["temp 1"] -- Supply temperature in °C temp_return = tags["OPC-UA"]["temp 2"] -- Return temperature in °C -- Calculation delta_T = temp_supply - temp_return heat_kW = flow * 1.16 * delta_T -- Return both values as a table return { TemperatureDifference = delta_T, Power = heat_kW }

nil Check

There are several reasons why the value of a tag may not be available:

In these cases, nil is returned. Script execution continues. It is recommended to always check tag values for nil before further processing. This helps avoid errors due to missing or not yet available values.

temp = tags["ModbusConnection"]["Temperature"] if temp == nil then print("Value not yet available --> no further processing") return end

Using Lua Functions

Lua offers some useful functions, which can be viewed via the editor's IntelliSense function (CTRL+Space). For more complex calculations, mathematical functions are available via the math table. The os table provides functions for reading date and time.

-- Get current hour using os.date() hour = tonumber(os.date("%H")) -- Hour (00-23) -- Turn on light after 19:00 until 06:00 tags["Siemens TCP"]["Light"] = hour >= 19 or hour < 6

For security reasons, not all Lua functions are available. For example, file operations are not allowed, and functions such as os.execute() or os.exit() have been removed.