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.
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.
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.
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 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).