This is driving me crazy, I feel like I’m missing something simple here.
I want a contact or motion sensor or whatever that is not directly a physical device and using the REST API endpoints send a command to modify the “state” of any of those non-physical devices. I have tried numerous things in the simulator and on my iPhone via the ST app.
Here is the scenario and maybe I’m just missing something simple?
Create a new device type
-> Call it “DSC Zone”
-> Select “Contact Sensor”
-> Leave everything else at default and click “Create”
-> Select the browse code box on the right and choose “Open/Close Sensor”, don’t make any changes to the code and click “Save”.
Create a new device
-> Call it “Zone1”
-> Assign a fake network ID of your choosing, I’m using “ZONE1” for example
-> Click the Type drop down and choose the type we created above “DSC Zone”
-> Click Location “Home” (or whatever locations you have)
-> Assign to a Hub or Don’t assign to a hub? (I have found various posts saying to not assign it to the Hub and I have tried it both ways many times without any differences)
-> Leave group alone (unless you want to group it?) and click “Create”
Now you have a Device called “Zone1” that shows up in your iOS or Android app and on the WebUI.
Create a new app
-> Call it “DSC Test”
-> Give it a description “test”
-> Pick category “My Apps”
-> Click “Enable OAuth in Smart App” and write down id’s
-> Click “Create”
-> Paste in the code below
-> When done click save and then Publish “For Me”
On iPhone or Android -> Add new app -> My Apps -> "DSC Test"
For your contact device pick the device you created above “Zone1” click install
You now have the app and your device, the device by default says “open” and shows the open icon.
Try to change it by sending the proper REST API:
https://graph.api.smartthings.com/api/smartapps/installations/${appID}/contacts/${contactID}/closed?access_code=${accessCode}
I have all this working properly so that I see the events in the WebUI log and on the iPhone activity log but no matter what attribute I ask for or try to change the device “Zone1” icon always says Open. I have also tried using motion sensors (also not real ones) and can’t change those either. The only successful device I changed was a fake switch using the device.on() and device.off() functions of a fake switch. That updated the tile properly but nothing using attributes ever updates.
Is it impossible to change the attributes/status of something that is not an actual Z-wave or Zigbee device?
Seems like there should be a simple way to change it’s attributes/status but I can’t figure out how.
How does a real device get it’s tile to change?
Note: Most of this code is taken directly from the REST API endpoint tutorial from Smartthings, all of the other bits I have found in various forum postings and just tried them to see if any of it would work. None of this code throws any errors but the tile(s) belonging to the devices you pick never change their status.
preferences {
section("Allow Endpoint to Control These Things...") {
input "contacts", "capability.contactSensor ", title: "Which Doors?", multiple: true, required: false
}
}
mappings {
path("/contacts") {
action: [
GET: "listContacts"
]
}
path("/contacts/:id") {
action: [
GET: "showContacts"
]
}
path("/contacts/:id/:command") {
action: [
GET: "updateContact"
]
}
}
def installed() {}
def updated() {}
def listContacts() {
contacts.collect{device(it,"contact")}
}
def showContacts() {
show(contacts, "contact")
}
void updateContact() {
update(contacts)
}
private List update(devices) {
def results = []
log.debug "update, request: params: ${params}, devices: $devices.id"
def command = params.command
if (command) {
def device = devices.find { it.id == params.id }
log.debug "Device: ${device}"
if (!device) {
httpError(404, "Device not found")
} else {
log.debug "Device: $device"
log.debug "Command: $command"
log.debug "Caps: $device.supportedAttributes"
if(command == "closed") {
def latestState = device.latestState("contact")
log.debug "latestState value: $latestState"
def currentState = device.currentState()
log.debug "currentState value: $currentState"
currentState = 'closed'
results << createEvent(name: "contact", value: "closed")
results << createEvent(name: "status", value: "closed")
log.debug "Status Closed"
}
}
}
results
}
private show(devices, type) {
def device = devices.find { it.id == params.id }
if (!device) {
httpError(404, "This Device not found")
}
else {
def supportedAttributes = device.supportedAttributes
def attributes = []
def a = ""
for(attribute in supportedAttributes){
a = device.currentState(attribute.toString())
if ( a != null)
attributes += [attribute: a]
}
[id: device.id, name: device.name, label: device.displayName, attributes: attributes]
}
}
private device(it, type) {
it ? [id: it.id, label: it.label, type: type] : null
}