# Update preferences field from code

**URL:** <https://community.smartthings.com/t/update-preferences-field-from-code/21219>\
**Category:** Writing SmartApps\
**Created:** [August 27, 2015, 6:45pm UTC](https://community.smartthings.com/t/update-preferences-field-from-code/21219 "2015-08-27T18:45:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dantheman2865](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/dantheman2865/32/17217_2.png) [@dantheman2865](https://community.smartthings.com/u/dantheman2865)\
**Post date:** [August 27, 2015, 6:45pm UTC](https://community.smartthings.com/t/update-preferences-field-from-code/21219/1 "2015-08-27T18:45:41Z")

</div>

Hi All,

Is there any way to dynamically fill a preferences field from code?

In my SmartApp, I am expanding on @brianwilson’s ThingSpeak code, but making the interface a little more user-friendly by automatically creating a channel on the user’s behalf.

My approach is to give the user an option to use an existing channel or create a new one. That means that I have options in my settings for “Channel ID” and “Channel Key”, but if those are empty, I create a new channel. I would like to fill in those fields once I create a channel and then use those values for my actual POSTs.

Does anybody know how to do this? Are preferences uni-directional?

Thanks,

Dan Kurin

---

<div class="post-metadata">

**Author:** ![Mike\_Maxwell](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/mike_maxwell/32/18673_2.png) [@Mike\_Maxwell](https://community.smartthings.com/u/Mike_Maxwell)\
**Post date:** [August 27, 2015, 6:50pm UTC](https://community.smartthings.com/t/update-preferences-field-from-code/21219/2 "2015-08-27T18:50:45Z")

</div>

Other than setting a default, I do not think it’s possible to programmaticly change these.  
Hopefully someone will chime in and say I’m wrong…

---

<div class="post-metadata">

**Author:** ![baldeagle072](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/baldeagle072/32/136125_2.png) [@baldeagle072](https://community.smartthings.com/u/baldeagle072)\
**Post date:** [August 27, 2015, 7:37pm UTC](https://community.smartthings.com/t/update-preferences-field-from-code/21219/3 "2015-08-27T19:37:00Z")

</div>

I don’t know if this will work, but it’s worth a shot. I think I’ve seen this method work for `options:`

In the options for the input, set default to a function. Then return the value you want in the function.

```
default: getName()

```

//

```
def getName() {
     return 'name-' + preference1 + '-' + preference2
}

```

Let me know if this works.

---

<div class="post-metadata">

**Author:** ![dantheman2865](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/dantheman2865/32/17217_2.png) [@dantheman2865](https://community.smartthings.com/u/dantheman2865)\
**Post date:** [August 28, 2015, 2:08pm UTC](https://community.smartthings.com/t/update-preferences-field-from-code/21219/4 "2015-08-28T14:08:46Z")

</div>

Here’s my solution.  
I use a dynamic page and set the description as a state variable (to my knowledge, “Default” is only available for device handlers)

```
input(name: "channelID", type: "number", title: "Channel ID", required: false, description: state.channelID)
input(name: "channelKey", type: "text", title: "Channel key", required: false, description: state.channelKey)

```

Then in my installed(), I check to see if either field is filled, otherwise I create a new channel:

```
if (channelID != null || channelKey != null) {
    state.channelID = channelID
    state.channelKey = channelKey.toUpperCase()
} else {
    createChannel()
}

```

In my updated(), I check again to see if the user specified a channel, otherwise we use what’s already in state:

```
if (channelID != null || channelKey != null) {
    state.channelID = channelID
    state.channelKey = channelKey.toUpperCase()
}

```

Because I’m using “Description” instead of “Default”, the returned value is still null even though text “appears” in the input box.

It’s a little hacky, but it serves the purpose. Any feedback?

---

<div class="post-metadata">

**Author:** ![zcapr17](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/zcapr17/32/26770_2.png) [@zcapr17](https://community.smartthings.com/u/zcapr17)\
**Post date:** [March 15, 2017, 5:03pm UTC](https://community.smartthings.com/t/update-preferences-field-from-code/21219/5 "2017-03-15T17:03:24Z")

</div>

It appears you can now use:

```
app.updateSetting(inputName, [type: type, value: value]) 
app.updateSetting(inputName, value)
// or for DTHs:
device.updateSetting(inputName, [type: type, value: value]) 
device.updateSetting(inputName, value)

```

However, while the above code seems to be able to change settings with existing non-null values, it does not appear to be able to add values to settings that are currently empty. So half-way there at least…

---

<div class="post-metadata">

**Author:** ![Mike\_Maxwell](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/mike_maxwell/32/18673_2.png) [@Mike\_Maxwell](https://community.smartthings.com/u/Mike_Maxwell)\
**Post date:** [March 16, 2017, 2:28pm UTC](https://community.smartthings.com/t/update-preferences-field-from-code/21219/6 "2017-03-16T14:28:20Z")

</div>

I wouldn’t expect a ton of responses regarding this, though it’s huge. Unless you’ve tried writing some pretty complex apps/handlers most wouldn’t even understand the implications here…

---

<div class="post-metadata">

**Author:** ![dantheman2865](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/dantheman2865/32/17217_2.png) [@dantheman2865](https://community.smartthings.com/u/dantheman2865)\
**Post date:** [March 24, 2017, 3:21pm UTC](https://community.smartthings.com/t/update-preferences-field-from-code/21219/7 "2017-03-24T15:21:46Z")

</div>

Thanks for the update!
