LUA4: Convert data point value
Description
This example shows how to convert values of data points between different units e.g.(from watt to kW). The conversion can be handled with a LUA function. The output value can be written to any selected item (Virtual Items or VAR items). The function reads the incoming values and converts them into any related measurement.
Creating a new LUA function
First, we will create a new LUA function which verifies the item value and sends an e mail if it is out of limit. Within the toolbar of the studio, click on the "Edit Script" button.
Afterwards, you can select the "nxaDefinitions.lua" to open the main LUA file. Paste in the following code at the end of the file:
function converter() local var = nxa.GetValue(nxa.SourceItemID()) local cal = var/1000 --conversion factor e.g. (1000 W will be converted to 1 kW) nxa.SetDestinationValue(cal) end
After having finished the LUA implementation, save LUA function and restart the LUA engine using the buttons within the toolbar of the LUA script editor.
Next, define a server task that is triggered upon changes of the source item "NETx\XIO\BACnet\NetXBACnet\Electricity_usage\Electricity_usage". As destination item use e.g. "NETx\VAR\Real\Item001". Use "SCRIPT" as command and as parameter enter "converter()". This means that the LUA function "converter()" is triggered whenever the source item values changes. Using this way, the incoming value is converted to kW.
After having defined the task, save the definition. Since Task Definitions are live ones, they are reloaded automatically – a server restart is not necessary. Now test the new tasks. For the given example, any value of "NETx\XIO\BACnet\NetXBACnet\Electricity_usage\Electricity_usage" will be converted and written to the "NETx\VAR\Real\Item001".
Article applies to the following products:
- NETx BMS Platform
- NETx Multi Protocol Server
- NETx BMS Server 2.0