Retrieving list of events

I am trying (and failing) to programmatically retrieve a list of all events (7 days worth) for a device. When logged in to the IDE I can use https://graph.api.smartthings.com/api/devices/?max=10000 and get something like what I need. I would need to be able to do it to all devices and dump the results into csv, text, etc… Can anyone help me out? Even where to start? I have read the documentation and have tried modifying various existing SmartApps to test it. Most focus on exporting a single event and its attributes. I am looking instead to do a bulk export of all events available for listed devices. Any help is appreciated. Can it be done as a Python script? The oAuth in Python was returning 401-2 errors anytime I tried the simplest tutorial for this. Thank you in advance.

SmartThings doesn’t really want to “encourage” bulk exports, IMHO, but that’s a different story.

The method I recommend for this (though not necessarily optimal or most convenient) is to write a SmartApp that uses preferences inputs to allow user to select desired devices (you get most of them with the filters “Sensor” and “Actuator”) and then subscribe to all their Events.

Upon receiving an event use http/REST-API to immediately (or upon buffering a batch) send to a logging server or service.

If you have a commercial requirement and budget, let me know.

I’m not sure exactly what you’re looking for, but you might take a look at the logging projects and see if anything there is of interest. Look in the project report section of the quick browse lists in the community – created wiki and then on the “logging” list to see what other people have done.

http://thingsthataresmart.wiki/index.php?title=How_to_Quick_Browse_the_Community-Created_SmartApps_Forum_Section#Quick_Browse_Links_for_Project_Reports.2FQuestions

1 Like

Thanks for the quick response! Every Logging SmartApp taht I have seen focuses on individual events logged as they occur. Ideally what I would want to be able to do is use a Python script to get all the stored events for devices. I’ve used a script like it to access other applications API’s. I understand that it might have a hard time without a SmartApp to provide oAuth. Even trying to use the myDevice.eventsBetween() and setting the days for 7 is not working.

You can generate an Access Token on demand in a SmartApp and send it to a server to, in turn, trigger the SmartApp endpoint. Or, if you log into the IDE, it will give access to the endpoint without a token. So OAuth complications are not required unless this needs to be fully automated.

Have you searched the GitHub public repo for uses of “eventsBetween()”? It’s always good to start with working code and then incrementally change it, test each iteration, until it breaks.

If you want your code reviewed, publish it on GitHub (or at least a Gist) and share the link here. But we kinda expect / hope you reduce it to a single point of failure first. I have successfully used event history queries and they work fine.

NB: SmartThings stores at most 7 days of events and does not guarantee that. If you want a higher probability of not missing Events, then using the subscribe and push methods is much more reliable.

Thanks for the help. Maybe you can help me with this bit. All I’m trying to do right now is modify the WebApp tutorial to provide me a list of events. So where they use def listSwitches() { def resp = [] switches.each { resp << [name: it.displayName, value: it.currentValue("switch")] } return resp } instead of [name: it.displayName, value: it.currentValue("switch")] I would like something like [switches.events(max: 200)]. I have tried doing the same and modifying to fit other properties of “switches” (id, Label, etc…) and it has worked when accessed through the url defined in the tutorial. The problem comes in when any “events” properties (events(), eventsBetweeen(), etc…) are accessed and I get a generic 500 Internal Server error. I believe it is a formatting issue. I have looked at the documentation and other codes, and have found no examples formatting a variable containing event entries. Any ideas?

Without seeing your full code (well… and offering to rewrite it for free, which I ain’t :stuck_out_tongue_winking_eye:), then all I can offer are code snips.

This is a working method:

def getEventsOfDevice(device) {
	def today = new Date()
	def then = timeToday(today.format("HH:mm"), TimeZone.getTimeZone('UTC')) - 1
	device.eventsBetween(then, today, [max: 200])?.findAll{"$it.source" == "DEVICE"}?.collect{[description: it.description, descriptionText: it.descriptionText, displayName: it.displayName, date: it.date, name: it.name, unit: it.unit, source: it.source, value: it.value]}
}