• Z-Wave Forum
    Home Automation made simple
It is currently Wed Oct 18, 2017 12:15 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

ACWUT (AlarmClock WakeUpTime) version 2.0.1

by jompa68 » Sun Nov 01, 2015 6:39 pm

New version 2.0.1
LUA code will check my server for new version, will only be updated there from now.

post-873-0-35212800-1446536811.png
post-873-0-35212800-1446536811.png (14.08 KiB) Viewed 22469 times


Code: Select all
--[[

INSTRUCTIONS
*
* add those devices you want to use in this script to "devices"
* set dimmer device to a "_maxValue"
* set "_duration" to a value in minutes for how long the dim function should be active
* to activate debug, helper and versionCheck set the value to true or false

* helper is to show all "lights" in your system.
* debug is debug :)
* versionCheck checks if script is updated. Click on "Get it"

--]]

-- REFERENCE
-- forum.fibaro.com, lua.org, domotique-fibaro.fr, www.gronahus.se
-- Thanks to robmac, stevenvd for good LUA functions code.

-- 2015-11-01 ver 2.0.0 new version of scene and virtual device. Supports 2 alarm.
-- 2015-11-02 ver 2.0.1 list all lights in scene. Add those you want to use in 'User Settings' section

-- SCENE SCENARIO
-- Advanced wakeup scene to turnOn lights at specified time, time you set in
-- alarm clock virtual device
-- Set value for how much the blinds should open with the slider in VD.

alarmClock VD 2.0.png
alarmClock VD 2.0.png (47.08 KiB) Viewed 22566 times

Code: Select all
--[[
%% autostart
%% properties
%% globals
AlarmClockStatus
AlarmClockDays1
AlarmClockDays2

--]]

--[[

INSTRUCTIONS
*
* add those devices you want to use in this script to "devices"
* set dimmer device to a "_maxValue"
* set "_duration" to a value in minutes for how long the dim function should be active
* to activate debug, helper and versionCheck set the value to true or false

* helper is to show all "lights" in your system.
* debug is debug :)
* versionCheck checks if script is updated. Click on "Get it"

--]]

-- REFERENCE
-- forum.fibaro.com, lua.org, domotique-fibaro.fr, www.gronahus.se
-- Thanks to robmac, stevenvd for good LUA functions code.

-- 2015-11-01 ver 2.0.0 new version of scene and virtual device. Supports 2 alarm.
-- 2015-11-02 ver 2.0.1 list all lights in scene. Add those you want to use in 'User Settings' section

-- SCENE SCENARIO
-- Advanced wakeup scene to turnOn lights at specified time, time you set in
-- alarm clock virtual device
-- Set value for how much the blinds should open with the slider in VD.

version = "{2.0.1}"

-------------------- USER SETTINGS -----------------------
devices = {339,341,24,256,335,337,25}   -- Lights, dimmers and blinds
_maxValue = 50             -- When dimmer reach this value it will stop there
_duration = 5            -- Time in minutes for how long wakeUp should be active
debug = true            -- set debug to true or false
helper = false            -- loop through all devices and search for lights/dimmers/binarySwitch etc
versionCheck = true      -- check if new version of script exist on server

-- EXTRA FUNCTIONS, OPTIONS
-- Start Sonos/Internet Radio
vDeviceID = 393          -- Id of Sonos virtual device
vDeviceButton = 7          -- Play
vDeviceFunc = false         -- set to true to activate Sonos/Internet Radio

-- Dimmers to set to fixed value when alarmWakeup
Dimdevices = {343}         -- Dimmers to set to fixed value
fixedValue = 50            -- Dimmer value
DimdevicesFunc = false      -- set to true or false

--TimeOfDay variable --
varTOD = "TimeOfDay"      -- TOD translate
varTODMorning = "Morgon"   -- TOD Morning translate
varState = "SleepState"      -- TOD translate
varStateMorning = "Vaken"   -- TOD Morning translate
variableFunc = true         -- set to true or false
-----------------------------------------------------------
timeNow = os.date("%H:%M")
dayName = os.date("%A")

