Digital Loggers

Customize your power controller using simple, powerful Lua scripting.


Advanced Power Control - Lua Scripting

On its own, a power switch isn't very smart.  Add custom functionality using the built-in simple Lua scripting language. It's really simple. No programming experience is required.  Give it a try!

 

Hardware Requirements

This page describes the Lua based scripting language used in DLI products with WiFi.  If you're using a product without the WiFi option, look here for the BASIC scripting reference.

 

Entering Scripts

First, review this website for a quick overview of the Lua language.  Log in as admin and use the scripting feature.  Click the Scripting link on the left.  Review these sample scripts to start:
 
--[[ This is a sample/test set of scripts for the DLI EPCR5 controller.
The scripting implementation has changed, and is no longer compatible
with the older BASIC implementation. The most important changes are:
- Now Lua-based.
- No more line numbers, blocks of code identified by functions.
- Most of ON, OFF, etc. are kept as legacy functions, which can be called like
 e.g  ON(2345), ON("2345") or ON "2345", your choice.
Execution is still based on threads. Now threads are more visible and
manageable. Try starting some and you'll see them appearing in the
list.
]]--
function turn_outlets_on()
ON(1)
ON(2)
ON(3)
ON(4)
ON(5)
ON(6)
ON(7)
ON(8)
end
function turn_outlets_off()
OFF(12345678)
end
-- Hope this looks familiar so far.
-- Indentation is useful but not mandatory:
function toggle_stuff_and_log()
   LOG "One"
   ON(1)
   OFF(1)
   LOG "Two"
   ON(2)
   OFF(2)
   LOG "Done"
end
function do_some_lua_stuff()
    for i=1,8 do
        if i>4 then OFF(i) else ON(i) end
    end
end
function test_display()
    DISPLAY "\1Percent %%\v"
    DISPLAY "\2Backslash \\\v"
    DISPLAY "\1%a\v" --  current Bus A
    DISPLAY "\2%A\v" --  voltage Bus A
    DISPLAY "\1%b\v" -- current Bus B
    DISPLAY "\2%B\v" -- voltage Bus B
    DISPLAY "\1%o\v" -- Outlets state in the form "12456" (ON are displayed)
    DISPLAY "\1%O\v" -- Outlets state in the form "++-+++--"
    DISPLAY "\1%n\v" -- Serial number
    DISPLAY "\2%f\v" -- Firmware version
    DISPLAY "\f"     -- Clear screen, first line intentionally blank
    DISPLAY "\2%d\v" -- System time/date
    DISPLAY "\1%M\v" -- MAC address of the power controller
    DISPLAY "\2%i\v" -- IP address of the power controller
    DISPLAY "\1%m\v" -- IP network mask
    DISPLAY "\2%g\v" -- IP gateway
end
--[[ Some additional scripts that may be of use ]]--
-- Swap the state of outlets 7 and 8
function flip_flop_7_8()
  if(outlet[7].state == on) then
    outlet[7].state = off 
    outlet[8].state = on
  else
    outlet[7].state = on
    outlet[8].state = off
  end
end 
-- Update the time via NTP
  function Update_Time()
     TIME("17.151.16.23")
 end
--Or via NTP name:
 function update_time_too()
    TIME("time.nist.gov")
 end
-- Log some meter readings to the system log
function log_meter_readings()
LOG("Standard Format Log") LOG(string.format("Bus A voltage: %g, bus A current: %g",meter.buses[1].voltage,meter.buses[1].current)) LOG(string.format("Bus B voltage: %g, bus B current: %g",meter.buses[2].voltage,meter.buses[2].current))
  LOG("Optional Format Log")
  if(meter.buses[1].voltage > 95 and meter.buses[1].voltage < 130) then
    log.notice("Bus A voltage: %g, bus A current: %g",meter.buses[1].voltage,meter.buses[1].current)
  else 
    log.warning("Warning! Bus A voltage: %g, bus A current: %g",meter.buses[1].voltage,meter.buses[1].current) 
  end
  if(meter.buses[2].voltage > 100) then 
    log.info("Bus B voltage: %g, bus B current: %g",meter.buses[2].voltage,meter.buses[2].current)
  else
    log.warning("Low Voltage Warning - Bus B voltage: %g, bus B current: %g",meter.buses[2].voltage,meter.buses[2].current) 
  end
 if(meter.buses[2].voltage > 120) then 
    log.notice("Bus B voltage: %g, bus B current: %g",meter.buses[2].voltage,meter.buses[2].current)
  else
     log.warning("Bus B voltage: %g, bus B current: %g",meter.buses[2].voltage,meter.buses[2].current) 
  end
end
-- Here's an example function with loops. wait_until sleeps until a specified time
local function wait_until(conditions)
   repeat
	  local ok=true
	  local date=os.date("*t")
	  for k,v in pairs(conditions) do
	     if type(v)=="function" then 
                ok=v(date[k])
	      else
	        ok=date[k]==v
	      end
	if not ok then
	  break
	  end
	end
	if not ok then
	  delay(1)
	end
	until ok
   end

-- Here we use wait_until to turn outlet 4 off at midnight.
function turn_off_light_at_midnight()
  wait_until({hour=0,min=0})
  OFF(4)
end
  

Have a smart script or unique way to use your switch?  Let us know!

engineering@digital-loggers.com