Posts: 77
Threads: 23
Joined: Sep 2017
Reputation:
0
Hi
I have pool water temperature currently captured as a trend with a resolution of 5 minutes.
I've seen the following info https://kb.logicmachine.net/libraries/trends/ and have the beginnings of a script
Code: require('trends')
dates = {
['start'] = { year = 2024, month = 6, day = 19 },
['end'] = { year = 2024, month = 6, day = 20 },
}
data = trends.fetch('Pool Water Temperature', dates, 0, true)
log(data)
Using my existing 5 minute data, is it possible with a different 'resolution' value to aggregate the data - for example to retrieve as 15minute data - without creating a separate trend log?
Also can I narrow the retrieval of the data to the last 6 hours instead of 24?
Many thanks in advance
Kind Regards
James
Posts: 7760
Threads: 42
Joined: Jun 2015
Reputation:
447
If you want a different resolution then either create a different trend log or calculate the required value in your script.
A custom range can specified by passing a timestamp number (in UTC) instead of a table with year/month/day. You can use os.time(date_table, true) to convert Lua date table to a UTC timestamp.
Posts: 77
Threads: 23
Joined: Sep 2017
Reputation:
0
Using the timestamp to select a subset of the data is working well.
Many thanks for pointing me in this direction.
Posts: 77
Threads: 23
Joined: Sep 2017
Reputation:
0
25.06.2024, 02:53
(This post was last modified: 25.06.2024, 03:08 by jamesng.)
After running this for a few days I've noticed that the data that is retrieved is NOT what I'm expecting.
The data does not match the data displayed in the inbuilt trends charts. Here's the script I'm using to retrieve trend data for the last 3 hours
Code: <?
require('trends')
local daterange = {}
daterange['end'] = os.time()
daterange['start'] = os.time() - (300 * 12 * 3) -- 5 mins x 12 mins per hour x 3 hours
local trenddata = {}
trenddata = trends.fetch('Office Rack Temperature', daterange, 3600, true)
local datatable = {}
for index, value in pairs(trenddata) do
datatable[index] = '{"x":' .. os.date('%d %m %Y %H:%M',value[1]) .. ',"y":' .. value[2] .. '}'
end
write("[[" .. table.concat(datatable, ",") .. "]]")
?>
Here is the data that the script retrieves
Code: [[{"x":25 06 2024 09:25,"y":19.75},{"x":25 06 2024 09:30,"y":19.75},{"x":25 06 2024 09:35,"y":19.75},{"x":25 06 2024 09:40,"y":19.75},{"x":25 06 2024 09:45,"y":19.75},{"x":25 06 2024 09:50,"y":19.75},{"x":25 06 2024 09:55,"y":19.75},{"x":25 06 2024 10:00,"y":19.75},{"x":25 06 2024 10:05,"y":19.42},{"x":25 06 2024 10:10,"y":19.33},{"x":25 06 2024 10:15,"y":19.27},{"x":25 06 2024 10:20,"y":19.27},{"x":25 06 2024 10:25,"y":19.23},{"x":25 06 2024 10:30,"y":19.17},{"x":25 06 2024 10:35,"y":19.17},{"x":25 06 2024 10:40,"y":19.17},{"x":25 06 2024 10:45,"y":19.17},{"x":25 06 2024 10:50,"y":19.17},{"x":25 06 2024 10:55,"y":18.91},{"x":25 06 2024 11:00,"y":18.85},{"x":25 06 2024 11:05,"y":18.79},{"x":25 06 2024 11:10,"y":18.71},{"x":25 06 2024 11:15,"y":18.71},{"x":25 06 2024 11:20,"y":18.71},{"x":25 06 2024 11:25,"y":18.67},{"x":25 06 2024 11:30,"y":18.6},{"x":25 06 2024 11:35,"y":18.6},{"x":25 06 2024 11:40,"y":18.6},{"x":25 06 2024 11:45,"y":18.6},{"x":25 06 2024 11:50,"y":18.6},{"x":25 06 2024 11:55,"y":18.38},{"x":25 06 2024 12:00,"y":18.33},{"x":25 06 2024 12:05,"y":18.3},{"x":25 06 2024 12:10,"y":18.26},{"x":25 06 2024 12:15,"y":18.26},{"x":25 06 2024 12:20,"y":18.26}]]
Here is a snapshot of the actual data from the trends module
Code: 25/6/2024 9:25,18.44
25/6/2024 9:30,18.82
25/6/2024 9:35,18.82
25/6/2024 9:40,18.82
25/6/2024 9:45,18.82
25/6/2024 9:50,18.82
25/6/2024 9:55,18.82
25/6/2024 10:00,19.04
25/6/2024 10:05,19.37
25/6/2024 10:10,19.37
25/6/2024 10:15,19.37
25/6/2024 10:20,19.37
25/6/2024 10:25,19.37
25/6/2024 10:30,19.64
25/6/2024 10:35,19.64
25/6/2024 10:40,19.64
25/6/2024 10:45,19.64
25/6/2024 10:50,19.64
25/6/2024 10:55,20.4
25/6/2024 11:00,22.5
25/6/2024 11:05,26.23
25/6/2024 11:10,29.38
25/6/2024 11:15,30.3
25/6/2024 11:20,30.3
25/6/2024 11:25,30.3
25/6/2024 11:30,30.3
25/6/2024 11:35,30.3
25/6/2024 11:40,30.3
25/6/2024 11:45,30.3
25/6/2024 11:50,30.3
25/6/2024 11:55,30.3
25/6/2024 12:00,30.3
25/6/2024 12:05,30.3
25/6/2024 12:10,30.3
25/6/2024 12:15,30.3
25/6/2024 12:20,30.3
25/6/2024 12:25,30.3
Can you see where this is going wrong? Am I calling trends.fetch correctly?
Many thanks in advance
Kind Regards,
James
Posts: 7760
Threads: 42
Joined: Jun 2015
Reputation:
447
Time is in UTC so you need to convert local date/time to UTC timestamp:
Code: local date = os.date('*t')
local time = os.time(date, true) -- UTC timestamp
local daterange = {
['start'] = time - 3 * 60 * 60,
['end'] = time,
}
os.date() format string must be prefixed with ! to convert UTC timestamp to local date/time value:
Code: os.date('!%d %m %Y %H:%M',value[1])
Posts: 77
Threads: 23
Joined: Sep 2017
Reputation:
0
That made all the difference!
Also in my trends.fetch call above, what 'resolution' value should I be using? I'm not after daily data, only the raw 5 minute trend data as captured.
Kind Regards
James
Posts: 7760
Threads: 42
Joined: Jun 2015
Reputation:
447
There are only two options: 86400 (daily data) and trend "data" resolution value. You can set the resolution argument to nil to use the trend "data" resolution value automatically.
Posts: 77
Threads: 23
Joined: Sep 2017
Reputation:
0
Can I convert the UTC timestamp to a local / unix timestamp using OS.date too?
|