Active TopicsActive Topics  Display List of Forum MembersMemberlist  Search The ForumSearch  HelpHelp
  RegisterRegister  LoginLogin
PowerHome General
 PowerHome Messageboard : PowerHome General
Subject Topic: Detect major holiday? Post ReplyPost New Topic
Author
Message << Prev Topic | Next Topic >>
jeffw_00
Super User
Super User


Joined: June 30 2007
Online Status: Offline
Posts: 929
Posted: May 30 2011 at 10:14 | IP Logged Quote jeffw_00

Hi - this feels like a semidumb qn so I'll limit it to asking for a pointer in the right direction. I want to write a macro that activates on major holidays. I see the "formulas" for major holidays under "Timed Event Formulas" but

1) I don't know what to compare them to
2) they seem to contain time-of-day, so would i have to compare them at midnight?

if anyone has some canned code that successfully tests for (anytime during) a single majot holiday, that would be sufficient for me to figure out the rest.

thanks!
/j
Back to Top View jeffw_00's Profile Search for other posts by jeffw_00
 
BeachBum
Super User
Super User
Avatar

Joined: April 11 2007
Location: United States
Online Status: Offline
Posts: 1880
Posted: May 30 2011 at 20:51 | IP Logged Quote BeachBum


Since no one jumped on this all I do is set a global that is reference in the on macro of some lights. I just use the supplied Timed Event formulas which triggers at midnight. Nothing fancy….



__________________
Pete - X10 Oldie
Back to Top View BeachBum's Profile Search for other posts by BeachBum
 
jeffw_00
Super User
Super User


Joined: June 30 2007
Online Status: Offline
Posts: 929
Posted: May 30 2011 at 21:33 | IP Logged Quote jeffw_00

oh, i get it - those are already defined - cool!

Does anyone understand the formulas for Memorial Day or Thanksgiving? I figured out how to hack "day after thanksgiving", but can't figure out how to add

LABOR DAY (first monday in sept.)
PRESIDENTS DAY (3rd? Monday in February)
INDEPENDENCE DAY (first monday in July)

Thanks!
/j

Edited by jeffw_00 - May 30 2011 at 21:46
Back to Top View jeffw_00's Profile Search for other posts by jeffw_00
 
jeffw_00
Super User
Super User


Joined: June 30 2007
Online Status: Offline
Posts: 929
Posted: June 04 2011 at 12:09 | IP Logged Quote jeffw_00

oh, come on - someone (Dave?) must understand how the Timed Event Formulas are constructed? :-}

Thanks!
/j

Edited by jeffw_00 - June 04 2011 at 12:09
Back to Top View jeffw_00's Profile Search for other posts by jeffw_00
 
krommetje
Super User
Super User
Avatar

Joined: December 29 2004
Location: Netherlands
Online Status: Offline
Posts: 695
Posted: June 04 2011 at 16:37 | IP Logged Quote krommetje

Here is an example for the 4th of july....

Code:

relativedate([REFTIME],1 * 14 + 4 - daynumber([REFTIME]) - if(daynumber([REFTIME]) = 4,0,7))


and here is the example for thanksgiving

Code:

datetime(date(year(today()) + [INCREMENT],11,if(daynumber(date(year(today()) + [INCREMENT],11,1)) > 5,34,27) - daynumber(date(year(today()) + [INCREMENT],11,1))),00:00:00)


You now have 2 examples which you can work with...
good luck!

Peter
Back to Top View krommetje's Profile Search for other posts by krommetje Visit krommetje's Homepage
 
jeffw_00
Super User
Super User


Joined: June 30 2007
Online Status: Offline
Posts: 929
Posted: June 04 2011 at 17:03 | IP Logged Quote jeffw_00

Hey peter - thanks for the copy/paste. Now - can you tell me, from these examples, how to code, say, Labor Day? If so, thanks! you're smarter than me because it's not obvious to me. If not, I'm unclear on why you posted.

thanks!
/j

Edited by jeffw_00 - June 04 2011 at 17:04
Back to Top View jeffw_00's Profile Search for other posts by jeffw_00
 
scottmi1
Groupie
Groupie
Avatar

Joined: July 11 2006
Location: United States
Online Status: Offline
Posts: 60
Posted: June 05 2011 at 19:18 | IP Logged Quote scottmi1

Here you go Jeff -

Code:
datetime(date(year(today()) + [INCREMENT],9,if(daynumber(date(year(today()) + [INCREMENT],9,8)) = 1,3,10) - daynumber(date(year(today()) + [INCREMENT],9,8))),00:00:00)


Mike..

Edited by scottmi1 - June 05 2011 at 19:22
Back to Top View scottmi1's Profile Search for other posts by scottmi1
 
jeffw_00
Super User
Super User


Joined: June 30 2007
Online Status: Offline
Posts: 929
Posted: June 05 2011 at 19:55 | IP Logged Quote jeffw_00

Ok mike - what's the trick - I need to code a number of these and clearly you've figured out the algorithm.

Thanks!
/j
Back to Top View jeffw_00's Profile Search for other posts by jeffw_00
 
scottmi1
Groupie
Groupie
Avatar

Joined: July 11 2006
Location: United States
Online Status: Offline
Posts: 60
Posted: June 06 2011 at 07:53 | IP Logged Quote scottmi1

Jeff,

I used Dave's supplied Memorial Day timed event formula as a starting point. If you take out the "+ [INCREMENT]" variables it's easier to follow.

Dave's event checks to see if the 31st of May is the 1st day of the week (Sunday). If true, it subtracts the day of the week of May 31 from 26 to get the date, if false, it subtracts the day of the week of May 31 from 33. This is a bit of clever code that relates to the earliest and latest dates in May when the last Monday can occur.

I'll admit that figuring out the exact new values to adapt this for Labor Day made my brain hurt, so I resorted to educated guesses and testing. To make this easier, I took out the "+ [INCREMENT]" variables as mentioned above so I could use the Verify function. I then replaced the 3 "year(today())" functions with an explicit year to allow me to test both paths of the IF statement. I added back the "+ [INCREMENT]" and "year(today())" once I knew the formula was working properly.

Hope this helps. So, what other dates do you need?

Mike..
Back to Top View scottmi1's Profile Search for other posts by scottmi1
 
BeachBum
Super User
Super User
Avatar

Joined: April 11 2007
Location: United States
Online Status: Offline
Posts: 1880
Posted: June 06 2011 at 08:19 | IP Logged Quote BeachBum

I think one of the best explanations is what Dave posted a while ago. This is in reference to a similar question.

“A Timed Event formula MUST evaluate to a datetime type. This is a must. Other than that, it's just a formula and the functions under the Date/Time section of the Help file are extremely useful. However, two special variables come into play. The first one is [REFTIME]. [REFTIME] is substituted for the datetime value in the "Reference Time" column of a timed event. When you are creating a brand new Timed Event, the "Reference Time" field defaults to the current date and time. If it's an existing Timed Event that has just fired and the Timed Event formula is being evaluated to determine the *next* reference time, it is the date and time in the reference field which is the datetime that the Timed Event was just executed at.

In a normal Timed Event with a "Daily" frequency, the reference time will be increased by 10080 which will be a full day forward. When that Timed Event is fired, all things being normal, the reference time will be current date and time. The frequency will then be "added" to the reference time to determine the new reference time. If the Frequency is a Timed Event Formula, the formula will be evaluated to determine the new reference time. Which brings us to our second special variable. The [INCREMENT] variable. [INCREMENT] initially starts as a value of 0. So the Timed Event formula is evaluated. If the datetime returned is less than the current datetime, 1 is added to [INCREMENT] and the Timed Event formula is evaluated again. This continues until the returned datetime is greater than the current datetime and this value becomes the new "Reference Time". So the [INCREMENT] is just a number that can be used anywhere within the Timed Event formula and increases by 1 until the formula returns a datetime in the future. You can use this [INCREMENT] value to increment the year, the day, an hour, etc.

So, if we were to breakdown the previous formula you reference in the first post, it would go like this:

relativedate([REFTIME],1 * 14 + 4 - daynumber([REFTIME]) - if(daynumber([REFTIME]) = 4,0,7))

First, in reviewing this (I did it pretty quick), the correct formula for "Every Other Wed" should be:

datetime(relativedate(date([REFTIME]),[INCREMENT] * 14 + 4 - daynumber(date([REFTIME])) - if(daynumber(date([REFTIME])) > 4,7,0)),time([REFTIME]))

[REFTIME] is substituted for whatever value is currently in the Reference Time. If this a brand new Timed Event, it will be the current date and time. If the Timed Event has already been working, it should be the Date and Time that the Timed Event has fired which should be Wednesday.

Since [REFTIME] is the "Reference Time" value which is actually a date AND a time, we should extract out the invidual date and time components using the date() and time() functions respectively.

The relativedate function accepts a date and an offset amount in days to determine a new date.

The daynumber function accepts a date and tells you what number of the day in the week that date falls on. Sunday is 1, Monday is 2, and so on. Saturday is 7. I didnt have to get quite this complicated in this formula, but I wanted it be easiest for the user in initial setup. If you are creating the Timed Event for the first time, the reference time will the current date and time which may be either before Wednesday of the current week or after Wednesday of the current week. Since it makes no sense to create a Timed Event in the past, the formula is designed to set the Reference Time to whatever the next Wednesday is.

