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

Summer/Winter/Month Variable


wwojt

Question

Hi,

 

I would like to create the variable to change in therms of what month is and the moment.

 

Do you have any LUA script i could use to implement into my scenes?

 

For example if  1st June set variable to JUNE. If 1st July set variable to July.

 

Thanks.

Edited by T.Konopka
Wrong category. Moved from Crowd's Solutions.
Link to comment
Share on other sites

19 answers to this question

Recommended Posts

  • 0

coudn't this be done by say a monhly triggered counter for variable? or something.

 

say add 1 each midnight on say 20th,

Please login or register to see this image.

Snap%202016-07-14%20at%2011.52.50_zpsvep

(obdobi = monthlystate)

than have another variable that will switch between seasons.

Snap%202016-07-14%20at%2011.53.02_zpsh4r

in order to switch between seasons you might want to have few other scenes :

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

if

monthlystate = 3,

than

season = spring,

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

if

monthlystate = 6,

than

season = summer,

 

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

if

montlystate = 9

than

season = fall,

 

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

if montly state = 12

than season = winter

and

monthly state counter = 0)

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

 

sorry for ammount of scenes but its kinda nescessary IF you want it to show words, otherwise just use numeric variable to give you +1 each 4 months and work with it that way = thats 2 variables and 2 scnes.

Edited by Kuba13
Link to comment
Share on other sites

  • 0

Use the 'TimeOfDay' script as baseline and modify it to do 'MonthOfYear' and while at it, add conditional checking to update a global variable 'SeasonOfYear'.

 

I see a single script easily handling this.

Link to comment
Share on other sites

  • 0

Something like this:

Please login or register to see this code.

Enjoy coding!

Please login or register to see this image.

/uploads/emoticons/default_smile.png" srcset="https://forum.fibaro.com/uploads/emoticons/[email protected] 2x" width="20" />
Edited by Sankotronic
typo in code
Link to comment
Share on other sites

  • 0

If system already has other time-based scene(s) that update global variables like 'TimeOfDay', use the script Sankotronic provided but modify it to be triggered by global variable 'TimeOfDay' so that when time of day = sleep (or dawn/morning, or whatever deem suitable), this Month-cum-SeasonOfYear script Sankotronic will run. That way, the system has lesser time-based scenes which translate to better 'behaving' system.

Link to comment
Share on other sites

  • 0

Hi People,

Sorry I'm a noob but I'm trying to get this work.

I' need some help with this code.

I've created a global variable named
"Season State" with the corresponding seasons such as "Spring", "Summer", "Autumn", "Winter".

and

"MonthState" with all months of the Year
Unfortunately, the script is not working.

I get the following message in the debug screen:

 

"line32 ')' expected (to close'( 'at line31) near then

[DEBUG] 11:46:02: line 32: ')' expected (to close '(' at line 31) near 'then'

 

--[[ 
%% autostart 
%% properties
%% globals
--]]
local debug = true;

local function log(str) if debug then fibaro:debug(str); end; end

local sourceTrigger = fibaro:getSourceTrigger();
if (sourceTrigger["type"] == "autostart") then
  
  -- check script instance count in memory 
if (tonumber(fibaro:countScenes()) > 1) then 
    log("Script already running.");
    fibaro:abort(); 
end

  log("HC2 start script at " .. os.date());
while true do
local currenttime  = os.date('*t');
local currentmonth = currenttime['month']
local currentwday  = currenttime['wday']
local currentday   = currenttime['day']
local currenthour  = currenttime['hour'];
local currentmin   = currenttime['min'];
local months = {'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'}
local weekday      = {'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Sunday'}

  -- if it is 1st day of the month and just after midnight then update month & season
