Device Capability Confusion..... humidSensors.each {total += it.currentHumidity}

I am using the below app as a learning tool. Its from a list of examples by Bruce found here

My confusion starts here:
I am assuming the input statement returns a list of devices named "humidSensors"
The part that throws me is in the totaling statement :

	humidSensors.each {total += it.currentHumidity}

which totals the humidity value of a number of different humidity sensors.
Specifically the capability "currentHumidity" I cannot find where the capability "currentHumidity" comes from. Its not in the Hubitat documents nor any of the many Z-Wave specs I've accumulated. It also appears nowhere else in the example program.

The app works but I'm lost has to why the seemingly undocumented capability functions.

Any help would be appreciated.

definition(
    name: "Average Humidity",
    namespace: "hubitat",
    author: "Bruce Ravenel",
    description: "Average some humidity sensors",
    category: "Convenience",
    iconUrl: "",
    iconX2Url: "")

preferences {
	page(name: "mainPage")
}

def mainPage() {
	dynamicPage(name: "mainPage", title: " ", install: true, uninstall: true) {
		section {
			input "thisName", "text", title: "Name this humidity averager", submitOnChange: true
			if(thisName) app.updateLabel("$thisName")
			input "humidSensors", "capability.relativeHumidityMeasurement", title: "Select Humidity Sensors", submitOnChange: true, required: true, multiple: true
			if(humidSensors) paragraph "Current average is ${averageHumid()}%"
		}
	}
}

def installed() {
	initialize()
}

def updated() {
	unsubscribe()
	initialize()
}

def initialize() {
	def averageDev = getChildDevice("AverageHumid_${app.id}")
	if(!averageDev) averageDev = addChildDevice("hubitat", "Virtual Humidity Sensor", "AverageHumid_${app.id}", null, [label: thisName, name: thisName])
	averageDev.setHumidity(averageHumid())
	subscribe(humidSensors, "humidity", handler)
}

def averageHumid() {
	def total = 0
	def n = humidSensors.size()
	humidSensors.each {total += it.currentHumidity}
	return (total / n).toDouble().round(1)
}

def handler(evt) {
	def averageDev = getChildDevice("AverageHumid_${app.id}")
	def avg = averageHumid()
	averageDev.setHumidity(avg)
	log.info "Average humidity = $avg%"
}

def contactHandler(evt) {
    log.debug "$evt.value"

    // The contactSensor capability can be either "open" or "closed"
    // If it's "open", turn on the light!
    // If it's "closed" turn the light off.
    if (evt.value == "open") {
        switch1.on();
    } else if (evt.value == "closed") {
        switch1.off();
    }
}

This is a SmartThings/Hubitat "shortcut" of sort for current attribute values. I don't use it because it scares me and I like to do everything myself, but it should work. :slight_smile: Anything you can do like this:

myDevice.currentValue("humidity")

...you can also do like this:

myDevice.currentHumidity

Basically, for any attribute, you can add current to the front, capitalize the first letter, and get your shortcut notation: myDevice.currentValue("switch") == myDevice.currentSwitch (this one seems to be pretty widely used), and so on.

2 Likes