Setting an Attribute from a preference

Is it possible in a Device Handler to set a device attribute to a string from a preferences input?

Should be able to using a SendValue but why would you want to?

1 Like

Yes. I use it to create a user-defined tile text for preset channels on a speaker.

If the tile has a state “preset_1” and preferences has a variable “preset_1_title”, then code like below works well in the updated block of code

def preset_1 = device.currentValue("preset_1")
if (preset_1_title != preset1) {
       sendEvent(name: "preset_1", value: "preset_1_title")

This checks if the title has changed, and if so, it updates the preset_1 attribute.

Correct answer, but your example has a typo in the if statement and the event value is being set to that string instead of to the setting value.

It should be something like:

def preset_1 = device.currentValue("preset_1")
if (preset_1_title != preset_1) {
       sendEvent(name: "preset_1", value: "$preset_1_title")
1 Like

© 2019 SmartThings, Inc. All Rights Reserved. Terms of Use | Privacy Policy

SmartThings; SmartApps®; Physical Graph; Hello, Home; and Hello, Smart Home are all trademarks of the SmartThings, Inc.