if ((currentday == 1) and (currenthour == 0) and (currentmin == 0) 
then
    fibaro:setGlobal("MonthState", months[currentmonth]);
if ( currentmonth >= 3 and currentmonth <= 5) then
      fibaro:setGlobal("SeasonState", "Spring");
elseif ( currentmonth >= 6 and currentmonth <= 8) then
      fibaro:setGlobal("SeasonState", "Summer");
elseif  ( currentmonth >= 9 and currentmonth <= 11) then  
      fibaro:setGlobal("SeasonState", "Autumn");
else  
      fibaro:setGlobal("SeasonState", "Winter");
end
end

  -- if it is midnight then update weekday
if ((currenthour == 0) and (currentmin == 0) then
    fibaro:setGlobal("WeekState", weekday[currentwday];
end
  Fibaro:sleep(60000) -- sleep for one minute
end -- loop

Link to comment
Share on other sites

  • 0

Better to use Code-block...

 

But your bug is here:

 

Please login or register to see this code.

There is a closing bracket missing.

 

Also here:

Please login or register to see this code.

 

Link to comment
Share on other sites

  • 0

Please login or register to see this code.

I did add three closing brackets (line 28, 42, 43) and one "end" (last line).

 

You must have deleted them somehow.

Every opening bracket must have a closing one later on so that the parser knows what belongs together or where this part of them command ends.

 

I did not check if there is any other bug or if I missed another bracket ;)

Edited by Alex
More text
Link to comment
Share on other sites

  • 0

Oke, this is getting almost right but in the debug window i get:

"debug line 45; attempt to index global fibaro (a nil value)"

 

Thanks!

 

[DEBUG] 18:34:02: 

 

Link to comment
Share on other sites

  • 0
1 hour ago, henzel said:

Oke, this is getting almost right but in the debug window i get:

"debug line 45; attempt to index global fibaro (a nil value)"

 

Thanks!

 

[DEBUG] 18:34:02: 

 

 

Just change this line

Please login or register to see this code.

to

Please login or register to see this code.

Sorry for typos in code. I updated first post.

Link to comment
Share on other sites

  • 0

Please login or register to see this code.

Please login or register to see this code.

 

 

as for me it's look like crazy, I should waiting Monday or Midnight for setting. :shock:

 

IMHO

1st note- I think easy to check last modified property like 

local lastChangeGlobVar = tonumber(((os.time()) - tonumber(fibaro:getGlobalModificationTime("MonthState"))));

2nd note - do not use strong comparison like

((currenthour == 0) and (currentmin == 0)) 'cuz you have chance to lose moment for start, for some reasons

 

 

Link to comment
Share on other sites

  • 0
54 minutes ago, 10der said:

Please login or register to see this code.

Please login or register to see this code.

 

 

as for me it's look like crazy, I should waiting Monday or Midnight for setting. :shock:

 

IMHO

1st note- I think easy to check last modified property like 

local lastChangeGlobVar = tonumber(((os.time()) - tonumber(fibaro:getGlobalModificationTime("MonthState"))));

2nd note - do not use strong comparison like

((currenthour == 0) and (currentmin == 0)) 'cuz you have chance to lose moment for start, for some reasons

 

 

 

Hi 10der,

 

You are absolutely right in both notes. Checking when global is last changed can be used in many cases, but can you give me an example for this code where one scene is constantly running just to adjust some time based global variables?

 

Regarding second note you are right, it is better to use different approach because strong comparison is due to be missed if for example HC is restarted at that moment.

Does the following code looks better for you or you can give me some advice how to do it better:

Please login or register to see this code.

Above scene is still in development phase and I'm missing some more features and descriptions for global variables used. It is working perfectly for me but I want to improve code so that can be used on any system meaning that users will only have to change setup that suits their need without needing to change code itself.

Link to comment
Share on other sites

  • 0
Quote

You are absolutely right in both notes. Checking when global is last changed can be used in many cases, but can you give me an example for this code where one scene is constantly running just to adjust some time based global variables?

 

Please login or register to see this code.

or

  1. -- We want to get the date of the last modification of the global variable named 'counter'
  2. local value, modTime = fibaro:getGlobalModificationTime('counter')
  3. -- if the value of the global variable has changed is more than 10 seconds
  4. if os.time() - value >= 10 * 1000 then
  5. fibaro:debug('The modification occurred over 10s')
  6. else
  7. fibaro:debug('The modification occurred is less than 10s')
  8.  

 

 

Quote

Does the following code looks better for you or you can give me some advice how to do it better:

 

I should look carefully at code. 

 


PS: offtopic

Please login or register to see this code.

Ha-ha-ha... I can read this text without translation, because I am Ukrainian :)

 

Link to comment
Share on other sites

  • 0

@Sankotronic, @Alex,  thanks this works for now.

The debug windows remains clean of errors.

Thank you for helping people.

 

I continue to follow this topic.

 

Regards!

Link to comment
Share on other sites

  • 0

@Sankotronic

 

Hi,

 

I've tested your script, and I'm sorry to say I'm not a LUA programmer. I'm trying to learn, though.

 

When I try your script, I keep getting an error on row 74 (  code: local presentStateMapping = {Home="Home", Away="Away", Holiday="Holiday"};  )

Error message: "line 74: bad argument #1 to 'match' (string expected, got nil)

 

I've made the Global Variables mentioned in the script. Can you tell me where to correct this?

 

Thank you for all your time and help here,

Cheers

Link to comment
Share on other sites

  • 0

@robw

 

Hi,

 

This sounds like some typo. Can you please copy/paste this scene from your HC and send me on PM to check.

Link to comment
Share on other sites

  • 0

Hi Guys,

I am trying to simplify my Global Variable scenes but I do something wrong

Can some one point me?

The Global Variables are created and in second instance I added the values no luck

scene runs without errors

Please login or register to see this image.

/monthly_2019_03/image.png.157f9349e9bab18a935b6239a838acc4.png" alt="image.png.157f9349e9bab18a935b6239a838acc4.png" />

Link to comment
Share on other sites

  • 0

Your 'elseif' is incorrect. A month cannot be both >= 11 AND <= 3. Change 'and' to 'or'.

 

Or just lose the 'elseif' line and replace with 'else'.

 

if (currentmonth >= 4 and currentmonth <= 10) then

  fibaro:setGlobal("StookSeizoenen", "Zomers")

else

  fibaro:setGlobal("StookSeizoenen", "Winters")

end

Link to comment
Share on other sites

  • 0

I wrote this which may help if your on HC3

 

Create a Variable Season

 

Declarations:

{
  conditions = { {
      isTrigger = true,
      operator = "match",
      property = "cron",
      type = "date",
      value = { "01""00""*""*""*""*" }
    } },
  operator = "all"
}
 
Actions:
local tSeason = {"Spring","Summer","Autumn","Winter"}
 
function WhichSeason ()
    local tNow = os.date("*t")
    local dayofyear = tNow.yday
    local season 
        if (dayofyear >= 79and (dayofyear <172)then season = tSeason[1]
        elseif (dayofyear >=172and (dayofyear <266then season = tSeason[2]
        elseif (dayofyear >=266and (dayofyear <355then season = tSeason[3]
        else season = tSeason[4]
        end
    return season
end
 
local currentSeason = WhichSeason()
fibaro.setGlobalVariable("Season", tostring(currentSeason))
--fibaro.debug("Season", fibaro.getGlobalVariable("Season"))
 
Edited by BusterB1959
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...