Skip to main content

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:image-1692192128948.png

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:

image-1692193770966.png

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):

image-1691483399099.png

Upload Lua script to script runner and press start. After this, Status should show Running and script process number will appear:

image-1692256160683.png

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).

image-1692256267495.png

Execute the command and check Imported signals:

image-1692256755579.png

On a 30s mark result signals will have calculated value:

image-1692256964491.png

After another 30s new value will be added to old value and result signals will be updated accordingly:

image-1692257097082.png

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