So, lets walk our way through it for a Tuesday. Lets use this Tuesday 11/11/2008. We're creating the Timed Event for the first time. [INCREMENT] will start out as 0. Substituting [INCREMENT], we get 0 * 14 + 4 - daynumber(date(2008-11-11)) which will equal 1. Since the date 2008-11-11 is a Tuesday and the daynumber for Tuesday is 3, we end up with 4 - 3 which equals 1. The next part of the equation contains an if statement which looks like: if(daynumber(date([REFTIME])) > 4,7,0). This is basically trying to determine if we're evaluating this formula on a day before or on Wednesday (Sun, Mon, Tue, or Wed) or a day after Wednesday (Thursday, Fri, Sat). If the daynumber of [REFTIME] (2008-11-11 in our case or 2) is greater than 4, then return 7 else return 0. Since 2 is less than 4, we return 0. So our formula is now 4 - 3 - 0 which equals 1. So our relativedate function will take the Tuesday date (2008-11-11) and add 1 which will give us a date of 2008-11-12 (Wednesday). We take that date as well as the time component of the [REFTIME] and create a datetime value for 2008-11-12.

Now, lets say the Timed Event is running as normal, it's Wednesday and the Timed Event has just executed. We now need to evaluate the formula to determine the new Reference Time for when the Timed Event will run again. Lets say the date is 2008-11-12 (Wednesday). Lets also say the [REFTIME] time component was 13:00:00. [INCREMENT] of course starts at 0. So we have 0 * 14 + 4 - 4 (for this part of the formula: [INCREMENT] * 14 + 4 - daynumber(date([REFTIME]))). This equates to 0. The if statement also equates to 0 since the daynumber of the current reftime is 4 and 4 is not greater than 4 (they're equal). So our offset amount for the relativedate function is 0 and the date portion is today. So the relativedate function will return 2008-11-12 and the time component of reftime is 13:00:00. So the total datetime value returned is 2008-11-12 13:00:00. This is compared to the current date/time. Since all Timed Events are actually executed 1 second after their scheduled time (this is by design), this returned time will automatically be less than the current time (the soonest of which would be 2008-11-12 13:00:01). So 1 is added to the [INCREMENT] variable resulting in a value of 1 and we repeat the formula. [INCREMENT] * 14 + 4 - daynumber(date([REFTIME])) substitutes out to: 1 * 14 + 4 - 4 which equals 14. The if statement still evaluates to 0. The result of the relativedate function will therefore be 14 days in the future (2 weeks) from the current date which is Wednesday, 2008-11-26. This is combined with the time component of reftime which is 13:00:00 yielding a new datetime of 2008-11-26 13:00:00. This is compared with the current date/time and since it's in the future, evaluation stops and the new reference time is set.

Sorry to be so long-winded, but it's difficult to explain clearly. Usually there are several different ways of coming up with the same formula so others may come up with an Every other Wednesday formula that works just as well but is completely different.

BTW, the formula for your "Seasonal" timed event of 11/20/???? thru 12/24/???? would be:
Code:
if(date([REFTIME]) < date(year(today()),11,20),datetime(date(year(today()),11,20) ,time([REFTIME])),if(date([REFTIME]) > date(year(today()),12,23),datetime(date(year(today()) + 1,11,20),time([REFTIME])),datetime(relativedate([REFTIME],[INCREMENT]),time([REFTIME]))))




Hope this helps,

Dave.



__________________
Pete - X10 Oldie
Back to Top View BeachBum's Profile Search for other posts by BeachBum
 
jeffw_00
Super User
Super User


Joined: June 30 2007
Online Status: Offline
Posts: 929
Posted: June 06 2011 at 09:49 | IP Logged Quote jeffw_00

thanks guys - a lot to absorb here, but looks like all I need - will review tonite.

appreciate it!
/j
Back to Top View jeffw_00's Profile Search for other posts by jeffw_00
 
jeffw_00
Super User
Super User


Joined: June 30 2007
Online Status: Offline
Posts: 929
Posted: May 28 2012 at 10:38 | IP Logged Quote jeffw_00

Ok, I finally got some time. 8-}

Below are Columbus Day and Presidents Day.

I also have the list below, but not sure which ones aren't common knowledge (or in the base code)
Xmas, 7/4, Thanksgiving & day after, New years day, new years eve, memorial day, Halloween, Labor Day - if you need any of these posted let me know.

Columbus Day

datetime(date(year(today()) + [INCREMENT],10,if(daynumber(date(year(today()) + [INCREMENT],10,17)) = 1,12,19) - daynumber(date(year(today()) + [INCREMENT],10,17))),00:00:00)


President's Day

datetime(date(year(today()) + [INCREMENT],2,if(daynumber(date(year(today()) + [INCREMENT],2,24)) = 1,19,26) - daynumber(date(year(today()) + [INCREMENT],2,24))),00:00:00)


Back to Top View jeffw_00's Profile Search for other posts by jeffw_00
 

If you wish to post a reply to this topic you must first login
If you are not already registered you must first register

  Post ReplyPost New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum