"state" data type & how to determine the type for some data

I'm writing an app. I'm saving a epoch data in "state.idleTime"

idleTime is a long Epoch number. Does the state.xxxx force the data to a string?


Is there a method that can determine the data type of some variable?

I tried the below but received the noted error.

    def obj = state.lastAC
    log.info("state class ${obj.getClass()}")

which resulted in:

Expression [MethodCallExpression] is not allowed: obj.getClass() at line number 84

SomeObject.properties.each{ log.info it}
Will tell you everything and more...

4 Likes

Another trick someone told me about a while back, if you don't want all the "extra" information that the above will also provide, is to just call a method that doesn't exist with your object passed as the parameter. Then, Groovy will complain that it can't find a method matching that signature, and the signature will contain the type of your object.

Once I left a call like fsdaklajsd(myObject) in code I accidentally released, and a user complained about an error in Logs. That was why. :smiley:

2 Likes

Thanks,

I really only wanted it to verify my assumptions of data type or my lack of knowing data type. At least for the moment.

I want to start a Hubitat "cheat sheet", this and the fact that state.xxxx will be a string no matter what you assign to it. Kinda like "enum" type.

1 Like

This works for me.
Tried all sorts of things to handle null values and therefore no class, but his works.

	obj.properties.each
		{ k,v -> 
		if (v?.class)	
			log.debug  "${k}: ${v} ${v.class.name}"
		else	
			log.debug  "${k}: ${v}"
		}

Sample result
image