Digital Loggers

Customize your power controller using simple, powerful Lua scripting.


Updated 10/07/2020

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!

 

Main Lua scripting page

Turn lights on and off weekdays on a schedule


To start automatically upon power up, set the start script



After entering the scripts (or copy/paste) and saving it, select the script to run and press the Start button to start it.
Start Scripts

-- Start all of my scripts from here.
-- I can start several at once this way.
function start_my_scripts()
	thread.run(lights_schedule, "Turn on/off lights on schedule.")
end

-- returns true if it's a weekday day
local function weekday(day_of_week)
	return day_of_week<7 and day_of_week>1
end
-- Turn on lights at 7:00am weekdays and off at 6:30pm
function lights_schedule()
	while true do
	    local event=wait_until({wday=weekday,hour=7,min=0},{wday=weekday,hour=18,min=30})
	    if event==1 then
	        for i = 1,5 do
	           outlet[i].on()
	        end
	    else -- event==2
	        for i = 1,5 do
	            outlet[i].off()
	        end
	    end
	    delay (120) -- prevent it from running more than one match in the same minute
    end
end



-- This accomplishes the same thing using an event.queue approach. 
-- This method requires firmware version 1.7.x or later
-- Remember that if there are multiple functions using the same name, only the last one in the script will run.
-- The event queue will trigger on the time or event defined. No need for additional delays to prevent multiple triggers.

-- returns true if it's a weekday day
local function weekday(day_of_week)
	return day_of_week<7 and day_of_week>1
end

-- Turn on lights at 7:00am and off at 6:30pm weekdays
-- Outlets 1-5 are lights
function lights_schedule()
    for i,t,data in event.stream(event.local_time({wday=weekday,hour=7,min=0}), event.local_time({wday=weekday,hour=18,min=30})) do
      if i == 1 then  -- On event time
        for j = 1,5 do
            outlet[j].on()
        end
      else  -- Off event time
        for j = 1,5 do
            outlet[j].off()
        end
      end    
    end
end

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

engineering@digital-loggers.com