Cronjob example
Lua device can be configured using a cron time expression. This way, script execution can be scheduled or executed at certain times. To create such a solution, the device sheet of the Excel configuration should look like this:
As seen in this configuration, the execution type for the Lua device is date, and the execution parameter is in a cron time expression. In this case script will be executed every 30 seconds 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 the Excel configuration, fill out the red fields with the correct parameters. For the "ip" field, enter the IP address of the Wi-Fi connection that is connected to the computer. This can be checked by entering the command "ipconfig" in a terminal window. For the "host" field, enter the IP address of the WCC Lite device.
The signals sheet should look similar to this:
IEC104 SCADA will send a command, which will then go to the Lua signal. Lua signal will send the response back to the IEC104 SCADA and Modbus TCP result signal.
The 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 the Excel configuration to WCC Lite (it should upload without any errors):
Upload the Lua script to the script runner and press start. After this, Status should show Running, and the script process number will appear:
Open Vinci as an IEC104 master, enter the 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 the Excel configuration for the Modbus device. With both communications running, check Protocol connections on the WCC Lite web interface, it should show connected. From the IEC104 Vinci window, go to the 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 a calculated value:
After another 30 seconds, a new value will be added to the old value, and the 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, a Lua script can help calculate certain parameters, which then can be sorted using a cron time expression.
Configuration --> Download
Lua script --> Download