Help with celsiusToFahrenheit()

I’m trying to add code to convert a value that is returned from a REST API as Celsius and convert it into Fahrenheit. Within a case that sends the temperature value event I added the simple def tempValue with the conversion function. But I keep getting an execution error. Any ideas? My groovy is rudimentary at best. Thanks!

switch (it.comp) {
            case "temp":
            	def tempValue = celsiusToFahrenheit(it.value.toDouble().round(1))
            childDevice?.sendEvent(name: "temperature", value: tempValue as Integer, unit:"°")
            break

The runtime error is:
groovy.lang.MissingMethodException: No signature of method: script_app_d8fdc447ce6984db86be2c32530c647a5ddc78d887a7290f375c39dc30204cad.celsiusToFahrenheit() is applicable for argument types: (java.lang.Double) values: [20.6] @line 412 (doCall)

@swamplynx, Try to do the rounding outside of the celsiusToFahrenheit function first, and just pass the variable to that function. Use log.debug to check the format of the variable.

Because looking at your error message, it looks like that you are trying to pass an array/map to the celsiusToFahrenheit function. Note the brackets [20.6]

And just for your information, when you log the temperature as an event, you might want to follow the documentation’s units.

{
    "id": "temperatureMeasurement",
    "version": 1,
    "name": "Temperature Measurement",
    "status": "live",
    "attributes": {
        "temperature": {
            "schema": {
                "type": "object",
                "additionalProperties": false,
                "properties": {
                    "value": {
                        "title": "TemperatureValue",
                        "type": "number",
                        "minimum": -460,
                        "maximum": 10000
                    },
                    "unit": {
                        "title": "TemperatureUnit",
                        "type": "string",
                        "enum": [
                            "F",
                            "C"
                        ]
                    }
                },
                "required": [
                    "value",
                    "unit"
                ]
            }
        }
    },
    "commands": {

    }
}

I’ve tried it that way too and get the same error… Very stumped at this point. Any other ideas?

 case "temp":
                def tmp = it.value.toDouble().round(1)
                def tempValue = celsiusToFahrenheit(tmp)
                childDevice?.sendEvent(name: "temperature", value: tempValue as Integer, unit: getTemperatureScale())

Runtime error:
groovy.lang.MissingMethodException: No signature of method: script_app_4a8b311f2fd144f9193859e1650964d5afe6032ea18c877a510be621c338e501.celsiusToFahrenheit() is applicable for argument types: (java.lang.Double) values: [20.8] @line 413 (doCall)

Do this before the tempValue line:

log.debug "This is the tmp variable: $tmp"

(Debug your code!)

And look at the Live Logging in the IDE.

Probably you will see this:

This is the tmp variable: [20.6]

It should be:

This is the tmp variable: 20.6

You need to make sure that tmp is not an array/map but a Number.

OK. It knows you are calling celsiusToFahrenheit() with a double, and the value of that double is 20.6 (don’t worry about the brackets, the arguments are being presented as a list and the key is that it saw a double). There doesn’t seem to be anything wrong with the call to the method, yet it can’t be found.

So where is this code being used within your device handler? Sometimes the context makes a difference.

That’s my guess, and I’m just lost in the sauce. I’ve also tried defining “def temp” within the case then doing the conversion with a getTemp() function outside of the function that writes the value to the device and calling that function as the value, but the variable I define within the case doesn’t seem to carry over globally (I just see a null value in the debug.

Here is the code for the DTH (actually this is a SA that updates the DTH), I am attempting to modify. The original dev doesn’t seem to be active in the forums unfortunately. The relevant code starts at line 410. Thanks guys, you rock!

	else if (descMap.cluster == "0402" && descMap.attrId == "0000") {
		def value = getTemperature(descMap.value)
		results = createEvent(getTemperatureResult(value))


private getTemperature(value) {
    	def celcius = Integer.parseInt(value, 16).shortValue() / 100
    //	log.debug "Celcius: $celcius Farenheit: ${celsiusToFahrenheit(celcius) as Integer}"
    	if(getTemperatureScale() == "C"){  
    		return celcius
    	} else {
    		return celsiusToFahrenheit(celcius) as Integer
    	}
    }

I just tried celsiusToFahrenheit() in a SmartApp and it didn’t work for me either, though it is fine in a device handler. Probably easier to just write the conversion yourself rather than worry about it too much.

Well that was stupid easy… That conversion function is hosed. Problem solved:

case "temp":
                	def tmp = it.value.toDouble()
                	def tempValue = 9 * (tmp / 5) + 32
                childDevice?.sendEvent(name: "temperature", value: tempValue.round(1) as Integer, unit: "F")
                break