Reading Device Properties in a Smart App

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

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

1 Like

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

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

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.

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

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

@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?

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}"
}

@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.

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 ()}”

@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

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.