Get Water Condition and Temperature from Water Sensor with Single Input


(Don Narbs) #1

Hi and thanks in advance. I am trying to write a SmartApp that detects water and turns on some lights. But I also want to read its temperature and battery level. So I have:

preferences {
    section("When Water Is Detected Here:") {
    	input "water", "capability.waterSensor", title: "Water Sensors", required: true, multiple: true
    }
    section("Turn On These Lights/Switches:") {
    	input "switches", "capability.switch", title: "Lights/Switches", required: false, multiple: true
    }
}

def installed() {
	log.debug "Installed with settings: ${settings}"

	initialize()
}

def updated() {
	log.debug "Updated with settings: ${settings}"

	unsubscribe()
	initialize()
}

def initialize() {
	subscribe(water, "water", myHandler)
    
	//Get the capabilities and attributes of the water sensor
        def mySensors = water.capabilities

	mySensors.each {cap ->
        	log.debug "Capability name: ${cap.name}"
	        cap.attributes.each {attr ->
        		log.debug "-- Attribute name; ${attr.name}"
        	}
	}       
}

def myHandler(evt) {
	if(evt.value == "wet") {
  		log.debug "Water Detected"
        
            switches.on()
            log.debug "Switches On"
        
            //Get Temperature and Battery Level
            log.debug "Temperature: ${evt.temperature.value}"
            log.debug "Battery Level: ${evt.battery.value}"
	}
}

When I check to see what capabilities and attributes the water sensor has, the output is this:

debug – Attribute name; [checkInterval]
debug – Attribute name; []
debug – Attribute name; []
debug – Attribute name; [water]
debug – Attribute name; [battery]
debug – Attribute name; [temperature]
debug Capability name: [Temperature Measurement, Battery, Water Sensor, Configuration, Refresh, Health Check]

However, when I try to get the Temperature Measurement or the Battery Level, I get this error:

groovy.lang.MissingPropertyException: Exception evaluating property ‘value’ for java.util.ArrayList, Reason: groovy.lang.MissingPropertyException: No such property: value for class: physicalgraph.app.AttributeWrapper
Possible solutions: name

I’m new at this so I don’t know what I’m missing. I don’t want to have to select the Water Sensor 3 times to be able to read its different attributes. How can I read the other attributes of the water sensor while only having the single input?

Thanks


(Kevin) #2

The event is specific to the attribute that changed and caused the handler to execute.

Get the most recent attribute values directly from the device using water.currentBattery and water.currentTemperature


(Don Narbs) #3

Thank you, sir! I was pulling my hair out trying to figure that out. So simple.