.getHistory() - get device history data

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
renerene
Posts: 356
Joined: Wednesday 03 August 2016 11:36
Target OS: -
Domoticz version:
Contact:

.getHistory() - get device history data

Post 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.
User avatar
waltervl
Posts: 6691
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2025.1
Location: NL
Contact:

Re: .getHistory() - get device history data

Post by waltervl »

You can get a device history from database into DzVents through Domoticz http API call.
See for example viewtopic.php?t=25156
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest