Reading Device Properties in a Smart App


(Adam Seed) #1

Can someone tell me how I can read a property set in a device in a smartapp?

In my device I have
input("room", "text", title: "Room", description: "Room Name", required: false, displayDuringSetup: true)

which I would like to read in my event handler:
def switchHandler(evt){ log.debug "Switch event: ${evt.name} = ${evt.value}" def d = evt.device if (evt.value == "on") { log.debug "Device: ${evt.device} Event: ${evt.name} Value: ${evt.value} State changed: ${evt.isStateChange()}" log.debug "d.getProperties("room")" }

Thanks


(Stuart Buchanan) #2

you need to subscribe to the events in the smartapp’s init function

def init() {
subscribe(room, “attributeName”, handleEvent)
}

then you can get its value by calling like the below in your app

room.currentValue(“attributeName”)

obviously replace the attributeName with what the value you want.

its better documented here

http://docs.smartthings.com/en/latest/smartapp-developers-guide/simple-event-handler-smartapps.html


(Adam Seed) #3

Hi @Fuzzyligic, thanks for the reply but I dont think I need to subscribe to an event .

The code in my first post gets called based on the switch subscribtion
subscribe(switchList, "switch", switchHandler)

What I want to do is to read the custom “room” attribute (under the preferences section of the device) when the switch event fires on the device and calls the switchHandler on my smartapp


(Stuart Buchanan) #4

then you need a second subscribe statement as that subscribe statement is only looking for the switch attribute, the second subscribe should be looking for the room attribute is an event is generated with that value


(Adam Seed) #5

Correct me if im wrong, but the room custom attribute is never changed (unless the device is edited). When my switch triggers I want to be able to pull out the room property which has been set for the device.

Another important thing to make clear, room is a custom preference attribute - not the room a device is associated to. For arguments sake it could be called ABC.


(James Coyle) #6

I’m after this too - a simple example would be great!


(Jim Anderson) #7

Just refer to it directly by name in your SmartApp, or access it through settings: http://docs.smartthings.com/en/latest/smartapp-developers-guide/preferences-and-settings.html#preferences-overview


(Adam Seed) #8

@Jim Thanks for you response, I had another go at this but still not getting this most basic primes.

In my smart app I subscribe to events:

subscribe(switchList, "switch", switchHandler)

this successfully tirggers my event handler:

def switchHandler(evt)(
    if (evt.value == "on") {
        log.debug "Device: ${evt.device} Event: ${evt.name} Value: ${evt.value} State changed: ${evt.isStateChange()}"
        log.debug "${evt.device.settings.room}"
    } 
    else if (evt.value == "off") {
        log.debug "Device: ${evt.device} Event: ${evt.name} Value: ${evt.value} State changed: ${evt.isStateChange()}"
    }
    else{
    	log.debug "Unexpected event: evt.name: $evt.value"
    }
}

the bit im trying to get loogging is:

log.debug "${evt.device.settings.room}"

but all i get is:
5:36:25 PM: error java.lang.IllegalArgumentException: Property 'settings' is not supported for devices @ line 56

any ideas?


(Jim Anderson) #9

If you’re simply trying to access the value of an input in your preferences, you can simply refer to it by name. If you want to get information about the device that triggered an event handler, the event object has a device property. That lets you get the device that triggered the event (if applicable).

preferences {
   section() {
      // text input named room
      input "room", "text"
      // input for switches
      input "switches", "capability.switch", multiple: true
   }
}

def initialize() { 
   subscribe(switches, "switch", switchHandler) 
}

// called when the "switch" attribute changes state for any of the configured switches
def switchHandler(evt) {
   // if you just want to access the input settings directly:
   log.debug "switches configured: $switches"
   log.debug "room configured: $room"
   // or get out of settings if you wish:
   log.debug "switches configured (accessed via settings): $settings.switches"
   log.debug "room configured (accessed via settings): $settings.room"
   
   // evt.device gets the device object associated with this event. 
   def device = evt.device
   log.debug "device associated with this event: ${evt.device?.displayName}"
}

(Adam Seed) #10

@Jim

Thanks for the swift response. From your code, I understand that I can reference the input in the local preference section, but im trying to reference the input of the evt.device object.

To be more specific, im trying to get the value of an input from a device type in a smart app.


(Mike Maxwell) #11

In the dth:
Create a method that returns the value of the preferences.
Def getRoom {
Return settings.room
}
Add the above method as a command in the definition section
Command “getRoom”

In the smart app:
Log.info “getRoom: ${device.getRoom ()}”


(Adam Seed) #12

@Mike_Maxwell, Thanks for explaining that, it makes sense now to use a standard get/set function. I am however still having issues implementing it.

In my Device Handler I have:

metadata {
	definition (...) {
	capability "Switch"
        capability "Thermostat"
        command "getRoom"
	}  
...

def getRoom(){
	log.debug "called getRoom"
	return "testing123"
}

in my smart app I have:

def switchHandler(evt){
	log.debug "Switch event: ${evt.name} = ${evt.value}"
    log.debug "testing: ${evt.device.getRoom() }"

I’m expecting to see

Device Handler: debug: called getRoom
Smart App: debug: Switch event: switch = on
Smart App: debug: testing: testing123

but I get

Smart App: debug: Switch event: switch = on
Smart App: debug: testing: null

(Mike Maxwell) #13

The form I gave you was meant to be called independently.
The call would be as I’ve shown it, off of the device object, not the event object.
If you want the value to be embedded in an event, it is more compicated, but does not require the command declaration in the dth.
See this:
http://docs.smartthings.com/en/latest/ref-docs/device-handler-ref.html#sendevent
I’ve assumed that you will override an existing event in your dth to include the data attribute.