DH: Trigger Event, Groovy APP: Subscribe to Event

What do I need to do within a DH to trigger an event that I can capture within an app?
For example:
Within the DH. a device state (ie: devRpmStatus) changes from slow to fast
Within the app I would include subscribe(theDevice,"devRpmStatus", myEvntFunct)

Hope that makes sense

First, since devRpmStatus is not a "standard" attribute (part of any capability), you'll need to make sure that you've declared it as a custom attribute in your metadata, e.g.,:

metadata {
   definition(/* ... */) {
   // ... 
   attribute "devRpmStatus", "NUMBER"
   // ...

To create an event, you can use sendEvent() anywhere in the driver, which in a very simple form could look something like:

sendEvent(name:"devRpmStatus", value: 120)

I can't find any formal Hubitat documentation on the rest of what you can pass to sendEvent(), but the SmartThings docs are pretty close aside from the fact that Hubitat does not use displayed or linkText. But the only other parameters/keys I think I've ever used are unit (mostly for standard attributes that expect these, like "K" for color temperature), isStateChange (the platform filters out events where the new value is the same as the old one; making this true generates one regardless, though except for certain kinds of devices or attributes the default behavior is probably desirable), and descriptionText, which is sometimes helpful for creating "friendly" event descriptions in the "Events" page and, if you choose, the logs. For example:

String eventName = "devRpmStatus"
Integer eventValue = 120
String descText = "$eventName is $eventValue"
sendEvent(name: eventName, value: eventValue, descriptionText: descText)
if (logDesc) log.info descText

That last piece is probably a bit more than you asked for, but it's convention for Hubitat drivers to log event description text to info logs if the user has a setting enabled for this, often labeled something like "Enable descriptionText logging" (that is on by default)--not to be confused with debug logging that normally shows what the driver is parsing and only stays enabled, again just by convention, for 30 minutes and is a separate option. Not a big deal if you're making the driver for yourself--do whatever you want then--but lots of Hubitat users are picky about logging otherwise. :slight_smile: You can see some examples in Hubitat's public repo: HubitatPublic/examples/drivers at master · hubitat/HubitatPublic · GitHub

Back to your original question and adding on to my answer just a bit, you might also be able to use createEvent(), but this only actually generates the event if the object returned from this method is returned as part of parse() (common in Z-Wave and Zigbee drivers but not required; sendEvent() works anywhere). Some people just ignore it entirely, and I'm one of them.

Hope this helps!

1 Like

That worked like a charm once I figured out that the DH I was modifying was not the same as the DH assigned to the device I was working with :blush: Drove me nuts for about an hour. :rage:Nothing seemed to make a difference and then...... :clap:

Thanks for your help