Tags: 
User library

AN17 shows a Lua script that can, at the given day and time, set the outputs of the NETIO device to the desired states or perform other actions with them (short on / short off).

 

 

Do you have any questions?

Supported devices: NETIO 4All, NETIO PowerPDU 4C, NETIO 4

 

The AN17 Lua script performs specified actions with the outputs of the NETIO device at the specified time. This can be used to switch outputs on or off at a specified time (one-time action). In the first section of the Lua script, the date and time for performing the desired action with the chosen outputs is specified in a variable. The script can perform 6 different actions with the outputs:

  • 0 - output switched off
  • 1 - output switched on
  • 2 - output switched off for a short time (restart)
  • 3 - output switched on for a short time
  • 4 - output switched to the other state (toggle)
  • 5 - no change in the output state

 

The date format requires the day and the month. The actions are repeated every year.

For the calendar to work, time and date must be set (and synchronized).

 

Creating the rule

To create and run the Lua script, do the following:

1) In the Actions section of NETIO 4 web administration, click Create Rule to add a rule


 

2) Fill in the following parameters:

  • Enabled: checked
  • Name: Calendar (user-defined)
  • Description: Setting outputs based on calendar (user-defined)
  • Trigger: System started up
  • Schedule: Always

3) Copy the following code and paste it into the field for the Lua script:

------------NETIO AN17------------
------------Section 1------------
-- Format: dd/mm,hh:mm,aaaa
local calendar = {
"06/08,07:30,1111",
"01/XX,13:35,2222",
"31/12,23:59,0055"}

local shortTimeMs = 2000
------------End of Section 1------------

local activated = {}
for i=1,#calendar do
  activated[i] = 0
end

function checkFormat()
  for i=1,#calendar do
	local cal = calendar[i]
	local timeCal1 = tonumber(cal:sub(7,8))
	local timeCal2 = tonumber(cal:sub(10,11))
	local dayCal = tonumber(cal:sub(1,2))
	local monthCal = cal:sub(4,5)
	local actionsCal = cal:sub(13,16)
    
	if (timeCal1==nil) or (timeCal1>23) or (timeCal1<0) then
  	logf("Time in state %d is invalid",i)
  	return false
	end
	if (timeCal2==nil) or (timeCal2>59) or (timeCal2<0) then
  	logf("Time in state %d is invalid",i)
  	return false
	end
	if (dayCal==nil) or (dayCal>31) or (dayCal<0) then
  	logf("Day in state %d is invalid",i)
  	return false
	end
	if (monthCal~="XX") and (monthCal~="xx") then
  	monthCal = tonumber(monthCal)
  	if (monthCal==nil) or (monthCal>12) or (monthCal<0) then
    	logf("Month in state %d is invalid",i)
    	return false
  	end
	end
	for j=1,4 do
  	action = tonumber(cal:sub(12+j,12+j))
  	if (action==nil) or (action>5) or (action<0) then
    	logf("Action %d in state %d is invalid",j,i)
    	return false
  	end
	end
  end
  return true
end


function checkDate()
  local stringTime = os.date("%X")
  local time = (3600*tonumber(stringTime:sub(1,2)) + 60*tonumber(stringTime:sub(4,5)) + tonumber(stringTime:sub(7,8)))
 
  local date = os.date("%x")
local day = date:sub(4,5)
  local month = date:sub(1,2)
 
  for i=1,#calendar do
	local cal = calendar[i]
	local timeCal = 3600*tonumber(cal:sub(7,8)) + 60*tonumber(cal:sub(10,11))
	local dayCal = cal:sub(1,2)
	local monthCal = cal:sub(4,5)
    
	--Check month
	if (monthCal == month) or (monthCal == "XX") or (monthCal == "xx") then
  	--Check day
  	if (dayCal == day) then
    	--Check time
    	if (time >= timeCal) and (time < timeCal+60) then
      	--Check if it was already activated
      	if activated[i] == 0 then
        	setOutputs(cal:sub(13,16))
        	activated[i] = 1
        	delay(120,function() activated[i] = 0 end)
      	end
    	end
  	end
	end
  end
  delay(5,function() checkDate() end)
