Author |
|
edtude Groupie
Joined: July 26 2006 Location: United States
Online Status: Offline Posts: 64
|
Posted: November 09 2008 at 23:13 | IP Logged
|
|
|
I want to create my own timed event formula so that I can run macros depending on the seasons/events ie Crhistmas. So I want this formula to cover the days leading up to Christmas and several days afterwards. so if I try to understand one of the examples:
datetime(date(year(today()) +[INCREMENT],7,4,00:00:00).
I am trying to understand this, my first question is what does this have to do with it:
+ [INCREMENT]
is that the year info?? I believe this
7,4,00:00:00
means obviously July 4th anytime after midnight July 3rd. All I want is this to work from the 20th of Novemeber until the 24th of December so this is what I think....
datetime(date(year(today()) + [INCREMENT],(>= 11,20 and <= 12,24),00:00:00).
Comments? Is it too late and my brain is not functioning properly?
Edited by edtude - November 09 2008 at 23:15
|
Back to Top |
|
|
grif091 Super User
Joined: March 26 2008 Location: United States
Online Status: Offline Posts: 1357
|
Posted: November 10 2008 at 04:58 | IP Logged
|
|
|
How about this ...
if (date(today()) >= date("nov 20 2008") and date(today()) <= date("dec 24 2008"),1,0)
__________________ Lee G
|
Back to Top |
|
|
edtude Groupie
Joined: July 26 2006 Location: United States
Online Status: Offline Posts: 64
|
Posted: November 10 2008 at 08:50 | IP Logged
|
|
|
That would work in the Macro if I set the Macro to fire every day right? I only want this event to fire on the days specified, I guess I am trying to get too complicated..
|
Back to Top |
|
|
BeachBum Super User
Joined: April 11 2007 Location: United States
Online Status: Offline Posts: 1880
|
Posted: November 10 2008 at 08:58 | IP Logged
|
|
|
I believe what you want to do is addressed in this post by Dave
http://www.myx10.com/forum/forum_posts.asp?TID=1942&PN=1
Your formula would be different but I think you can achieve your results this way.
__________________ Pete - X10 Oldie
|
Back to Top |
|
|
grif091 Super User
Joined: March 26 2008 Location: United States
Online Status: Offline Posts: 1357
|
Posted: November 10 2008 at 09:00 | IP Logged
|
|
|
You put the if in the Boolean field of the timed event. The if returns false if outside the window of dates. The timed event only runs the macro when the Boolean field is true, which is only within the bounds of the specified dates.
EDIT: corrected the text to say that the macro is run only when true. You will still see a timed event in the trace, it just does not do anything.
Edited by grif091 - November 10 2008 at 09:18
__________________ Lee G
|
Back to Top |
|
|
edtude Groupie
Joined: July 26 2006 Location: United States
Online Status: Offline Posts: 64
|
Posted: November 10 2008 at 12:20 | IP Logged
|
|
|
BeachBum wrote:
I believe what you want to do is addressed in this post by Dave
http://www.myx10.com/forum/forum_posts.asp?TID=1942&PN=1
Your formula would be different but I think you can achieve your results this way.
|
|
|
Saw that thread Pete and Dave used relativedate which thru me for another loop. Was hoping he would pop in and give a lil advice. I will be playing some more tonight. Kinda love that the wife has a seasonal job I can play around with the lights while she is not around!
|
Back to Top |
|
|
BeachBum Super User
Joined: April 11 2007 Location: United States
Online Status: Offline Posts: 1880
|
Posted: November 10 2008 at 13:30 | IP Logged
|
|
|
For more information I posted a thread working with Timed Event Formulas.
http://www.myx10.com/forum/forum_posts.asp?TID=1793&PN=4
Of course sticking the formula in the Boolean field will work too as Lee suggested. Just not as eloquent as Dave put it.
__________________ Pete - X10 Oldie
|
Back to Top |
|
|
dhoward Admin Group
Joined: June 29 2001 Location: United States
Online Status: Offline Posts: 4447
|
Posted: November 10 2008 at 17:56 | IP Logged
|
|
|
The Timed Event Formulas can be tricky, but arent too bad once you get used to it. It helps to know how it works internally though.
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.
|
Back to Top |
|
|
edtude Groupie
Joined: July 26 2006 Location: United States
Online Status: Offline Posts: 64
|
Posted: November 10 2008 at 18:07 | IP Logged
|
|
|
WOW! I have to run to a meeting, but I will be studying your answer more in depth when I come back and then print it out! Thanks Dave
|
Back to Top |
|
|
BeachBum Super User
Joined: April 11 2007 Location: United States
Online Status: Offline Posts: 1880
|
Posted: November 10 2008 at 18:08 | IP Logged
|
|
|
WOW!!! Now that’s about as good an explanation you’ll ever see. I want to grow up and be like Dave but I’ve already got grey hairs at least those that are left.
__________________ Pete - X10 Oldie
|
Back to Top |
|
|
edtude Groupie
Joined: July 26 2006 Location: United States
Online Status: Offline Posts: 64
|
Posted: November 13 2008 at 19:33 | IP Logged
|
|
|
Dave
Just wanted to let you know Dave that this is working flawlessly....just the way you wrote it! thanks again.
Edited by edtude - December 10 2008 at 13:10
|
Back to Top |
|
|
|
|