Events and Subscribing

I'm working on coding a device and an app to use it.

How do I create my own events in the device code that the app (or whatever) that is using the device can subscribed to?

Thanks, Frederick

Depending on the protocol and how your driver is written, there are a couple different options. But one that works regardless of any of that is calling sendEvent() youself, as documented here: Driver Object | Hubitat Documentation

If you are brand new to driver development, I would suggest looking at the explanation for a simple virtual driver here: Driver Overview | Hubitat Documentation (and the general overview of the environment itself if you have not already).

Thanks much.

I looked at the sample code and saw the sendEvents using the name of "switch".

But there is no explicit "attribute" specified in the code, as I have seen on other code.

Does the reference to the capability "Switch" somehow result in attributes being "added" to my device?

Frederick

Yes, you get all attributes specified by the the capability for "free" (if there are any, as there are for most capabilities). Those are listed for each capability here: Driver Capability List | Hubitat Documentation

You only need things like attribute "myAttributeName" if you are declaring a custom (i.e., not part of any capability) attribute.

The same applies to commands, should you get to that point.

1 Like

OK I think I've got things sorted, more or less.

For testing, I added to my device code an attribute and a sendEvent that referenced that attribute.

The sendEvent was being executed as verified by some log entries created in my code.

BUT the subscription I added to my app code wasn't working.

I was doing the subscribing in the apps updated() method.

I discovered that just editing and saving my app code was NOT calling the updated() method so the new "subscriptions" were not happening.

Opening the app and clicking the "Done" button solved the problem.

Is there anyway to more easily to force the required updating whenever I edit/save my app code?

Thanks, Frederick

No, nothing gets called when your app code is saved. Hitting "Done" makes updated() run and is probably the easiest way, though you could also call this method yourself anywhere in your app code that makes sense.

1 Like

Thanks. It all is starting to make sense.

My app and device driver now seem to be working, basically.

Frederick