Lua runner examples
- Configuration Example With Default and Saved Values (Modbus TCP, IEC104)
- Signal Delay with Lua
- Mathematical Operations With Lua (FW version 1.7)
- Cronjob example
- Execution_signal example
Configuration Example With Default and Saved Values (Modbus TCP, IEC104)
Excel configuration
In order to control signals with Lua script an excel configuration for WCC Lite is needed. In this case three devices are required: one for Modbus TCP Master, one for IEC104-slave and one for Lua runner. Configuration example:
As it is seen from example values such as min_value and max_value can be added to determine limits of a signal. This way command signal will only return results which are within this range. Otherwise command value will have negative cot with invalid, non topical or overflow attributes and new value will not be sent to result signals. As configured, until command value is sent default value will be represented for Lua command signal. For saved values to be represented a Lua script is needed.
Lua script
Lua script example for this configuration is shown below:
local saved = get(signals.result) --getting result signal which is equated to new variable 'saved'
local command = get(signals.command) --getting command signal which is equated to new variable 'command'
--get() function returns nill if there is no valid value
if not command then --if command is not nill
if saved then --if signal is not nill
publish(signals.result, saved.value) --this value is published to result signals and saved value
end
return 0
end
local time_diff = time_ms() - tonumber(command.time) --compares command time and real time
local is_command = time_diff < 30000 and time_diff > -30000 --if command time differs from
--real time more than 30s it will not be executed
if string.find(command.attributes, "nt") or string.find(command.attributes,
"iv") or string.find(command.attributes, "ov") then
--searching if signal has negative attributes
if is_command then --if command execution time is not exceeding the limits then
command.attributes = "cot=7,cotn" --equates negative cot values to response signal attributes
publish(signals.command, command) -- and publishes value to command signals and value
if saved then --if there is saved value then
publish(signals.result, saved.value) --restores saved value to result signals
end
return 0
end
else
if is_command then
command.attributes = "cot=7"
publish(signals.command, command) --in this cycle command value is being returned as well as
--cot7 and cot10 values in case given signal is command and has no negative attributes
command.attributes = "cot=10"
publish(signals.command, command) --publishes response to the command
save(signals.result, command.value) --command value is being saved to result signal
end
publish(signals.result, command.value) --in this row command value is being published to result signals
end
Uploading configuration and Lua script to WCC Lite
First Excel configuration needs to be uploaded to WCC Lite:
After uploading configuration default value will be shown:
After uploading configuration no errors should appear and all signal should be represented on the web. To upload Lua script go to Script-Runner, select upload script and then start:
Connecting master and slave via Vinci software
Connecting IEC104 slave
In order to connect to WCC Lite via IEC104 protocol, select Master(Client) mode on Vinci:
Check Settings tab to match excel configuration:
Specify IP address which should match the one in Excel configuration.
Connecting ModbusTCP master:
To connect ModbusTCP Master, select Slave (Server) mode on Vinci:
Check address to match id in Excel configuration:
Match the IP address given in Excel configuration as well.
Executing commands
Start both master and slave simulations on Vinci. Check if both protocols are connected to WCC Lite on the web tab Protocol Connections:
To execute commands, open Vinci program with IEC104 master running. Here, go to System tab and fill in required fields such as IOA and select data type indicated in Excel configuration. First, try sending value that is outside the set range:
After selecting execute this value will not be showed on the web and positive cot6 and negative cot7 values will be seen on Vinci IEC104 simulation window. Positive cot6 indicates command activation and negative cot7 means that command activation confirmation was denied. The command signal value will not be represented as result signals, because it is determined in the script, that signals with negative attributes will not be published.
Now specify value which will be sent as a result and is within the given range.
Positive cot7 and cot10 values will be seen on the Vinci IEC104 simulation window:
This value will also be represented on Modbus TCP master Vinci simulation window:
To show what happens if the value is not within determined range after the correct value has been sent before, try executing command with smaller or larger value specified:
Again, positive cot6 and negative cot7 values is seen on Vinci window.
As seen on WCC Lite web window, command signals have negative attributes and result signals have the same value as before, because it was saved by Lua script. This example shows that Lua runner can be used to save certain values to signal. For example after the restart saved value could be seen on command signal to determine minimum or maximum value, last value or typical value. This solution could be useful for protecting important data even after reboot or connection faults.
Configuration --> Download
Lua script --> Download
Signal Delay with Lua
For delaying the response of command execution, Lua runner could be used as one of the solutions. In this example IEC104 and Modbus TCP are used. IEC104 protocol sends the command to Modbus and Lua signals and the results are represented as two separate signals. To create Excel configuration for WCC Lite in this case device sheet should look like this:
In the fields marked red, for Modbus TCP enter IP address of Wi-Fi connected to computer and for IEC104 enter IP address of WCC Lite. Map the signals as shown below:
IEC104 SCADA will send command which will then go to Lua signal. Lua signal will send the response back to IEC104 SCADA and to Modbus TCP result signal.
For delaying signal response, Lua script could be written in many ways, however the most simple and effective one is to determine a wait time before publishing signal values to result signals. So in this case Lua script will look like this:
local saved = get(signals.result)--getting result signal which is equated to new variable 'saved'
local command = get(signals.command) --getting command signal which is equated to new variable 'command'
if not command then
if saved then
publish(signals.result, saved.value)--this value is published to result signals and saved value
end
return 0
end
if string.find(command.attributes, "nt") or string.find(command.attributes, "iv") or string.find(command.attributes, "ov") then
--searching if signal has negative attributes
if command then
command.attributes = "cot=7,cotn"--equates negative cot values to response signal attributes
publish(signals.command, command)-- and publishes value to command signals and value
if saved then
publish(signals.result, saved.value)--restores saved value to result signals
end
return 0
end
else
if command then
command.attributes = "cot=7"
publish(signals.command, command)--in this cycle command value is being returned as well as
--cot7 and cot10 values in case given signal is command type and has no negative attributes
command.attributes = "cot=10"
publish(signals.command, command) --publishes response to the command
save(signals.result, command.value)--command value is being saved to result signal
end
local sleepTime = 30
sleep(sleepTime) --before publishing values to result signals script waits 30s
publish(signals.result, command.value)--in this row values are being published to result signals
end
After entering values to empty Excel configuration fields, upload the configuration to WCC Lite (it should upload without any errors):
Upload Lua script to script runner and press start. After this, Status should show Running and script process number will appear.
Open Vinci as IEC104 master, enter IP address of WCC Lite and start communication. Then open another Vinci window and connect Modbus TCP master – select Modbus TCP slave in Vinci and enter the same IP address as set in Excel configuration for Modbus device. With both communications running check Protocol connections on WCC Lite web interface, it should show connected. From IEC104 Vinci window go to System tab. Select command determined in the Excel configuration (50), IOA (1) and value (eg. 2).
Execute the command and check Imported signals:
After 30s result signals will now have the same value:
Signal delay could be used as a tool to synchronize signals so all the values are received at the same time. It can also be used to schedule commands or tasks when delay is required. Since Lua is one of the faster programming languages, it is the most effective instrument to be used is such matters.
Configuration --> Download
Lua script --> Download
Mathematical Operations With Lua (FW version 1.7)
Mathematical operations can be applied to Lua signals as in any other protocol. This can be done by configuring WCC Lite according to solution needed. To create an example which would test multiple mathematical operations, Excel configuration and Lua script is required. Device sheet should look similar to this:
Each Lua device is created to send result values to Modbus TCP signals with different mathematical functions applied. This way the same Lua script can be reused and is more optimal since signal alias for each device can stay the same. There are many other solutions but this one allows to observe results more clearly.
In the field "ip" for Modbus TCP master, enter IP address of Wi-Fi connection for computer in use. In the field "host" for IEC104 slave protocol enter IP address of WCC Lite device.
Signals for these devices should be mapped in example to this:
Each Lua device has command and result signals. Command received from IEC104 protocol is sent to Lua command signal and then this signal sends back a response for IEC104 protocol. If the response does not have negative cot attributes, value is then sent to Lua result signal which sends value to Modbus TCP result signal. Mathematical operations are applied to IEC104 protocol signals since it is the one sending the commands.
As mentioned before Lua script for each Lua device is going to be unchanged and should look like this:
local saved = get(signals.result) --getting result signal which is equated to new variable 'saved'
local command = get(signals.command) --getting command signal which is equated to new variable 'command'
--get() function returns nill if there is no valid value
if not command then --if command is not nill
if saved then --if signal is not nill
publish(signals.result, saved.value) --this value is published to result signals and saved value
end
return 0
end
local time_diff = time_ms() - tonumber(command.time) --compares command time and real time
local is_command = time_diff < 30000 and time_diff > -30000 --if command time differs from
--real time more than 30s it will not be executed
if string.find(command.attributes, "nt") or string.find(command.attributes,
"iv") or string.find(command.attributes, "ov") then
--searching if signal has negative attributes
if is_command then --if command execution time is not exceeding the limits then
command.attributes = "cot=7,cotn" --equates negative cot values to response signal attributes
publish(signals.command, command) -- and publishes value to command signals and value
if saved then --if there is saved value then
publish(signals.result, saved.value) --restores saved value to result signals
end
return 0
end
else
if is_command then
command.attributes = "cot=7"
publish(signals.command, command) --in this cycle command value is being returned as well as
--cot7 and cot10 values in case given signal is command and has no negative attributes
command.attributes = "cot=10"
publish(signals.command, command) --publishes response to the command
save(signals.result, command.value) --command value is being saved to result signal
end
publish(signals.result, command.value) --in this row command value is being published to result signals
end
Upload Excel configuration to WCC Lite:
After uploading configuration no errors should appear and all signal should be represented on the web. To upload Lua script go to Script-Runner, select upload script and then start (for each Lua device):
Open Vinci as IEC104 master, enter IP address of WCC Lite and start communication. Then open another Vinci window and connect Modbus TCP master – select Modbus TCP slave in Vinci and enter the same IP address as set in Excel configuration for Modbus device. With both communications running check Protocol connections on WCC Lite web interface, it should show Connected. From IEC104 Vinci window go to System tab. Select command determined in the Excel configuration (50), IOA (different for each signal) and value. After executing command, each signal (IEC104 command, Lua command, Lua result and Modbus result) will have the same value, which now will be with math applied. For example, command with IOA=1 and value 1 is being executed. In Excel configuration for this signal add column has a value of 5, which means that this value is going to be added to the value sent and the result will be 6.
There can be multiple mathematical operations for one signal. For example add, multiply, bit select etc. If that is the case, math will be applied in typical order (eg. first bit select, then multiply, then add). More detailed explanation about mathematical operations in Excel configuration can be found here: Optional parameters for signals
A user can also apply mathematical condition for the signal value. For example minimum or maximum value, threshold, suppression time for specific value etc. Minimum and maximum values can be applied to set the range of the signal, if the value is smaller or larger signal state will show invalid or overflow. Thresholds can be used in many ways. It can be a specific value or a percentage. If the signal value passes set threshold it will be represented on imported signals window. Threshold works by comparing old value with new value and then applying the condition of either representing the value or suppressing it, depending on the value change. Suppression value and suppression time is best used together, because suppression time determines how long the specific value should be suppressed. There could be multiple values set for suppression. In Excel configuration those values should be separated by comma.
Mathematical operations combined with Lua script is useful for may cases. They can be used for filtering data, converting units, applying specific mathematical logic or other solutions.
Configuration --> Download
Lua script --> Download
Cronjob example
Lua device can be configured using cron time expression. This way script execution can be scheduled or executed at certain times. To create such solution, device sheet of Excel configuration should look like this:
As seen in this configuration, execution type for Lua device is date and execution parameter is in cron time expression. In this case script will be executed every 30s starting from a mm:30 or a mm:00 mark. There are a lot of online cron expression parsers or generators to convert this expression to a more understandable form: https://crontab.cronhub.io/.
A Cron expression must have six variables, for instance, the code "0 * * * *" will not suffice because it contains only five variables. To rectify this, add a "0" to the beginning of the code: "0 0 * * * *". Similarly, the code "0 20-23,0-4,11 * * *" which is displayed as correct on the website, to achieve the effect of every hour from 08:00 PM to 11:59 PM, 12:00 AM to 04:59 AM, and 11:00 AM, it must be adjusted as follows: "0 0 20-23,0-4,11 * * *".
To complete Excel configuration fill out the red fields with correct parameters. For "ip" field enter IP address of Wi-Fi connection that is connected to computer. This can be checked by entering command "ipconfig" on terminal window. For the "host" field enter IP address of WCC Lite device.
Signals sheet should look similar to this:
IEC104 SCADA will send command which will then go to Lua signal. Lua signal will send the response back to IEC104 SCADA and to Modbus TCP result signal.
Lua script in this case will be very simple. To show how Lua with cronjob can be used in real life, a calculation for kilowatts per hour has been added:
local kW = tonumber(get_value(signals.command))--function "get_value" will get value from iec104 command
--without any attributes this value is still in string form so funtion "tonumber" will convert it to number
--a new variable is creted which is now equal to command value
value = value or 0 --new variable is created. It has to be equal to itself or to 0 if the script is running
--for the forst time
value = (kW * 30/3600) + value --formula for calculating kilowatts. command value from iec104 is multiplied by time.
--Since the script is being executed every 30s, this time needs to be converted to hours. an old value is being
--added to new value, this way result value that is being published will grow every 30s
publish(signals.result, value)--publishes result to result signals for lua and modbus TCP.
--Since the script is being executed every 30s, those values will be refreshed every 30s as well
To test the functionality of this script upload Excel configuration to WCC Lite (it should upload without any errors):
Upload Lua script to script runner and press start. After this, Status should show Running and script process number will appear:
Open Vinci as IEC104 master, enter IP address of WCC Lite and start communication. Then open another Vinci window and connect Modbus TCP master – select Modbus TCP slave in Vinci and enter the same IP address as set in Excel configuration for Modbus device. With both communications running check Protocol connections on WCC Lite web interface, it should show connected. From IEC104 Vinci window go to System tab. Select command determined in the Excel configuration (50), IOA (1) and value (for example 3600).
Execute the command and check Imported signals:
On a 30s mark result signals will have calculated value:
After another 30s new value will be added to old value and result signals will be updated accordingly:
Cronjob with Lua can be used mainly to schedule tasks. It can also be used as a way to filter, monitor or control the data. As seen in this example, Lua script can help calculate certain parameters which will then can be sorted using cron time expression.
Configuration --> Download
Lua script --> Download
Execution_signal example
The execution_signal
parameter returns information about the specific signal that triggered execution. If there are multiple possible source signals, it identifies the one that actually initiated the execution. In this example, Modbus RTU protocol is used. Configuration of devices:
Execution_signal
should always be "signal" when execution_signal is used. PORT2 is used to connect IOMod 8DI8DO to WCC Lite via RS485. IOMod signals configuration in this case:
In this case IOMod's input signals are used, but also other signals can be used depending on needs. Map the signals as shown below:
First lua signal is executing first input signal, while second lua signal is executing second input signal. Results signal is used to store information about executed signal.
A Lua script can be written in various ways using the execution_signal
parameter. However, in this example, a simple Lua script is used to retrieve the signal_alias
of the executing signal by accessing execution_signal.tag.signal_alias
:
local DI = execution_signal.tag.signal_alias if DI == "DI1_lua" then publish(signals.results, 1) elseif DI == "DI2_lua" then publish(signals.results, 2) end
This is just one example of execution_signal
usage. It can retrieve different information about executing signal using:
execution_signal.tag.device_alias
- retrieves device_aliasexecution_signal.tag.signal_alias
- retrieves signal_aliasexecution_signal.value.value
- retrieves value of executing signal (for example, 1 or 0)execution_signal.value.time
- retrieves system time in milliseconds (UNIX timestamp)execution_signal.value.attributes
- retrieves attributes (for example, iv, nt, sb)
Upload the configuration to WCC Lite (it should upload without any errors):
Upload Lua script to script runner and press start. After this, Status should show Running and script process number will appear.
After activating first input, in the WCC Lite web's imported signals tab, results should display 1:
After activating second input, in the WCC Lite web's imported signals tab, results should display 2:
Configuration --> Download
Lua script --> Download