Starting out with a Driver

Hi All,

I have a need/desire to write a driver to query an API. I'm not having a lot of luck with info on starting out. I can look at code that already exists (there's some python code for this API), and I can look at existing drivers, but I'd love some tips for starting down this road. Do you all dev directly on your Hubitat? Are there any good starting guides for explaining the parts of a driver, etc?

Thanks!

1 Like

A good starting point for driver and app development is the original SmartThings docs - most of which still applies to Hubitat - API Documentation — SmartThings Classic Developer Documentation

1 Like

Yup. Looking at those. I guess I'm wondering more from a "how do you actually do it" standpoint? Am I just pasting test code into my Hubitat and seeing what happens, or is everyone testing in the JVM on their computer?

Thankfully it's a pretty simple API call. =)

I did all my development directly on the Hubitat, by pasting code into the Driver section and then iteratively testing/fixing until it did what I wanted. I, too, was starting with a driver written in python. I took an existing user driver that was fairly simple and used it as a guide for what I was trying to do. I also relied heavily on the SmartThings Classic guide linked in another post above. It was an invaluable resource.

Regards,

Bob

1 Like

cool. do you have a link to your driver and the python you based it off? might help make sense of things. =)

Sure. This driver is for a garage door control. It logs on to a website hosted on the controller and grabs a cookie via an HTTP Post. Then it uses that cookie to call other HTTP Gets to control the door and inquire about it's status.

Here is the python link: Python GoGoGate2 Garage Control Driver

Here is my Hubitat Driver: Hubitat Groovy GoGoGate2 Garage Control Driver

I'm happy to help with your effort, so feel free to post the python and/or any questions and I'll do whatever I can to assist.

Regards,

Bob

2 Likes

Okay, need some help.

Started with something simpler. Just pulling stats from a website.

https://github.com/staze/hubitat-gmcmap/blob/master/hubitat-gmcmap.groovy

I keep getting "error occured calling httpget java.lang.NullPointerException: Cannot invoke method remove() on null" and I don't understand why... =/

Thanks!

I pulled your code down and ran it. First I set up an account and got an ID. I can't get your link to return any data, even when I run it in the browser. The error that you're getting is consistent with the fact that your HTTPGet is not returning any value.

If you paste this link into a browser, do you get any data back? If so, what value are you using for the timezone?

Regards,

Bob

Okay, I looked at this a bit further, and you have a couple of issues that are causing your problem, related to how you are building your HTTPGet string, as well as how you are handling the response. There are a lot of ways to do this, but this worked for me, so I'll show you what I did and turn it back over to you.

Please replace your code from line 37 on down with my code. Your code is:

def getParams = [
uri: "http://www.gmcmap.com/historyData-plain.asp?Param_ID=${GeigerID}&timezone=${Timezone}",
requestContentType: "application/json",
]

def refresh() {
try {
httpGet(getParams) { resp -> log.debug resp.json }
} catch(Exception e) {
log.debug "error occured calling httpget ${e}"
}
if (logEnable) log.info resp.body
}

Code to try instead:

def getParams() { "http://www.gmcmap.com/historyData-plain.asp?Param_ID=${GeigerID}&timezone=${Timezone}" }

def refresh() {
def responseBody

try {
	log.debug "Params:  ${getParams()}"
	httpGet(getParams()) { resp -> 
		log.debug resp.getData()
		responseBody = resp.getData()}
} catch(Exception e) {
	log.debug "error occured calling httpget ${e}"
}
if (logEnable) log.info responseBody

}

This returned the JSON object you were looking for, and you should be able to parse it out from there based on the example I gave you previously.

Try this out and let me know how you make out. If you need more assistance, let me know and I'll be happy to help.

EDIT: I decided to take this a little further down the road for you. It now calls the website URL and parses out the json data to the individual values, which are written to the debug log. From this point, you should be able to take that data and assign it to attributes or whatever you plan to do with it from here.

Here is a link to the code in my repository:

gmcGeigerCounterDriverLink

Regards,

Bob

Perfect, thanks!

Now I just need to work on displaying the results. =)

Will post back if I have more questions. The next driver will be more complex. =/

I too am trying to figure out where to start. I have a lighting system that has a serial port on it for control. It's not currently supported by Hubitat. I wrote an app in LUA for the Vera which has worked great on that platform.

I'm trying to determine if I need a driver, or both a driver and an app. I want to be able to put in a list of load ID's on the lighting system, and have the devices all automatically created (this makes me think I also need an app). Manually creating each device is going to take a long time.

Are there any other lighting drivers/apps that magically create all of the devices based on discovery or a user provided list?

You may want to pattern it after the Lutron integration.

My LIFX LAN Protocol driver works in a similar way, probing the local network for responses to commands, it then creates child devices. See GitHub - robheyes/lifxcode

That's perfect. I'll have to grab it anyway since I have a bunch of those bulbs.

Feel free to ask anything about the code if you need to.

1 Like

Nice thread. I am also starting on driver development.