end

function setOutputs(state)
  for i=1,4 do
	value = tonumber(state:sub(i,i))
	if value == 0 then -- turn off
  	devices.system.SetOut{output = i, value = false}
	elseif value == 1 then -- turn on
  	devices.system.SetOut{output = i, value = true}
	elseif value == 2 then -- short off
  	devices.system.SetOut{output = i, value = false}
  	milliDelay(shortTimeMs,function() devices.system.SetOut{output=i,value=true} end)
	elseif value == 3 then -- short on
  	devices.system.SetOut{output = i, value = true}
  	milliDelay(shortTimeMs,function() devices.system.SetOut{output=i,value=false} end)   
	elseif value == 4 then -- toggle
  	if devices.system["output" ..i.. "_state"] == 'on' then
    	devices.system.SetOut{output=i,value=false}
  	else
    	devices.system.SetOut{output=i, value=true}
  	end
	elseif value == 5 then
  	-- do nothing
	end
  end
end

if checkFormat() then
  log("Format valid, starting the script")
  checkDate()
else
  log("Format is not valid, shutting down the script")
end


4) To finish creating the rule, click Create Rule at the bottom of the screen.

 

Method of operation

The script periodically checks the current date and time as kept by the device. In the calendar variable, the user has specified the date, time and actions. When the device datetime is equal to any of the specified datetimes, the script performs the requested actions with all outputs.

 

Setting the variables

calendar

  • This variable specifies the datetime and actions to perform with the outputs. It is a table of text strings. Each string specifies a date, a time and the actions. Individual strings are enclosed in double quotes and separated with commas.
  • String format: "dd/mm,hh:mm,aaaa" - day/month,hour:minute,actions
    • day
      • Expressed in double digits from 01 to 31.
    • month
      • Expressed in double digits from 01 to 12, or XX.
      • The value of XX means every month (e.g. to set the output every first day of the month).
    • hour
      • Expressed in double digits from 01 to 24.
      • The 24-hour format must be used.
    • minute
      • Expressed in double digits from 01 to 59.
    • actions
      • Action to perform with each output
      • 0 - output switched off
      • 1 - output switched on
      • 2 - “short off”, output is set to 0, and after a delay specified in the shortTimeMs variable, the output is set to 1
      • 3 - “short on”, output is set to 1, and after a delay specified in the shortTimeMs variable, the output is set to 0
      • 4 - “toggle”, if the output was on, it is turned off, or vice versa
      • 5 - the output is unchanged
  • Examples:
  • Switch on all outputs on August 25th at 6:00pm:
  • "25/08,18:00,1111"
  • Restart outputs 1 and 2 and keep outputs 3 and 4 unchanged every first day of the month at 5:00am:
  • "01/XX,05:00,2255"

 

shortTimeMs

  • Specifies (in milliseconds) for how long is the output on and off for actions 2 and 3 respectively
  • The minimum value is 100 ms.
  • Example for 2-second pulses: shortTimeMs = 2000

 

Starting the script

After configuring all the parameters and saving the script, the NETIO smart sockets device needs to be restarted. After the device is rebooted, the script starts. When the variables are set correctly, the script logs the following line when started: “NETIO AN17: Format valid, starting the script”.

If the settings are incorrect, the script logs: "NETIO AN17: Format is not valid, shutting down the script" as well as the malformed string. In this case, it is necessary to correct the error, save the script and reboot the device again.

 

FAQ

1) Is it possible to specify an action to be performed repeatedly on the 15th day of every month?

Yes, enter “15” as the day and “XX” as the month in the corresponding string in the calendar variable.

 

2) Is it possible to repeat an action every Sunday?

No, not with this script. Use the script described in AN07 – Periodical scheduler for many actions based on text file - Lua script.

 

Supported FW versions:

3.3.1 and higher

Ask for a price or technical parameters

For device testing use name/password demo/demo