Finding the class of a variable


(Michael Seeley) #1

I am writing a Heating and AC app and want the app to auto switch from heating to cooling based on max an min temperature from the web. I can get the data from the web but am unable to compare the data from the web to values set by my app. the part of the code that is important is below. I think it is a class issue does anyone know how to show the class of a variable?

//Heating Cooling switch point Min
    section("Select the outside temperature threshold minimum to switch between Heating and Cooling") {
        input "tempSwitchMin", "decimal", title: "Temp Min Heating"
    }
    
    //Heating Cooling switch point Max
    section("Select the outside temperature threshold maximum to switch between Heating and Cooling") {
        input "tempSwitchMax", "decimal", title: "Temp Max Heating"
    }

private setTemp() {
    //Check summer winter
    //This code checks the current weather including the high and low then sets cooling or heating
    def pollParams = [
        uri: "http://api.openweathermap.org",
        path: "/data/2.5/find",
        headers: ["Content-Type": "text/json", "Authorization": "Bearer ${atomicState.authToken}"],
        query: [q: "West+Jordan+UT", units: "imperial"]
    ]
    
    try{
        httpGet(pollParams) { resp ->
            if (resp.data) {
                log.debug "The Response had data - ${resp.data}"
                log.debug "Status - ${resp.status}"
                log.debug "Min Temp - ${resp.data.list.main.temp_min}"
                def tempMin = resp.data.list.main.temp_min
                log.debug "tempMin = $tempMin"
                log.debug "Max Temp - ${resp.data.list.main.temp_max}"
                def tempMax = resp.data.list.main.temp_max
                log.debug "tempMax = $tempMax"
                log.debug "Current Temp - ${resp.data.list.main.temp}"
                log.debug "$tempSwitchMin - $tempSwitchMax"
                if (tempMin <= $tempSwitchMin && tempMax <= $tempSwitchMax) {
                    log.debug "setting thermostat to heat mode"
                    thermostat.setThermostatMode("heat")
                } else {
                    log.debug "setting thermostat to cool mode"
                    thermostat.setThermostatMode("cool")
                }

            }
               if(resp.status == 200) {
                log.debug "The Response status was 200"
                log.debug "poll results returned"
            }
            else {
                log.debug "The Response status was not 200"
                log.error "polling children & got http status ${resp.status}"
            }
        }
    } catch(Exception e) {

    }
    
}

(Patrick Stuart [@pstuart]) #2

Pretty safe to assume the data being parsed via json will be a string, you will need to change the type either toString() on your decimals inputs.


(Michael Seeley) #3

OK although I do not entirely understand it I figured it out. First what is returned from the JSON is not a string. So you have to first convert it to a string. It also was putting brackets around the number Max Temp - [84.2] so I also had to remove those. then converted it to a double and shazam like magic it works. def tempMax = resp.data.list.main.temp_max.toString().minus(’[’).minus(’]’).toDouble() Might be a cleaner way to do this but I have pulled my hair out enough on this.

Below is the end result.

private setTemp() {
	//Check summer winter
    //This code checks the current weather including the high and low then sets cooling or heating
    def pollParams = [
    	uri: "http://api.openweathermap.org",
    	path: "/data/2.5/find",
    	headers: ["Content-Type": "text/json", "Authorization": "Bearer ${atomicState.authToken}"],
    	query: [q: "West+Jordan+UT", units: "imperial"]
    ]
    
    try{
    	httpGet(pollParams) { resp ->
        	if (resp.data) {
            	log.debug "The Response had data - ${resp.data}"
                log.debug "Status - ${resp.status}"
                log.debug "Min Temp - ${resp.data.list.main.temp_min}"
                def tempMin = resp.data.list.main.temp_min.toString().minus('[').minus(']').toDouble()
                log.debug "tempMin = $tempMin"
                log.debug "Max Temp - ${resp.data.list.main.temp_max}"
                def tempMax = resp.data.list.main.temp_max.toString().minus('[').minus(']').toDouble()
                log.debug "tempMax = $tempMax"
                log.debug "Current Temp - ${resp.data.list.main.temp}"
                log.debug "$tempSwitchMin - $tempSwitchMax"
                if (tempMin < tempSwitchMin && tempMax < tempSwitchMax) {
                	log.debug "setting thermostat to heat mode"
                    thermostat.setThermostatMode("heat")
                } else {
                	log.debug "setting thermostat to cool mode"
                	thermostat.setThermostatMode("cool")
                }

        	}
       		if(resp.status == 200) {
            	log.debug "The Response status was 200"
        		log.debug "poll results returned"
    		}
        	else {
            	log.debug "The Response status was not 200"
        		log.error "polling children & got http status ${resp.status}"
    		}
        }
	} catch(Exception e) {

	}  
}

(Patrick Stuart [@pstuart]) #4

unfortunately the groovy option to “.getClass()” is restricted for some unknown reason.

However, you can check if a var is a type by doing the following:

def isVarString = (var instanceof String)

Replace String with any class you are trying to test, will return true or false if is or isn’t that type.