------------- DO NOT CHANGE LINES BELOW -------------------
startSource = fibaro:getSourceTrigger();
sortedtbl = {}
tmptbl = {}
runOnce = true
alarmTime1 = fibaro:getGlobal("AlarmClockTime1")
alarmTime2 = fibaro:getGlobal("AlarmClockTime2")
alarmDay1 = fibaro:getGlobal("AlarmClockDays1")
alarmDay2 = fibaro:getGlobal("AlarmClockDays2")
blindUpLevel = fibaro:getGlobal("openBlinds")

alarmTimes = {alarmTime1, alarmTime2}
alarmDays = {alarmDay1, alarmDay2}


-- 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", "WakeUpTime scene - LUA Scripting by Jonny Larsson 2015" );
Debug( "orange", "Version: "..version);
Debug( "lightgreen", "-- Set alarmtime in virtual device --");
Debug( "lightgreen", "-- Set slider to how much blinds should open --");


function timerFunction()
 
-- Check if alarmDay2 is in use, else remove it from tables
if alarmDay2 == "00:00" then
   table.remove(alarmDays, 2)
   table.remove(alarmTimes, 2)
end
for i = 1,#alarmDays do
       --Check if day and time is same as alarmDays and alarmTimes
       if timeNow == alarmDays[i] and alarmTimes[i] then
            -- Sort table of devices
         sortTable()
       else
          if (debug) then
             Debug( "grey", "Next alarmTime is set to next "..alarmDays[i]..":"..alarmTimes[i]);
          end
         end
    end

setTimeout(timerFunction, 60*1000)
end


sortTable = function()
   for i = 1,#devices do
      lightItems = devices[i];
        if fibaro:getType(lightItems) == "com.fibaro.FGR221" or fibaro:getType(lightItems) == "com.fibaro.FGRM222" or fibaro:getType(lightItems) == "com.fibaro.rollerShutter" or fibaro:getType(lightItems) == "com.fibaro.FGWP101" then
            table.insert(sortedtbl,lightItems)
       elseif fibaro:getType(lightItems) == "com.fibaro.binarySwitch" then
          table.insert(sortedtbl,lightItems)
       elseif fibaro:getType(lightItems) == "com.fibaro.multilevelSwitch" then
          table.insert(sortedtbl,lightItems)
         end
   end
     turnLightOn()
end


-- TurnOn lights, functions
turnLightOn = function()
   for i = 1,#sortedtbl do
      lightItems = sortedtbl[i];
       if fibaro:getType(lightItems) == "com.fibaro.FGR221" or fibaro:getType(lightItems) == "com.fibaro.FGRM222" or fibaro:getType(lightItems) == "com.fibaro.rollerShutter" or fibaro:getType(lightItems) == "com.fibaro.FGWP101" then
            fibaro:call(lightItems, "setValue", blindUpLevel)
         elseif fibaro:getType(lightItems) == "com.fibaro.binarySwitch" then
            fibaro:call(lightItems, "turnOn")
         elseif fibaro:getType(lightItems) == "com.fibaro.multilevelSwitch" then
            table.insert(tmptbl,lightItems)
       end
    end
      if (DimdevicesFunc) then
            for i = 1,#Dimdevices do
                DimItems = Dimdevices[i];
                fibaro:call(DimItems, "setValue", fixedValue);
          end
      end
if (vDeviceFunc) then
         fibaro:call(vDeviceID, "pressButton", vDeviceButton)
end
if tmptbl == nil then
if (debug) then
   Debug( "red",'No dimmers in table, will not run next function')
end
else wakeUpFunc()end

end

-- Now its time to turnOn some lights
wakeUpFunc = function()
if (debug) then
   Debug( "green",'Start soft wakeup light')
end
for i = 1,#tmptbl do
    lightItems = tmptbl[i];
    fibaro:call(lightItems, "setValue", "0");
   fibaro:sleep(200);
   addValue = _maxValue / tonumber(_duration);
    local currentValueLights = tonumber(fibaro:getValue(lightItems, "value"));
    local newValue = currentValueLights + addValue;
