Custom Events

Is it possible to create custom events to establish communication between smartapps ?

Use case: let’s say I create a smartapp to manage groups of events (e.g. motion on any of these sensors) and generate a synthetic event based on any of said device publishing one. Then I would have other smartapps subscrbing to this custom event to take action e.g. one smartapp could switch on lights whilst another one would compute statistics.

I think many people would just do that with a virtual switch. Turn on the virtual switch with the first smartapp and subscribe to that switch coming on in the second Smartapp.

There are probably other ways to do it as well, so hopefully some of the master coders will comment. But the virtual switch method is pretty simple.

2 Likes

Thanks JDRoberts. I also found this reference now that you’ve directed on this interesting track : FAQ: Creating a virtual Device

1 Like

It’s undocumented (and thus could be deprecated at any time), but you can create arbitrary Location Events.

That is an unofficial way for SmartApp to SmartApp communication.

Creating Virtual Device instances fits the SmartThings paradigm and is more secure.

2 Likes

Thanks tgauchat.

Just wondering if this actually works or what I am doing wrong.

Smartapp A issues without error
def locevent = [name:“shmdelay”, value: “foobar”,
displayed: true, descriptionText: “Issue entry delay foobar”, linkText: “Issue entry delay foobar event”]
sendLocationEvent(locevent)

Smartapp B contains, but never executes the foobarHandler (no debug message, good subsciption, no errors)
subscribe(location, “shmdelay”, foobarHandler)

def foobarHandler(evt)
{
log.debug(“foobarHandler entered, event: ${evt.value}”)

I haven’t used it, personally, and, since undocumented … I’d never assume it works. Check the open source of webCoRE, perhaps, for uses?

http://docs.smartthings.com/en/latest/smartapp-developers-guide/simple-event-handler-smartapps.html#subscribe-to-location-events

But one thing to try… Be try setting the isStateChange parameter of sendLocationEvent to true?

Thank you for that suggestion. I was hopeful, but it did not work. I’ll take a look at the WebCore source. The Smartapp(s) I plan to execute are all children of the main app, so I can simply get them all via the parent, then execute the target routine versus using sendLocalEvent. Not nearly as elegent, but it will work.

Edit: Whoops a deeper review of the logs shows the subscribe did work, but I had a spelling error on the value test. Thanks!!! :joy:

1 Like