Cannot send correct "sendevent" for custom attribute

I have a custom Zigbee sensor that sends a simple voltage value. I have been able to parse the incoming value to a float. However I've not been able to create the proper "sendevent" to set the value.
The smartthings documentation says the value in a sendEvent can be a value.

sendEvent(name: "Voltage", value: floatValue, unit:"V", isStateChange: true) shows "floatValue" in text
sendEvent(name: "Voltage", value: "floatValue", unit:"V", isStateChange: true) shows Null

Can anyone suggest where I'm going wrong?
Thanks
John

my code:

/**
 *	Analog Simple Test
 *  2021-01-10  v02
 *	2021-01-23  v07 - (open) adding the ability to convert the PVTO IEEE-754 input to decimal.
 *
 */
metadata {
	// Automatically generated. Make future change here.
	definition (name: "AnalogTst", namespace: "johnrob", author: "johnrob") {
		capability "Actuator"
		capability "Switch"
		capability "Configuration"
		capability "Refresh"
		capability "Sensor"
		attribute "Voltage", "BigDecimal"
        // attribute "Voltage", "long"

		//fingerprint profileId: "0104", inClusters: "0000,0003,0004,0005,0006,0B04,0B05", outClusters: "0019"
	}
}  // --- metadata ---
// Parse incoming device messages to generate events
def parse(String description) {
	//log.debug " 24 Parse description= $description"
	def name = null
	def value = null
    Map map = [:]
	if (description?.startsWith("read attr ")) {
		def descMap = zigbee.parseDescriptionAsMap(description)
		if (descMap.cluster == "000C" && descMap.attrId == "0055") {
//___Convert Hex Value to Float _____________________________________________
			hexValue = descMap.value
			valueHexStr = hexValue.toString()
			int intBits = Long.valueOf(valueHexStr, 16).intValue();
			float floatValue = Float.intBitsToFloat(intBits);
			log.info " 38 Voltage=${floatValue}"    // active log shows correct number
// __________________________________________________________________
		}
                   // for this test floatValue = 3.295
        sendEvent(name: "Voltage", value: floatValue, unit:"V", isStateChange: true)
        //sendEvent(name: "Voltage", value: "floatValue", unit:"V", isStateChange: true)
	}
} // --- parse ---

def parseDescriptionAsMap(description) {
	(description - "read attr - ").split(",").inject([:]) { map, param ->
		def nameAndValue = param.split(":")
		map += [(nameAndValue[0].trim()):nameAndValue[1].trim()]
	}
}
// --- eof ---

Looks like you're defining floatVal (the float floatVal = ... line) inside the if block, so by the time you get to the sendEvent() call, you're outside that scope and it no longer exists--but it does when you do the log.info, which explains why that works as expected. Either moving the sendEvent() inside the if (guessing you might want to do this and only send the event if there is indeed a value, though you obviously know what your device and driver should do better than I do) or moving the definition outside (Groovy will let you declare the variable without assigning a value) should work.

...if that's the problem. Just basing this on the only thing I can see off hand. :smiley:

Also, not sure if it matters, but the name: “voltage” should be lowercase.

2 Likes

Thank :slight_smile:
I thought I moved that } but I guess not.