API and Endpoints

Hi all,

It would be nice to see how folks are setting up endpoints within their environments.

Iā€™m wondering if someone can help me setup integration with Tasker using endpoints? Canā€™t seem to find a tutorial anywhere explaining how to enable that functionality.

Thanks,

I had a lengthy Project write-up on how to make this all workā€¦looks like it got wiped out with the new forums. :frowning:

Yeah I havenā€™t been here to the new forum site yet so Iā€™m still trying to understand how to get around and where to look for things. Maybe your write-up is somewhere here: http://community.smartthings.com/category/archive-migrated-from-old-forums?

Hereā€™s a string where you talked about the write-up, but the link doesnā€™t work anymoreā€¦

Yeah, seems as though the write-up is gone. It was a 3-parter, so Iā€™m not sure I want to go through the effort to re-write it here. Would have been nice if ST had archived those somewhere.

Damn, could you by chance let me know what the first steps were? If I recall correctly, there was just a link to click on to enable OAuth or something, and then it asked to check the box for each device you want to control. Then somewhere in the process it gave you the URL to use to view the devices and their control URLs. I guess the link to enable OAuth is really all I need as the rest I can probably figure out.

Yup, its http://labs.smartthings.com/exampleOauth.php

Let me know if you run into trouble after that.

1 Like

Awesome! That was exactly what I needed. Thanks csader!

Hey csader, another quick question about thisā€¦ do you know if itā€™s possible to run a SmartApp by use of endpoints? I see you can toggle switches, unlock doors, etc., but Iā€™m wondering if you can run a SmartApp by doing an HTTP Get task through Tasker.

Thanks

@triggertact yes, you can. Itā€™s been a few months since I did ST development, but yes and you can pass things in the URL and I think I read somewhere that it would take a JSON object (but I never verified that). In the app you have a mapping section like:

   mappings {
      path("/panel/:eventcode/:zoneorpart") {
        action: [
          GET: "updateZoneOrPartition"
        ]
      }
    }

where ā€œupdateZoneOrPartitionā€ is a function. If you check out the app portion of this repo

https://github.com/kholloway/smartthings-dsc-alarm

youā€™ll get an idea of how it works. If youā€™re asking what I think you are, that isā€¦

Hmm, Iā€™m not sure if this is or isnā€™t what Iā€™m looking for. Iā€™m real novice when it comes to the coding side of SmartThings. What Iā€™m mainly concerned with is when I run through the process of Authorizing Endpoint Access here: http://labs.smartthings.com/exampleOauth.php that @csader just referenced above, it only seems to allow me to check boxes next to devices but not any smartapps.

In a separate forum string, Iā€™m trying to figure out how to create a smartapp that has the little play button on its icon so I can activate it manually by pressing the app button, rather than having the app activate itself by way of some automated trigger specified within. The use case for this would be if I wanted to trigger a smartapp for a scenario not able to be planned for. For example, if I want to have all my lights dim when I want to watch a movie I would like to just manually activate the smartapp (or use AutoVoice/Tasker).

So, in this string, Iā€™m trying to determine if I can have this smartapp be checkmarked at this Authorize Endpoint Access webpage so it can also be controlled through Tasker like my devices can. Iā€™m wondering if your previous answer is indeed telling me that it can, but that it will take some coding to setup?

Hey guus, just wondering if anyone had any ideas regarding my last post. Hopefully it made sense! Basically, Iā€™m just trying to activate a SmartApp using HTTP Get through Tasker/AutoVoice.

Thanks in advance for any suggestions!

I dont have much help here, but I would like to see something ā€œofficialā€ in the way of using the the OAuth.php to call particular apps. http://labs.smartthings.com/exampleOauth.php ā€“ while great, I need a bit more functionality besides toggle/on/off. I know it can be doneā€¦ but like others here, it appears Iā€™m missing one little step (mostly, in addressing how I call the OAuth Client and ID).

I think it would be wise (from an end user point of view) for SmartThings to really push this feature or help users get this specific aspect working. A little show and tell would go a long, long way. Iā€™m pretty sure HomeKit is going to have a voice action feature to ā€œturn on/off/dimā€, etc., and I can only imagine what will come at I/O this year (and possibly make good on the Android@Home).

Without much more banter, I hope someone at SmartThings can hook up a show-n-tell on how to call custom SmartApps over HTTPS. Users seem to want it, the functionality is thereā€¦

Thanks!

1 Like

@triggertact - take a look at my post on creating virtual switches that execute smartapps -

This should help lead you in the right direction.

2 Likes