end
fibaro:sleep(1000);
 
for variable = 0, _maxValue - 1, addValue do
        local currentValueLights = tonumber(fibaro:getValue(lightItems, "value"));
        if (variable ~= 0 and currentValueLights == 0 ) then
          if (debug) then
              Debug( "blue","timer stop, lights turned off");
          end
          break;
        end
        local newValue = currentValueLights + addValue;
        if (debug) then
           Debug( "yellow",'Increase value to ' ..  newValue)
        end
        --Increases the value of the lamp
       for i = 1,#tmptbl do
       lightItems = tmptbl[i];
           fibaro:call(lightItems, "setValue", newValue);
         end
        --Waits before the next step
       fibaro:sleep(60*1000);
end
end

insert2Table = function()
if runOnce then
sortedtbl = {}
  for i = 1, 500 do
    if ("com.fibaro.FGR221" == fibaro:getType(i) or "com.fibaro.FGRM222" == fibaro:getType(i) or "com.fibaro.rollerShutter" == fibaro:getType(i) or "com.fibaro.multilevelSwitch" == fibaro:getType(i) or "com.fibaro.multilevelSwitch" == fibaro:getType(i) or "com.fibaro.binarySwitch" == fibaro:getType(i) or "com.fibaro.FGWP101" == fibaro:getType(i)) then
         table.insert(sortedtbl,i)
    end
  end
end
  Debug( "grey","All your lights will be listed in a table below, add them in devices in 'User Settings' section")
  Debug( "grey",json.encode(sortedtbl))
  runOnce = false
end

------ CHECK SCRIPT VERSION ON SERVER ------
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 = "ACWUT.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/ACWUT.lua" target="_blank" style="display:inline;color:Cyan">Get it!</a>')

    end
    end,
     function(err)
    print('error' .. err)
end
)
end
---------------- SCRIPT CHECK END ---------------------



------------------ START OF SCENE ----------------------
if ( startSource["type"] == "autostart" ) or ( startSource["type"] == "global" ) then
   if helper then
      insert2Table()
   end
   if versionCheck then
      versionChecker()
   end
     if fibaro:getGlobal("AlarmClockStatus") == "On" then
      timerFunction()
    else Debug( "blue","Scene is not active until you set AlarmClock to On")
    end
end


Alarm_Clock_2.0.vfib.zip
(2.83 KiB) Downloaded 504 times
jompa68
 
Posts: 22
Joined: Mon Feb 16, 2015 11:06 am

by Forumadministrator » Tue Nov 03, 2015 9:17 am

Nice work jompa68!

This VD must be the best one around ;)
Forumadministrator
Site Admin
 
Posts: 4
Joined: Thu Feb 05, 2015 8:15 pm

by hegu » Wed Nov 18, 2015 3:36 pm

Hi jompa68,

I can not get the alarm clock to work without making changes to the code.
I want to emphasize that I am not a programmer so it's probably totally wrong what I have done but it started working after that.

The scene never triggered at the time I put into the virtual device.
I added some debug to get to the problem.

It turned out that the scene compared the time when the scene were started with the alarm time. Time stood still;)

Here are my changes, what can be wrong in my setup that forces me to change this?
(I had already changed the "comparison part before" I realized that the time was standing still so I do not know whether it was necessary or not ...)


--Check if day and time is same as alarmDays and alarmTimes
if dayName == alarmDays[i] and timeNow == alarmTimes[i] then
-- Sort table of devices
sortTable()
else
if (debug) then
Debug( "grey", "Next alarmTime is set to next "..alarmDays[i]..":"..alarmTimes[i].."-time is-"..timeNow.."-dag-"..dayName);
end
end
end
timeNow = os.date("%H:%M")
dayName = os.date("%A")

setTimeout(timerFunction, 60*1000)
end
hegu
 
Posts: 1
Joined: Tue Nov 17, 2015 2:22 pm

by jompa68 » Mon Nov 23, 2015 6:02 pm

Hi

there is a new and updated version out now.

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


Return to LUA

cron

User Menu

Login