The First Script
As a first step into the Lua scripting environment, we will create a very simple calculation to be executed by the Node. This will be done in 3 steps. First, we will prepare a location in the object tree to host the objects we need for this example. In the second step we’ll create objects that simulate data, before finally creating the first simple calculation.
Prerequisites
1 Open WebStudio and login with administrative rights.
2 In the System Model, create a new Time Series Data Store and call it 'Lua Example Time Series Data Store'.
| The link for the Time Series Data Store takes you to the Object Guide page for this object. Object guide pages provide step-by-step instructions how to create a new object of a given type, along with then necessary prerequisites and essential configuration options. In this case the Object Guide informs you that the new Data Store needs to be added to the Node’s Data Store Sets. Make sure to also give this Data Store Set a telling name. |
Step 2: Data Generating Items
As a starting point, we will create two items that generate simulated data.
Create a Generic Item, setting the Object Name property to "Saw Tooth".
Select the new Generic Item and open the Object Lens.
Navigate ot the Generation Type compound and change the Function Period to 30000.
In the Archive Options compound, set the Archive Selection to the Lua Example Data Store Set and check the Raw History option.
Create another Generic Item - calling it "Sinus".
In the Generation Type compound, change Function to Sine wave values and the Function Period to 30000.
In the Archive Options step, change the Archive Selection to the Lua Examples Data Store Set and check the Raw History option.
Step 3: Periodical Calculation
In this step, we will create another GenericItem, that executes a script making a simple addition calculation periodically. Right-click the Jump Start folder and select from the context menu. In the Common step of the Create Generic Item wizard, enter "Simple Calc" in the Object Name edit box, and select 5000 ms as Generation Period.
In the Generation Type step, select the Lua Script
Data Generator option, and then click the icon to open the Lua Script Body Editor. Enter the following code
into the script editor:
function calc()
-- get the values of the two simulation items
local sin = syslib.getvalue("/Lua Examples/Lua/Jump Start/Sinus")
local saw = syslib.getvalue("/Lua Examples/Lua/Jump Start/Saw Tooth")
-- return the sum of the values
return sin + saw
end
-- return the result of the calculation
-- it becomes the current value of the calculation
return calc()
You can see that the editor highlights the Lua syntax in different colors (keywords in blue, comments in green, and strings, including
paths, in red). The system extension to the Lua environment is also used in this script.
Here the getvalue function is called to read a value from an object by using the object’s path in the object tree.
Note how the calculation is executed every 5000 milliseconds (as configured during creation of the object), independently of the inputs from the Saw Tooth and Sinus objects that change every 1000 milliseconds (as was configured during their creation). If you add the Saw Tooth, Sinus and Simple Calc objects to a Real Time Grid tab, the different execution times can be observed concurrently, as the respective object values update.