Subscribing to driver events?

Hi all, I’m the process of writing an application to manage irrigation valves, using weather station data and soil moisture sensors.

But, I’m having difficulty understanding how to subscribe to device drivers and use the data from them.

Can anyone please share some code with me that demonstrates how this works?

PS, I’m an idiot, so simple explanations (or nicely commented code), would be appreciated.

You begin by selecting devices. Apps can't talk to devices without being selected. So... now you have a list of devices that have some common Capability. Then, you subscribe to them...

def sensorPage() {
	dynamicPage(name: "sensorPage") {
		if (deviceType == "powerMeter") {
			section ("<b>When this device starts/stops drawing power</b>") {
				input "pwrMeter", "capability.powerMeter", title: "Power Meter" , multiple: false, required: false, defaultValue: null
			}
		}
}
def initialize() {
	if (settings.deviceType == "powerMeter") {
		subscribe(pwrMeter, "power", powerHandler)
	}
}

All those are snippets from Better Laundry Monitor Child

2 Likes

Thank you mate, I’ll take a look tomorrow morning when I don’t have a couple of scotch whiskeys onboard! (It’s friday night here in straya) :rofl:

3 Likes

Auto_Off_child also has subscribe sprinkled in. :slight_smile:

1 Like

Cheers, this will be super helpful. :ok_hand:

Download the Hubitat app