previousAge() Does Not Produce Expected Result

wiki

#1

I’m not sure if this is a bug, an error in the wiki, or a simple misunderstanding on my part due to an excess of fermented grape juice. :wink: The wiki for the previousAge function states:

previousAge([device﹕attribute])
returns the number of milliseconds an attribute had the previous value

but my experience is that the number the function returns doesn’t quite match this description. Consider the following actual data from a presence sensor (for simplicity I’ve converted the results to minutes):

example

The age([device﹕attribute]) function correctly returns the amount of time an attribute had the current value. However, the previousAge([device﹕attribute]) function doesn’t return the amount of time an attribute had the previous value (which in my view should be 177 minutes), but rather the total amount of time since the device last began to have the previous value (177+378=555).

So in order to get the answer I’m expecting I have to write the expression as

previousAge([device : attribute]) - age([device : attribute])

That’s fine, the wiki was just misleading on this point and I’m uncertain if the function is working as intended.


#2

That is misleading, thanks for reporting. Confirmed that it is coded as you described. Also worth noting in the wiki, if there is no known previous value this will return 604800000 which is 7 days.


#3

I would suggest this be changed in the code rather than the wiki to achieve what I believe was the intended result, but I don’t want to be responsible for blowing up a few thousand pistons. :rofl:


#4

Wiki has been updated https://wiki.webcore.co/Functions#Integers

previousAge([device﹕attribute])

returns the number of milliseconds that have elapsed since an attribute changed to the previous value. If there is no previous value recorded returns 604800000 , which corresponds to 7 days.

Use previousAge([device﹕attribute]) - age([device﹕attribute]) to determine the length of time that the device attribute had the previous value.


#5

I use previousAge to tell me the amount of time the previous state lasted, not the amount of time elapsed since the change before last.

In early minion discussions at the development stage, this was always the intended result.


#6

But as currently coded previousAge() doesn’t tell you the amount of time the previous state lasted, instead it tells you the amount of time since the change before last. In order to get the amount of time the previous state lasted (i.e, in my original example, to get: “How long was I home before I left?”) you have to use.

previousAge([device : attribute]) - age([device : attribute])

There’s no question that the way the previousAge() function is currently coded is inconsistent with the way the age() function is coded, but the workaround is simple enough and I can live with it. It just didn’t make sense to me, especially since it contradicted the wiki. I thought it was worth pointing out just in case someone cared.

I apologize if my joke somehow offended. :confused:


#7

never noticed, but then I’ve always checked previousAge when triggered by the device I’m checking, so I suppose it never has a chance to clock up extra time before being checked.


#8

I expect this is true for a number of people. It was for me with my earlier pistons.

Perhaps since previousAge() does not provide what you indicate was the ‘intended result’ the minions might want to address it before webCoRE matures further. But … that’s for others to think about; as long as I understand the result I’m getting, I can work with it. :slightly_smiling_face: