Driver and app code examples


#1

I would like to code more drivers and apps for HE, but there just isn‘t enough documentation out there. I would prefer not to rely on an existing ST DTH or app by just porting. I know that some things in the ST groovy code doesn‘t apply to HE.
So, considering creating a full documentation is very time consuming, I suggest the HE team should release the source of some of the existing drivers and apps to give some less experienced coders (like myself) examples of how to code for HE. I would recommend release the code for the generic ZigBee and Zwave drivers and maybe one or two simple apps. This could get more in the community going with coding.
I‘m also interested in what type of LAN based protocols/connectivity HE supports as I would like to code support for my router (which also has SmartHome features) that supports TR-064 which is perfect for accessing different status information (smartphone presence, internet connection, SmartHome devices) as well as sending commands.


#2

#3

Ah, thanks. I overlooked this.


#4

I notice the httpGetSwitch example. Does HE also support SOAP? That would be perfect for the above mentioned TR-064 access to my router.


#5

Yup. I believe the wemo switch driver port utilizes SOAP. Should be able to search for wemo connect and find them easily for reference.


#6

Note that this is only example driver code; they have not released any example app code as the original poster also asked about. (That being said, I think there are fewer differences between SmartApps and apps than between DTHs and drivers.)


#7

There's one App in the repository.


#8

That's actually a SmartApp for ST, and it's probably only open-source because that's the easiest way to get it to ST users for installation. :slight_smile:


#9

You're right!


#10

Are there any simple app examples out there?

There are a few webcore things I can't easily do in RM that I think I'll have to write an app for. They aren't that complicated, so I think if I saw a sample app that did something like the following, I could hack my way through it:

  1. Executed on a set schedule (5 minute for example)
  2. Had 3-4 user defined variables - exposed or internal
  3. Read a few device values in
  4. Did some simple math - if X > y, add 10% to level of device
  5. And wrote the new values

Any suggestions? Seems like a pretty simple first app to do, so if anyone has anything remotely similar that would be a big head start for me to start plodding through it.


#11

What are you trying to do? Slowly dim or brighten a light over time?


#12

I had a webcore piston that did room temperature balancing by moving Keen vents. Basically it does the following (and the opposite if HVAC mode is HEATING), but I just listed the cooling logic steps below):

  1. Looked to see if HVAC mode is COOLING
  2. Looked at the temperatures in two rooms
  3. If delta temp > 2 DegF, then start adjusting the vents to balance the temp between the two rooms
  4. Base load the hottest room at 100% vent output
  5. Close off other room vent by x% each execution in which the HVAC mode is COOLING and the delta temp between rooms is >2 DegF
  6. Don't close a vent more than y% (minimum open setting)

Inputs are:

  1. HVAC mode
  2. Vent 1 level
  3. Vent 2 level
  4. Room 1 Temp
  5. Room 2 Temp

Outputs are:

  1. Vent 1 level
  2. Vent 2 level

Variables, or hard coded settings, are:

  1. Min vent position (%)
  2. Delta Temp dead band before acting (DegF)
  3. Vent move size when a move needs to be made (%)

I have screenshots of the ST webcore pistons from before I converted over to HE.


#13

Ahh.. ok. It shouldn't be too hard at all. Just break it down piece by piece and then build it up.

I usually start with a template for my apps like below. Please note that I just wrote this without testing it so I may have missed something but it should give you an idea.

In the section() section you would put any options you would want the user to configure. Such as choosing devices to include (that is the ventDevices) or a number option. I don't know what the capabilities are for the vent device so I put switch in for now but you can change it.

Here is a reference for capabilities: Capabilities Reference — SmartThings Classic Developer Documentation
Here is a reference for the input methods: Preferences and Settings — SmartThings Classic Developer Documentation

There are 3 methods I always include. Installed (run when installed), updated (run when updated) and initialize when I usually just make installed and updated execute. I also include uninstalled as some of my apps may do things to clean up like remove any child devices it created.

To make it run an action every 5 minutes you would included the runEvery5Minutes() method as you see. I usually include an unschedule just to stop it in certain situations. Of course its useless below but just show you know it exists.

Here is a reference for schedule stuff: Scheduling — SmartThings Classic Developer Documentation

The vent logic will probably be in the checkDevices() method. This is where you can do your checking/adjusting. Its hard to say exactly what the code would look like as I don't know the methods for the devices but you could use groovy commands like "each" to loop through ever device and get the temperature etc.

Also log.debug will also show you everything you put in the logs. Valuable for helping to troubleshoot.

Hopefully this will at least get you started.

definition(
  name: "New App Template",
  namespace: "GvnCampbell",
  author: "Gavin Campbell",
  description: "",
  iconUrl: "",
  iconX2Url: "",
  iconX3Url: "")

preferences {
page(name: "pageConfig")
}

def pageConfig() {
dynamicPage(name: "", title: "", install: true, uninstall: true, refreshInterval:0) {
    
  section("") {
	  input(name: "ventDevices", "capability.switch", title: "Trigger Devices", multiple:true)
      input(name: "numberOption", type: "number", defaultValue: "10", range: "1..*", title: "", description: "", required: true)
    }
    
}

def installed() {
  log.debug "installed"
  initialize()
}
def updated() {
  log.debug "updated"
  initialize()
}
def initialize() {
  log.debug "initialize"
  log.debug "ventDevices: " + ventDevices
  log.debug "numberOption: " + numberOption
  unschedule()
  //runEvery5Minutes(checkDevices)
}
def uninstalled() {
  log.debug "uninstalled"
}

def checkDevices() {
  log.debug "checkDevices"
}

#14

That is a tremendous help! Thank you very much!

I agree that it shouldn't be hard to do. I'm not an expert programmer, but certainly no novice either. I'm sure I can get it going as soon as I find the time to attempt it.


#15

After I did my first few apps I got the hang of it. Most of the code between apps are very common. For example I have logging methods and applabel methods that I just copy and paste from a template to make life easy.

I'm pretty sure you will have many more questions once started. Most examples I found in the forums (smartthings/hubitat) and just worked from there. But the base template was always the most confusing to me.


#17

I guess you figured it out. I think I missed a } right before def installed. Part of the preferences.

I didn't test the code so bare with it.


#18

Yup. missing }. :slight_smile:

I got my test app to install. So yay.

I'm having trouble actually fetching a value from a device though... Stupid, I know. So I'll peck away at that....

I was trying to just spit out a value like log.debug "Temp 1 is " + temp1.value

But it is always null


#19

Groovy is a funny language. As I got use to it I started to understand the syntax a lot more. A lot of my coding is basic and I learn something new whenever I try to add more.

You might just want to touch up on groovy syntax so you get an idea, but that example if anything gets you at least started.


#20

I should have waited 2 more minutes before posting. I got it:

log.debug "Temperature #1 = " + temp1.currentValue("temperature")

2018-12-18 08:43:16.322 pm debugTemperature #1 = 72.0


#21

The devices you select in the input are actually a list. So you might have to do something like

ventDevices.each {
    it.currentState("something")
}

This will loop through each device if you select multiple The something depends on the device itself. It could be contact for example.

Here's a reference of some of the device calls: State — SmartThings Classic Developer Documentation