• Z-Wave Forum
    Home Automation made simple
It is currently Wed Oct 18, 2017 12:11 pm
  • View new posts
  • View unanswered posts
  • Who is online
  • In total there is 1 user online :: 0 registered, 0 hidden and 1 guest (based on users active over the past 5 minutes)
  • Most users ever online was 33 on Fri Apr 21, 2017 2:30 pm
  • Users browsing this forum: No registered users and 1 guest

Sleep & SecureYourHouse scene - version 0.0.5

by jompa68 » Wed Mar 18, 2015 8:57 pm

New version, will check server for new version and add a link in scene for downloading it.
Will react on variable change if house is set to sleep or away.

-- 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
Status

--]]

-- 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
-- 2015-12-10 ver 0.0.4 small bugfix
-- 2015-12-11 ver 0.0.5 Check server if new version exist.

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

version = "{0.0.5}"


-------------------- USER SETTINGS -----------------------
debug = true            -- set debug to true or false
versionCheck = true      -- check if new version of script exist on server

-- EXTRA FUNCTIONS, OPTIONS
-- TimeOfDay, SleepState and Status variable
varTOD = "TimeOfDay"      -- TimeOfDay variable
varTODNight = "Night"      -- TimeOfDay value
varState = "SleepState"      -- SleepState variable
varStateSleep = "Sleep"      -- SleepState value
varHome = "Status"         -- Home variable
varHomeStatus = "Borta"      -- Home status home/away

-----------------------------------------------------------


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

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

function versionChecker()
local function getMethod(requestUrl, successCallback, errorCallback)
local http = net.HTTPClient()
  http:request(requestUrl, {
      options = {
        method = 'GET',
        headers = {
        },
      },
      success = successCallback,
      error = errorCallback
  })
end
content = "SSYH.lua"
local url = 'http://jonnylarsson.se/JL/'..content

getMethod(url, function(resp)
   s = resp.data
    serverVersion = string.match(s, "{(.-)}");
    scriptVersion = string.match(version, "{(.-)}");
    if serverVersion > scriptVersion then
       Debug("grey",  "Checking script version...")
       Debug("yellow", "There is a new version out! "..'<a href="http://jonnylarsson.se/JL/SSYH.lua" target="_blank" style="display:inline;color:Cyan">Click here to get it!</a>')

    end
    end,
     function(err)
    print('error' .. err)
end
)
end

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",'Closing blind '..fibaro:getName(deviceItems))
         end
            fibaro:call(deviceItems, "setValue", "0")
            fibaro:sleep(200)
        elseif fibaro:getType(deviceItems) == "com.fibaro.binarySwitch" then
            if (debug) then
            Debug( "green",'Turnoff lights '..fibaro:getName(deviceItems))
         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 dimmers '..fibaro:getName(deviceItems))
         end
            fibaro:call(deviceItems, "setValue", "0");
        elseif fibaro:getType(deviceItems) == "com.fibaro.doorSensor" then
            if (debug) then
            Debug( "green",'Arming sensor '..fibaro:getName(deviceItems))
         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 or fibaro:getGlobal(varHome) == varHomeStatus then
      startFunc()
     end
   if versionCheck then
      versionChecker()
   end
end



jompa68
 
Posts: 22
Joined: Mon Feb 16, 2015 11:06 am

by jompa68 » Sat Dec 12, 2015 10:00 am

Updated!
jompa68
 
Posts: 22
Joined: Mon Feb 16, 2015 11:06 am


Return to LUA

cron

User Menu

Login