@nelemansc Oh very cool! Iā€™m definitely a novice when it comes to the code side of things. That said, is there an easy way to explain how to manipulate the code you created to execute SmartApps, rather than change modes?

Yup, you just need to add this to the preferences section of the Smartapp

    {
	section("Select Switch to monitor"){
		input "theSwitch", "capability.switch"
	}
}

Followed by initializing the on/off handlers with this

def initialize() {
	subscribe(theSwitch, "switch.On", onHandler)
    subscribe(theSwitch, "switch.Off", offHandler)
}

If youā€™d like help, go ahead and copy/paste the code for your Smartapp and Iā€™ll edit it for you

Ok cool, Iā€™m basically using the Make It So appā€¦ I pasted the two sections of code in that you provided, and I also added the virtual on/off switch as explained in your write-up. Then after installing the app to my phone, I selected this new virtual switch as the ā€œSwitch to Monitorā€ in the app, but when I turn the switch on itā€™s not executing the app. The app does execute when you touch its play button, though.

Make sure you also have the initialize in the def installed & def updated groupings

def installed() {
	subscribe(location, changedLocationMode)
	subscribe(app, appTouch)
	saveState()
    initialize()
}

def updated() {
	unsubscribe()
	subscribe(location, changedLocationMode)
	subscribe(app, appTouch)
	saveState()
    initialize()
}

Hmmm, just added that in as well but still not executing when turning on the virtual switch. Hereā€™s the full code Iā€™m working with:

definition(
    name: "Movie Lights",
    namespace: "smartthings",
    author: "SmartThings",
    description: "Saves the states of a specified set switches and thermostat setpoints and restores them at each mode change. To use 1) Set the mode, 2) Change switches and setpoint to where you want them for that mode, and 3) Install or update the app. Changing to that mode or touching the app will set the devices to the saved state.",
    category: "My Apps",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/light_thermo-switch.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/light_thermo-switch@2x.png"
)

preferences {
	section("Switches") {
		input "switches", "capability.switch", multiple: true, required: false
	}
	section("Thermostats") {
		input "thermostats", "capability.thermostat", multiple: true, required: false
	}
	section("Locks") {
		input "locks", "capability.lock", multiple: true, required: false
	}
	section("Select Switch to monitor"){
		input "theSwitch", "capability.switch"
	}
}

def initialize() {
	subscribe(theSwitch, "switch.On", onHandler)
    subscribe(theSwitch, "switch.Off", offHandler)
}

def installed() {
	subscribe(location, changedLocationMode)
	subscribe(app, appTouch)
	saveState()
    initialize()
}

def updated() {
	unsubscribe()
	subscribe(location, changedLocationMode)
	subscribe(app, appTouch)
	saveState()
    initialize()
}

def appTouch(evt)
{
	restoreState(currentMode)
}

def changedLocationMode(evt)
{
	restoreState(evt.value)
}

private restoreState(mode)
{
	log.info "restoring state for mode '$mode'"
	def map = state[mode] ?: [:]
	switches?.each {
		def value = map[it.id]
		if (value?.switch == "on") {
			def level = value.level
			if (level) {
				log.debug "setting $it.label level to $level"
				it.setLevel(level)
			}
			else {
				log.debug "turning $it.label on"
				it.on()
			}
		}
		else if (value?.switch == "off") {
			log.debug "turning $it.label off"
			it.off()
		}
	}

	thermostats?.each {
		def value = map[it.id]
		if (value?.coolingSetpoint) {
			log.debug "coolingSetpoint = $value.coolingSetpoint"
			it.setCoolingSetpoint(value.coolingSetpoint)
		}
		if (value?.heatingSetpoint) {
			log.debug "heatingSetpoint = $value.heatingSetpoint"
			it.setHeatingSetpoint(value.heatingSetpoint)
		}
	}

	locks?.each {
		def value = map[it.id]
		if (value) {
			if (value?.locked) {
				it.lock()
			}
			else {
				it.unlock()
			}
		}
	}
}


private saveState()
{
	def mode = currentMode
	def map = state[mode] ?: [:]

	switches?.each {
		map[it.id] = [switch: it.currentSwitch, level: it.currentLevel]
	}

	thermostats?.each {
		map[it.id] = [coolingSetpoint: it.currentCoolingSetpoint, heatingSetpoint: it.currentHeatingSetpoint]
	}

	locks?.each {
		map[it.id] = [locked: it.currentLock == "locked"]
	}

	state[mode] = map
	log.debug "saved state for mode ${mode}: ${state[mode]}"
	log.debug "state: $state"
}

private getCurrentMode()
{
	location.mode ?: "_none_"
}