Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

Day/Night variable check


amatt
 Share

Question

I am trying to create a day/night setup via lua. Where at 8 am-10pm the variable is set to day and at 10pm-8am the variable is set to night. I cannot successfully do this via a block scene so i have used LUA to do this. Could someone help me look over my code to see if this is correct as i am fairly new in LUA. Thanks!

 

 

 

--[[ 
%% autostart 
%% properties 
%% globals 
--]] 

-- Only need to call getSourceTrigger once
-- For better debugging, I moved it to the top,
-- before countScenes

local sourceTrigger = fibaro:getSourceTrigger()
fibaro:debug("Script started because of: " .. sourceTrigger["type"]) 

if fibaro:countScenes()>1 then
 fibaro:debug("Scene already running, aborting this one")
 fibaro:abort() 
end 

if (sourceTrigger["type"] == "autostart") then 

 while true do 

   local currentDate = os.date("*t")

   -- Do the time-string manipulation thingy here, instead of in
   -- each and every if-then statement
   -- %02u means unsigned int, 2 wide, leading zero

   TimeAsString =  string.format("%02u",currentDate.hour).. ":" ..
   string.format("%02u", currentDate.min)

   fibaro:debug("TimeAsString: ".. TimeAsString)

   Day_night = fibaro:getGlobalValue("Day_night") 

   fibaro:debug("Day_night: " .. Day_night)
    
     if 
     (
       currentDate.wday == 1 
       or currentDate.wday == 2 
       or currentDate.wday == 3 
       or currentDate.wday == 4 
       or currentDate.wday == 5 
       or currentDate.wday == 6 
       or currentDate.wday == 7
     ) 
     and
     TimeAsString == "8:00"

     then 
     fibaro:debug("Setting scenario number 1")  
     fibaro:setGlobal("Day_night", "Day")

       elseif 
     (
       currentDate.wday == 1 
       or currentDate.wday == 2 
       or currentDate.wday == 3 
       or currentDate.wday == 4 
       or currentDate.wday == 5 
       or currentDate.wday == 6 
       or currentDate.wday == 7
     ) 
     and TimeAsString == "22:00"

     then

     fibaro:debug("Setting scenario number 2") 
     fibaro:setGlobal("Day_night", "Night")
      
        else

     fibaro:debug("Current time doesn't match any scenario")
      
      end
    end
    
    fibaro:debug("Scene not started, this is autostart-only!")
end -- end of sourceTrigger["type"] == "autostart"

 

 

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

It is pretty simple to do via Block scenes, it would look like (attached)

The same in LUA would look like:

 

--[[
%% autostart
%% properties
%% events
%% globals
--]]

local sourceTrigger = fibaro:getSourceTrigger();
function tempFunc()
local currentDate = os.date("*t");
local startSource = fibaro:getSourceTrigger();
if (
 ( ((currentDate.wday == 1 or currentDate.wday == 2 or currentDate.wday == 3 or currentDate.wday == 4 or currentDate.wday == 5 or currentDate.wday == 6 or currentDate.wday == 7) and string.format("%02d", currentDate.hour) .. ":" .. string.format("%02d", currentDate.min) == "05:00") )
)
then
    fibaro:setGlobal("TimeOfDay", "Morning");
end

setTimeout(tempFunc, 60*1000)
end
if (sourceTrigger["type"] == "autostart") then
tempFunc()
else

local currentDate = os.date("*t");
local startSource = fibaro:getSourceTrigger();
if (
startSource["type"] == "other"
)
then
    fibaro:setGlobal("TimeOfDay", "Morning");
end

end

Please login or register to see this attachment.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...