Bool || boolean || Boolean

As I continue to learn Groovy/Hubitat I have been making notes for myself. I'm trying to list the basics so when I forget a syntax but know its important I can look at my notes.

I've found what (to me) are conflicting "best practices" in defining a type Boolean. While some areas might not complain if the wrong syntax is used, some might. I would like to know the correct method of defining this type.

Thanks
John

2020-12 bravenel:
"bool" is not a supported attribute type. So, it has no built-in operators, such as comparison.
You can select an attribute from the drop down list in RM if it exists but if it's unsupported you can't do anything with it.

void logsOff(){
    log.warn "debug logging disabled..."
    device.updateSetting("logEnable",[value:"false",type:"bool"])
}

There are two different things in your question above: "bool" is a perfectly valid type for a setting (i.e., an input in an app or driver). It is not a valid type for an attribute (so it's not used by any stock attributes, i.e., ones that are part of capabilities; and the takeaway here is that you can't define custom attributes like attribute "myAttribute", "bool" in your driver metadata). That is why Bruce's example works; it's updating the value for an input from true to false (in the UI, this would un-check the box for that setting).

Separately from both of those, there are also the issues of Groovy/Java types. Java offers a primitive boolean data type that is still available in Groovy. However, Groovy automatically "wraps" (boxes--and unboxes) these in Object types, here Boolean, so these two lines of code are pretty much equivalent in Hubitat:

Boolean myValue = false
boolean myValue2 = false

Here, these are data types for variables, which you'd really only be using in pieces of code you write yourself.

2 Likes

Isn't is all so clear! NOT. George Boole would be proud of how this all worked out in that distant and obscure place called Java.

1 Like

On Boolean vs. boolean,

Boolean is an object type. It actually can have 3 values false, true, null. (two of which map to false, and 1 to true)

boolean is a java type, It typically is not really used in groovy on HE as groovy scripts convert everything to object types. You can run into it at times for code that interacts with java, static, etc.

Now attributes, inputs are their own methods, and have their own definitions (vs. groovy, java).

If writing groovy, you can use def, boolean or Boolean. def is 'easy', but it does add more code to figure out what is going on.

2 Likes

So I should note that:

Attribute types are:
Boolean | boolean | number | string | decimal | enum | date (capitalization doesn't matter)

Variable types are:
Boolean | boolean | bool | number (int) | string | BigDecimal (float) | decimal | enum | date (capitalization ???)

Boolean | boolean are not valid Attribute types.

The drivers code check will not pop an error for them, but it WILL cause errors in some apps and such (I had some in my drivers for a long time not knowing this). So if you need a true/false specifically for an attribute you should use some other type (possible string so it shows nicely) and have "true" / "false".

I don't disagree with you but your experience conflicts with the above recommendation by bertabcd1234 and assumedly seconded by bruce.

I can appreciate what you are saying, I wonder if it was a bug that has since been fixed?

I actually meant the same thing as @snell: "bool" (or any variation thereof) is not currently a valid type for attributes. The docs did not accurately reflect this at one point, if I recall correctly (or maybe still don't). Internally, you are free to use any valid types for your own variables. It is also a valid type for an input (device--or app--setting).

Documentation updated to remove BOOL from the list of types.

3 Likes

Download the Hubitat app