Is there a way to get a reference to the SmartApp that called a command on my device type handler?

I’m looking to get a reference to the SmartApp that called a command on my device type handler so that I can get it’s “app” object to reference the id (vs. having to ask people to pass that id in each time the command is called).

I went around in circles trying to leverage Java and Groovy solutions, but they are all blocked by the ST flavor of the language.

1 Like
  1. Curious to know “why” (i.e., as an analyst, I really like to understand the underlying problem / use case; because it might lead to an outside-the-box solution).

  2. My only off-the-cuff guess is to see if there is any information you can get from querying the Event log?

Yah for sure!

  1. The idea is I have a device type that will take messages from a number of SmartApps. The device type will manage those messages to automatically expire old ones from the same SmartApp. eg. if you opened and closed your front door 5 times, and you left your garage door open (2 different apps), the last message about the front door would be the one that stuck around since it would’ve expired the previous ones.

  2. Not super sure how to answer this one other than under “SOURCE” for the device I see “COMMAND” and “APP_COMMAND”. The live logging also doesn’t help me much in this department either. Maybe there is a method you’re talking about that exposes more data, but I’m not sure how to view it!

  1. Hmmm… I gotta think about your use case and figure out what ideas / questions I have.

  2. Meanwhile, the Event object does seem to have installedAppId in it, but not sure of the context. i.e., Is this stored in the Device Event as it receives the Command? Maybe…?

http://docs.smartthings.com/en/latest/ref-docs/event-ref.html

http://docs.smartthings.com/en/latest/ref-docs/device-ref.html#eventsbetween

Is there a hidden parameter within a command call? I thought it had to be defined in the DTH: command "message", ["string"] and I don’t see a way for me to pass any sort of event to the command itself.

The Event occurs intrinsically (or doesn’t occur at all).

ie, I believe, but not documented or tested, that every Command call fires an Event in the Device Object. The Notification page seems to indicate this… I’ll try to find an example.

But you could also see what types of events are returned if you immediately query inside the Command method.

I’m not sure what you mean by this or how I would go about getting info of an event within the Command method.

Appreciate you having a hunt. I tried searching as well, but I have a feeling that the terms I’m using aren’t correct so I’m coming up with nothing.

Call the events() related method(s) Documented here: http://docs.smartthings.com/en/latest/ref-docs/device-ref.html#events

(I apologize for not being more detailed; but I’m posting quick hints between tasks, and I’m also a “teach a man to fish” kinda guy… :angel:).

No no, this is my preferred style of learning :smiley:

Consider this particular fish caught :wink:

def last_event = device.events(max: 1)
log.trace "last event app id: ${last_event.installedSmartAppId}"

It’s showing up like [UUID_HERE] vs UUID_HERE, but I can keep working with that.

Thanks for your finger pointing in the right direction stuff!!

1 Like

Well getting it as a string was as easy as I thought:

def device_events = device.events(max: 1)
log.trace "last event app id: ${device_events[0].installedSmartAppId}"

Not sure why it’s an array, but oh well! works fine now!

edit: It was bugging me why the code was working, so I went back and re-read it and came up with the correct solution that I edited above. The reason is that that device.events returns a list, even though I only wanted 1, it’s still a list! So moving the index selector to device_events made the code work AND make sense.

1 Like