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

LUA Code to Set Global Variable


Bips

Question

I have set a global variable called "LightsOn" in the HC2 variable panels with a value of 0

 

What im trying to do is set the value to 1 if the time of day is after 17:00 and before 07:00.

 

Got other scenes that will switch on lights if the value is 1, which seem to work ok when the value is 1.

 

Problem im having is the LUA code im using doesnt trigger by itself, if i run it, it sets to 1 if the time is from 17:00-07:00, and zero if not.

 

How can i get the code to trigger automatically, even though i have set it to auto

 

Or is there a better way of setting the global variable?

 

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

local sourceTrigger = fibaro:getSourceTrigger();
function tempFunc()
local currentDate = os.date("*t");
local startSource = fibaro:getSourceTrigger();
local LightsOn = fibaro:getGlobalValue("LightsOn")
if (
 ( (tonumber(os.date("%H%M")) >= tonumber(string.format("%02d%02d", "17", "15")) or tonumber(os.date("%H%M")) <= tonumber(string.format("%02d%02d", "07", "00"))) and (math.floor(os.time()/60)-math.floor(1676306520/60))%1 == 0 )
)
then
    fibaro:setGlobal("LightsOn", "1");
    fibaro:debug("Stage 1")
    fibaro:debug(LightsOn)
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("LightsOn", "0");
    fibaro:debug("Stage 2")
    fibaro:debug(LightsOn)
end

end
 

 

 

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

Hi @Bips ,

 

Try this code:

Please login or register to see this code.

 

Scene if autostarted then will loop and compare current time with set times. If current time is between from time and to time then will set global variable LightsOn to value "1" then print this change in debug window and then will continue checking time in silence (no debug is printed). When current time goes out of the set times then will set global variable to "0" then print this change and again continue to loop in silence.

If scene is manually run then will do only one check, do what is needed and stop running.

 

Since from time is "17:00" so later than to time "07:00" you have to check if current time is between "17:00" and "23:59" and also if it is between midnight "00:00" and to time "07:00" in the morning to properly keep lights turned on during night until morning time.

 

Above code can be easily extended to check day of the week and e.g. turn on lights with from/to times only on weekends or to setup different from/to times for different days of the week.

 

 

  • Like 3
Link to comment
Share on other sites

  • 0
  • Inquirer
  • On 2/16/2023 at 11:09 AM, Sankotronic said:

    Hi @Bips ,

     

    Try this code:

    Please login or register to see this code.

     

    Scene if autostarted then will loop and compare current time with set times. If current time is between from time and to time then will set global variable LightsOn to value "1" then print this change in debug window and then will continue checking time in silence (no debug is printed). When current time goes out of the set times then will set global variable to "0" then print this change and again continue to loop in silence.

    If scene is manually run then will do only one check, do what is needed and stop running.

     

    Since from time is "17:00" so later than to time "07:00" you have to check if current time is between "17:00" and "23:59" and also if it is between midnight "00:00" and to time "07:00" in the morning to properly keep lights turned on during night until morning time.

     

    Above code can be easily extended to check day of the week and e.g. turn on lights with from/to times only on weekends or to setup different from/to times for different days of the week.

     

     

    Thank you so much Sanlotronic, the code is brilliant and works a treat, much appreciated 

    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.

    ×
    ×
    • Create New...