Page 1 of 1

Sleep & SecureYourHouse scene - version 0.0.2

Posted: 19 Mar 2015, 07:31
by jompa68
-- SCENE SCENARIO
-- Scene will check through API for all lights,dimmers,blinds and sensors and
-- store them in a temp table.
-- When TimeOfDay or SleepState variable are set to a predefined value it will
-- loop through the temp table and do stuff like
-- turnOff all lights and dimmers, close blinds, arm door/windows sensors.


You are welcome to give me input for strange coding or if you want new features :-D

Code: Select all

--[[
%% autostart
%% properties
%% globals
TimeOfDay
SleepState

--]]

-- REFERENCE
-- forum.fibaro.com, lua.org, domotique-fibaro.fr, www.zwaveforum.net 
-- 2015-03-18 ver 0.0.1 
-- 2015-03-19 ver 0.0.2 Check if binarySwitch isLight

-- SCENE SCENARIO
-- TurnOff all lights and dimmers. Close blinds. Arm door/windows sensors.
-- 

-------------------- USER SETTINGS -----------------------
debug = true				-- set debug to true or false

-- EXTRA FUNCTIONS, OPTIONS
-- TimeOfDay and SleepState variable
varTOD = "TimeOfDay"		-- TimeOfDay variable
varTODNight = "Night"		-- TimeOfDay value
varState = "SleepState"		-- SleepState variable
varStateSleep = "Sleep"		-- SleepState value
-----------------------------------------------------------


------------- DO NOT CHANGE LINES BELOW -------------------
startSource = fibaro:getSourceTrigger();
version = "0.0.2"

-- Give debug a fancy color
Debug = function ( color, message )
  fibaro:debug(string.format('<%s style="color:%s;">%s</%s>', "span", color, message, "span")); 
end

Debug( "orange", "Sleep & SecureYourHouse scene - LUA Scripting by Jonny Larsson 2015" );
Debug( "orange", "Version: "..version);

startFunc = function()
ADevicesbl = {}
local httpClient = net.HTTPClient(); 
httpClient:request('http://127.0.0.1:11111/api/devices', { 
    success = function(resp) 
      local devices = json.decode(resp.data) 
      	for i, v in pairs(devices) do
        	table.insert(ADevicesbl,v.id)
        end
        activateFunc()
    end, 
    error = function(err) 
      print('error = ' .. err) 
    end, 
    options = { 
      method = 'GET' 
    }
})
end

-- TurnOff lights and close blinds, functions
activateFunc = function()

   for i = 1,#ADevicesbl do
      deviceItems = ADevicesbl[i];
       if fibaro:getType(deviceItems) == "com.fibaro.FGR221" or fibaro:getType(deviceItems) == "com.fibaro.FGRM222" or fibaro:getType(deviceItems) == "com.fibaro.rollerShutter" then
         	if (debug) then
				Debug( "green",'Close all blinds')
			end
      		fibaro:call(deviceItems, "setValue", "0")
      		fibaro:sleep(200)
        elseif fibaro:getType(deviceItems) == "com.fibaro.binarySwitch" then
         	if (debug) then
				Debug( "green",'Turnoff all lights')
			end
      		if fibaro:getValue(deviceItems, "isLight") == "1" then 
            	fibaro:call(deviceItems, "turnOff")
      			fibaro:sleep(200)
        	end
    	elseif fibaro:getType(deviceItems) == "com.fibaro.multilevelSwitch" then
         	if (debug) then
				Debug( "green",'Turnoff all dimmers')
			end
         	fibaro:call(deviceItems, "setValue", "0");
        elseif fibaro:getType(deviceItems) == "com.fibaro.doorSensor" then
         	if (debug) then
				Debug( "green",'Arm sensors')
			end         
            fibaro:call(deviceItems, "setArmed", "1")
      		fibaro:sleep(200)
       end
    end

end

------------------ START OF SCENE ----------------------
if ( startSource["type"] == "global" ) then
  	if fibaro:getGlobal(varTOD) == varTODNight or fibaro:getGlobal(varState) == varStateSleep then
		startFunc()
    end
end