Active TopicsActive Topics  Display List of Forum MembersMemberlist  Search The ForumSearch  HelpHelp
  RegisterRegister  LoginLogin
PowerHome Programming
 PowerHome Messageboard : PowerHome Programming
Subject Topic: Comparing DateTime values Post ReplyPost New Topic
Author
Message << Prev Topic | Next Topic >>
GadgetGuy
Super User
Super User
Avatar

Joined: June 01 2008
Location: United States
Online Status: Offline
Posts: 942
Posted: December 15 2010 at 10:54 | IP Logged Quote GadgetGuy

I'm struggling with comparisons of DateTime values. I've tried everything I can think of and prowled the Forum without success.

I am trying to determine if the house is "occupied" by determining of there is more than 4 hours since the last time that motion was detected.

The motion timestamp is saved with a datetime() function and looks like . . .

       

I want to determine if that time is greater than 4 hours before the current TODAY() value.

Doesn't seem like it should be that hard, but it sure has me stumped

__________________
Ken B - Live every day like it's your last. Eventually, you'll get it right!
Back to Top View GadgetGuy's Profile Search for other posts by GadgetGuy
 
GadgetGuy
Super User
Super User
Avatar

Joined: June 01 2008
Location: United States
Online Status: Offline
Posts: 942
Posted: December 15 2010 at 11:10 | IP Logged Quote GadgetGuy

I thought I might have discovered a way to do what I want, but am still hitting roadblocks.

Here is a bit of test code that works when I make Seq# 50 . . .

    ph_minutesafter (TODAY(),TODAY())



But as soon as I use [LOCAL3] as a variable, the routine fails with a Syntax Error.

Since [LOCAL3] was created with the datetime() function and looks "OK" when printed out with ph_msgbox(), it has me stumped.
{Where the 1st msgbox line is LOCAL3, line 2 is LOCAL4 (TODAY) and line 3 is LOCAL5}

It appears that my LOCAL3 datetime variable really isn't, but why?

Edited by GadgetGuy - December 15 2010 at 11:37


__________________
Ken B - Live every day like it's your last. Eventually, you'll get it right!
Back to Top View GadgetGuy's Profile Search for other posts by GadgetGuy
 
grif091
Super User
Super User


Joined: March 26 2008
Location: United States
Online Status: Offline
Posts: 1357
Posted: December 15 2010 at 20:07 | IP Logged Quote grif091

Set System [Local1] today()
Wait 65
Set System [Local2] today()
Set System [Local3] ph_minutesafter(ph_getvar_dt(1,1), ph_getvar_dt(1,2))
User Messsage “Local3=” + ph_getvar_n(1,3)

Local3 has a number of minutes between the two times [Local1] and [Local2]

By comparing Local3 to > 240 will be more than 4 hours


__________________
Lee G
Back to Top View grif091's Profile Search for other posts by grif091
 
GadgetGuy
Super User
Super User
Avatar

Joined: June 01 2008
Location: United States
Online Status: Offline
Posts: 942
Posted: December 16 2010 at 07:21 | IP Logged Quote GadgetGuy

Lee - thanks but I'm not sure what you answered

I haven't had any problems making ph_minutesafter() work using Today().

My issue has been that I'm trying to compare Today() with a Today() time captured earlier and saved as a Global var string used to label Motion camera images with a time stamp.

To get the datetime back from that string I thought that the DATETIME() function should do the trick to reconstruct a numeric datetime value.

Apparently it doesn't, and that is my issue.

To eliminate the LOACALs as issues I even tried

Code:
ph_minutesafter(datetime("12/15/10,09:00:00"), TODAY())


where TODAY() was later than 9AM so I didn't have a negative compare issue, if any.

That fails with a SYNTAX ERROR, however, leading me to suspect something is going on with the datetime() function

PS- Re-reading your post several times, I dawned on me that maybe I missed that you were saying that ph_minutesafter() doesn't work with the use of [LOCAL3] in the parameter list, and that the ph_getvar_dt(1,3) form must be used instead? Is that what my syntax error is about?

__________________
Ken B - Live every day like it's your last. Eventually, you'll get it right!
Back to Top View GadgetGuy's Profile Search for other posts by GadgetGuy
 
grif091
Super User
Super User


Joined: March 26 2008
Location: United States
Online Status: Offline
Posts: 1357
Posted: December 16 2010 at 09:59 | IP Logged Quote grif091

The previous example was not breaking down DateTime and then reconstructing it. Seemed less complex to save and use Today() DateTime values directly.

The following takes a DateTime value in Local3, uses it in ph_minutesafter(), storing the result in Local4. As with the previous post this is a working example. Hope this one helps more than the last.


Set System [Local3] datetime("12/16/2010, 08:00:00")
Set System [Local4] ph_minutesafter(ph_getvar_dt(1,3), today())
User Message "Local3=" + ph_getvar_dt(1,3) + "   Local4=" + ph_getvar_n(1,4)

The User Message - Local3=12/16/2010 08:00:00   Local4=131.6132666666667

Edited by grif091 - December 16 2010 at 10:14


__________________
Lee G
Back to Top View grif091's Profile Search for other posts by grif091
 
BeachBum
Super User
Super User
Avatar

Joined: April 11 2007
Location: United States
Online Status: Offline
Posts: 1880
Posted: December 16 2010 at 10:15 | IP Logged Quote BeachBum

If I’m not missing the objective here, you could also use a Timed Event to trigger greater than 4 hours and Modify it if less.

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

Joined: June 01 2008
Location: United States
Online Status: Offline
Posts: 942
Posted: December 16 2010 at 10:24 | IP Logged Quote GadgetGuy

Lee - directly or indirectly (depending on how one accesses the way my mind works) you solved the problem.

Apparently the following does NOT work ...
Code:
ph_minutesafter ([LOCAL3],TODAY())


BUT, your form does ...
Code:
ph_minutesafter (ph_getvar_dt(1,3),TODAY())


For ease of code reading and for simplicity I have always used the [LOCALxx] style of variable, rather than the ph_getvar() function. Apparently this approach does not always work!

With your approach, I now have everything working. Thanks for your great insight. I must admit that PH keeps me humble! Every time I think I'm really getting to understand it well, I get a curve ball that strikes me out.

__________________
Ken B - Live every day like it's your last. Eventually, you'll get it right!
Back to Top View GadgetGuy's Profile Search for other posts by GadgetGuy
 
grif091
Super User
Super User


Joined: March 26 2008
Location: United States
Online Status: Offline
Posts: 1357
Posted: December 16 2010 at 10:33 | IP Logged Quote grif091

The issue is that [Local3] is a string. The ph_minutesafter() requires Type DateTime, thus the ph_getvar_dt() call to convert to DateTime Type.

Edited by grif091 - December 16 2010 at 10:34


__________________
Lee G
Back to Top View grif091's Profile Search for other posts by grif091
 
GadgetGuy
Super User
Super User
Avatar

Joined: June 01 2008
Location: United States
Online Status: Offline
Posts: 942
Posted: December 16 2010 at 10:40 | IP Logged Quote GadgetGuy

Roger that!

After playing around with this new success, I finally realized that.

It didn't dawn on me initially, and I must have missed it in the documentation

__________________
Ken B - Live every day like it's your last. Eventually, you'll get it right!
Back to Top View GadgetGuy's Profile Search for other posts by GadgetGuy
 
dhoward
Admin Group
Admin Group
Avatar

Joined: June 29 2001
Location: United States
Online Status: Offline
Posts: 4447
Posted: December 21 2010 at 21:57 | IP Logged Quote dhoward

Not wanting to confuse things, but figured I could add some "little known" info for those interested.

As Lee pointed out, the variables are stored internally as a string. The ph_getvar_XX functions will take this string and perform the proper data conversion (if possible). This is generally safer and goes a long way into insuring you don't get syntax errors from data types that dont match. The [LOCALXX] form of a variable is a "search and replace" type of mechanism. If you're certain that the var contains the datatype that you're interested in, you can use this form. Some examples might help clear things up:

[LOCAL1] contains 5

[LOCAL1] + 8 will return a number type that equals 13

[LOCAL1] + " is a number." will return a syntax error because the formula actually passed to the evaluator will look like: 5 + " is a number."

"[LOCAL1]" + " is a number." will work and returns a string that looks like: 5 is a number.

"[LOCAL1] is a number." will also work and is the equivalent of the formula just above.

What is little known is that a datetime value is in the format of yyyy-mm-dd hh:mm:ss. No comma between the date and time.

Using this new info, if the [LOCAL3] var contains a saved datetime value in the form of 12/15/2010 10:49:00 will fail in this formula:

ph_minutesafter([LOCAL3],today())

But...it will suceed if the saved datetime value in [LOCAL3] looks like: 2010-12-15 10:49:00.

Dave.
Back to Top View dhoward's Profile Search for other posts by dhoward Visit dhoward's Homepage
 

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