I noticed that when specifying an input of type "bool", the default value of the option is "null" until the user interacts with the toggle if the "defaultValue" option isn't specified. This is a bit unintuitive since the toggle appears in the "false" position by default, you would think this "false" value would be used. Is this the intended behavior?
AIUI this is the intended behaviour.
All the input variable values are null, until you set them yourself.
If defaultValue is used, then this is used to set the value that is shown when the page is rendered - it doesn't set the actual value of the variable itself.
To set the variable, you should init them in either initialize() or installed() methods i.e. something like
device.updateSetting("trcEnable", [value: "true", type: "bool"])
The above matches my experience as well (and I'll reiterate the point that this isn't "bool"-specific--and that the default values are only displayed as such and thus actually still null
until the page is actually submitted).
An alternative to creating/updating default values "manually" as suggested, you could also keep this in mind when checking the value of a setting. This means you just rely a bit less on Groovy truth. For example, if you have defafultValue: true
on a "bool"
that may not yet have been submitted, when you check the value of this setting later, you could say if (mySetting != false)
or whatnot instead of just if (mySetting)
or if (mySetting == true)
. This way, you'll capture both true
(as it would be after submission if left at the default) and null
(as it may be, even though it's showing as true due to your defaultValue
that just hasn't been submitted yet). That's one example; you may need to do different things depending on your default values and the desired expectations in various cases. The above might be a cleaner way to solve this problem...I just have never bothered to try anything like that yet myself.
PS - I hope you don't mind that I've moved your post to the "Developers" category since it relates to development, not general support.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.