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
Configuration Example With Default and Saved Values (Modbus TCP, IEC104)
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 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
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
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
publish(signals.result, command.value) --in this row command value is being published to result signals
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.
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: