Finding A Single Value In An Array Using A Specific Time

Recommended Posts



I note that it is possible to retrieve data from an array based on a [from, to] time range e.g. chan[5h,6h]. Is it possible to retrieve a single piece of data based on a specific time.


I have tried the following unsuccessfully.



I've been through the manual but can't find anything that will do this.


I've also tried v.mychannel[sysTime()-60, Systime()-59.9]. This works but seems a bit erratic when I display the result on a page.



Link to comment
Share on other sites

Yes, you certainly can.  For example to get all the data in the last hour, you would do:


myChannel[systime(), systime() - 3600]


OK, but here's the trick and why you were having difficulty: time is a floating point value and recorded to a precision in the microseconds.  If you do v.myChannel[systime() - 60] then it will only return a data point if there is a data point with the exact time, to the microsecond 60 seconds in the past at the instant the code is executed.  This would be very unlikely so pretty much always returns an empty value.  Really the only way you can use a single time value to subset is when you've recorded that time.  For example,


private theTime = myChannel.time[5]


? myChannel[theTime]


It may seem like myChannel[theTime] would equal myChannel[5], but not if new data is being added to the channel because [5] becomes [6], then [7], etc.  But the data at that exact timestamp remains the same.  This works because theTime has the exact timestamp to the millisecond.


You have a similar problem when you tried to do the range.  Your range is only 0.1 seconds, but the refresh rate for the page is probably once a second, so only about 1 in 10 page refreshes has a data point in that data range (if the data rate is also once a second), and worse then that, its possible that the page refresh will line up in the blank area and not show a value for a long time.  You really need to broaden the range to be longer than the refresh rate of the page and the data read rate.  If you find that this returns several values, subset the result:


(myChannel[systime()-60, systime()-58])[0]

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.