Driver and app code examples

Thanks for the pointers. Getting closer. Just need to read more and peck away... Groovy is just different enough from other languages I use that I need to simply spend some time with the syntax.

1 Like

@gavincampbell Finished my vent/temp balancing app, seems to be working well.

I appreciate your help. It is amazing what just a few pointers in the right direction can do in terms of moving things forward.

I still have a number of things to look into/read up on to make writing the next app a little more efficient, but full steam ahead. For instance I figured out the absolute value function, but didn't see a MIN or MAX function so just did it in conditional code. There may be a few other math functions that would speed up coding versus doing it the long way.

Thanks again!

1 Like

I remember seeing that in one of my drivers... this should help.

def onTime = Math.max(Math.min(onTime, 720), 8)

I've learned a lot by pulling apart other apps/drivers. I've also rewritten some of them as I found certain drivers were just done really bad and weren't working properly and some apps I wanted to add more features too. Most of my stuff is all custom written now and I know it inside out. I keep the features basic and add as I need.

The next thing you will probably want to look at is subscribing to device events. This is the fun stuff. Having methods launch when your contact is opened or a switch is turned on. Then you can get really creative.

Just curious -

Anyone have a preferred groovy editor? I've just been using Notepad++ with a groovy language plugin. It works, but was curious what others use.

emacs :smile: ... my text editor for everything, even docs LOL.

3 Likes

I'd recommend IntelliJ from Jetbrains - the community edition is free and supports Groovy

2 Likes

I just do it right in the web interface. Works well for me and is easy to quickly save and test code.

Its missing a few small features (I think there is no search and replace) but if I have to I just cut and paste to notepad and do the replace and paste it back.

1 Like

Example code can be useful when someone else has already done what Iā€™m trying to do, but it canā€™t substitute for full documentation when I need to do something new.

Specifically, Iā€™m trying to adapt a Cooper Aspire Scene Controller driver so that instead of associating with scene-capable devices, it sends button events to the hub. So far Iā€™ve managed to get it to log button events (in the form of BasicSet and SceneActivationSet commands,) but I have no idea how to tell it to send these events to the hub.

For either of these types of commands the driver executes the following statements:

cmds << response(zwave.indicatorV1.indicatorGet())
sendHubCommand(cmds)

The ā€˜indicatorGetā€™ method is called in many places throughout the driver, but it isnā€™t defined there so I have no idea what it does. The ā€˜responseā€™ method isnā€™t used anywhere else or defined anywhere; I put in some log.debug statements and found that this apparently returns the number 8702 regardless of which indicators are lit on the switches. The ā€˜sendHubCommandā€™ also isnā€™t defined anywhere, but I assume thatā€™s something built into Hubitat ā€” itā€™s mentioned in a community post from last October, but it isnā€™t documented.

I have a Simple Lighting rule thatā€™s supposed to trigger when the first button on this scene controller is pressed. It isnā€™t doing anything, even though I can see the event being generated in the driver. So Iā€™m pretty certain itā€™s a matter of sending the right command to the hub, but I have no idea where to start.

This is just a specific example. My point is to illustrate how the lack of adequate documentation hinders development, and that just relying on public driver code as examples is insufficient.

1 Like

Most, if not all of these can be found in the SmartThings developer docs API Documentation ā€” SmartThings Classic Developer Documentation

The ZWave stuff requires you to log in with a Samsung or Smartthings account

Is there any hope of getting generated docs for Hubitat instead of looking at the ST docs and hoping they are right? Since this is all groovy having auto-generated groovydoc for each release would be WONDERFUL. I don't even need comments, just knowing all the method signatures would be huge.

1 Like

Have you looked at the developer documentation at the following? It's not 100% complete, but compared to a year ago, Hubitat has come a very long way, IMHO.

https://docs.hubitat.com/index.php?title=Hubitat_Elevation_Documentation

Also, the Hubitat team welcomes help from the community to improve and complete the documentation.

Sure, there is some stuff there but the vast majority of the API has no docs.

For example the Hub Update 2.0.3 release notes say that HubMultiAction has been added. Searching the wiki for that class gets me to Driver Object but it just mentions HubMultiAction as a return type with no information on how to use it.

Even HubAction isn't really documented. I have to go look at the ST docs to get an idea of how to use the API: HubAction ā€” SmartThings Classic Developer Documentation

1 Like

I understand. Did you come from SmartThings originally? Or another platform? Or is Hubitat your first home automation hub?

For many of us that are SmartThings refugees, we sometimes take for granted our previous experience on that platform with the very similar API. I still pop back over to ST to check their documentation from time to time.

I can say that if you have a specific issue, which the community cannot help with, the Hubitat staff is usually very quick to lend their expertise to help developers sort things out. Just tag @mike.maxwell (drivers), @bravenel (apps), @patrick (dashboard), @bobbyD (support), or @chuck.schwer (platform). They are a great team to work with! :sunglasses:

Welcome to Hubitat!

Coming from ST and more so coming from ~20 years of open source software development. I just feel bad poking on here constantly when I run into things that I feel like I could sort out if I could dig into the API docs.

Currently I'm trying to figure out why my ported ST driver threw an error. I posted a dedicated thread about it: Modify sendHubCommand from ST?

2 Likes

Sorry to ask a stupid question but...

Are you saying that the Apps code is the Groovy language and that if I study the documentation for Groovy then I'd be good to go?

I'd like to write some Apps and I'm looking for useful stuff to read before diving in.

I've found good documentation on Groovy (a simple Google search brings that up), and I'm not looking for some worked examples to go with it.

Any recommendations would be most welcome.

Ya they are written in groovy.

If anything get familiar with the groovy syntax and the layout of the app structure. Then start looking at code for existing apps to understand how they work. Look for something simple.

The example I gave above is a very basic example. You can then start to add your own inputs and act on them from there.

1 Like

Thanks Gavin. Good advice, which I shall follow.

Looking for sample driver that can open local end-point URL to listen for requests (GET/POST) from LAN, any references?

You'll want to use asyncHTTP, I assume....

def poll() {
	def requestParams = [ uri: "https://api.apixu.com/v1/forecast.json?key=$apixuKey&q=$zipCode&days=3" ]
	// log.debug "Poll ApiXU: $requestParams"
	asynchttpGet("pollHandler", requestParams)
}

/*
	pollHandler
	Purpose: the APIXU website response
	Notes: a good response will be processed by doPoll()
*/
def pollHandler(resp, data) {
	if(resp.getStatus() == 200 || resp.getStatus() == 207) {
		obs = parseJson(resp.data)
    //    	if (debugOutput) log.debug "wx-ApiXU returned: $obs"
		doPoll(obs)		// parse the data returned by ApiXU
	} else {
		log.error "wx-ApiXU weather api did not return data"
	}
}

A driver can not open a listener of UDP/TCP/HTTP...

You will need to look at the Maker API application and documentation HERE