Page 1 of 1

.getHistory() - get device history data

Posted: Sunday 02 April 2023 23:05
by renerene
development request:
A function like this would be great to have in dzVents:
The transcript below is of a chatGPT conversation, that shows it's bad knowledge about dzVents, because getHistory() is a made up function.

"To retrieve data from a sensor in the past using dzVents, you can use the getHistory() method. You can define the start and end times for the desired period using os.time(), then use the getHistory() method to retrieve the history data for the device for that period. The method returns a table of DeviceHistoryItem objects, each with a time and value property. You can then loop through the history data and print out the time and value of each item using os.date() to format the time in a readable format.

here is an example of how to use the getHistory() method to retrieve data from a sensor in the past:

Code: Select all

local myDevice = 'myDeviceName'
local startTime = os.time{year=2023, month=3, day=1, hour=0, min=0, sec=0}
local endTime = os.time{year=2023, month=3, day=31, hour=23, min=59, sec=59}
local history = dz.devices(myDevice):getHistory(startTime, endTime)

for _, item in ipairs(history) do
  print(os.date('%c', item.time) .. ': ' .. item.value)
end
In this example, we first define the name of the device we want to retrieve history data from, and then we define the start and end times for the period we want to retrieve data from using os.time(). We then use the getHistory() method to retrieve the history data for the device for that period, and store it in a variable called history. Finally, we loop through the history table and print out the time and value of each item using os.date() to format the time in a readable format."

There you have it. Would be nice to read database history values directly within dzvents.the current Historical variables API, as described' on the wiki page is more complex, because you are building a copy / shadow variable.

Re: .getHistory() - get device history data

Posted: Monday 03 April 2023 8:53
by waltervl
You can get a device history from database into DzVents through Domoticz http API call.
See for example viewtopic.php?t=25156