Trouble calling a function with two parameters via API end point

Hi all,

Brand new to this, so please excuse any guffaws (and edumacate me!) —

I’ve been toying with an app that will help to refresh some virtual sensors (actually wireless sensors for my alarm system, added by custom device handler via @QCCowboy ) since the handler doesn’t have real time polling built in due to the platform limitations.

This is the code for my SmartApp, of which I’m able to get all the sensors listed, but when I click on the button to execute /contacts/:id/:refresh it doesn’t do anything. I haven’t even been able to get my logging messages to come up from the update function.

I built it off the bones from a thread that had a walk through to create an API-enabled SmartApp.

My SmartApp Code:

preferences {
	section("Sensors to force refresh...") {
		input "contacts", "capability.contactSensor", title: "Which sensors?", multiple: true
	}
}

mappings {

	path("/contacts") {
		action: [
			GET: "listContacts"
		]
	}
	path("/contacts/:id") {
		action: [
			GET: "showContact"
		]
	}
	path("/contacts/:id/:command") {
		action: [
			GET: "updateContact"
		]
	}  
    
}

def installed() {
	subscribe(contacts, "refresh", deviceHandlerRefresh)
}

def updated() {}

//contact sensors
def listContacts() {
    log.debug "listing sensors for TotalConnect 2.0"
	contacts.collect{device(it,"contact")}
}

def showContact() {
	show(contacts, "contact")
}
def updateContact() {
	log.debug "starting update, request: params: ${params}, devices: $devices.id"
	update(contacts)
}

def deviceHandlerRefresh(evt) {
    if (evt.value == "open") {
        log.debug "sensor is open!"
    } else if (evt.value == "closed") {
        log.debug "sensor is open!"
    }
}

private void update(devices) {
	log.debug "update, request: params: ${params}, devices: $devices.id"

		def device = devices.find { it.id == params.id }
		if (!device) {
			httpError(404, "Device not found")
		} else {
            	device.refresh()
                log.debug "device.displayName"
		[id: device.id, label: device.displayName, value: s, status: device.contactState]
}

    
}

private show(devices, type) {
	log.debug "listing TotalConnect 2.0 sensors"
	def device = devices.find { it.id == params.id }
	if (!device) {
		httpError(404, "Device not found")
	}
	else {
		def attributeName = type == "motionSensor" ? "motion" : type
		def s = device.contactState
        log.debug "device.displayName"
		[id: device.id, label: device.displayName, value: s, type: type]
	}
}


private device(it, type) {
	it ? [id: it.id, label: it.label, value: it.contactState, type: type] : null
}


This is the output of my web-facing script, which outputs each sensor that was selected in the app’s options via smartphone. Any idea why the refresh function isn’t getting called?

How are you passing the deviceID in query to the endpoint?

Hi Tim,

This is the link I’m currently generating: http://graph.api.smartthings.com/contacts/378c966e-7205-4c26-b7b9-ade093c13a42/refresh?access_token={token}

I’m now going to kick myself because I see the error.

Well I’m glad you got it figured out :slight_smile:

Thank you guys for having an infrastructure to help new SmartThings users! :smile:

It’s now hitting everything it’s supposed to hit.

3 Likes