Question: Driver Attributes and Values - Setting and Getting

I am modifying a driver, and I am having trouble understanding attributes and values, specifically how I can set attributes and how i can type / modify and read values.

Specifically, I am working on a multizone audio device. I would like attributes

attribute "PowerOn", "boolean"
attribute "ZoneActive", "boolean"

I have then tried to set their values with

device.updateDataValue("PowerOn", true)

which gives an error as updateDataValue takes two string arguments.

I tried

device.updateDataValue("PowerOn", "true")
log.debug "PowerOn = ${device.currentValue("PowerOn")}."
def mydm = device.getData()
mydm.each { key, value ->
  log.debug "Name: $key Value: $value"
}

which gives

"PowerOn = null."
"Name: PowerOn Value: true"

In this case it seems it creates a new "Value" "PowerOn" and sets its value to "true" but that is not the device attribute.

So my questions really are:

  1. What is the practical difference between attributes and values?
    (when should I use one vs the other)
  2. How can I set and read attributes?
  3. Can I get values that are not string data types.

Thanks for your help.

There's probably a more lengthy explanation that hopefully the more experienced and wise members of the Community can provide, but to get you going, I expect this is what you want:

Set Attribute Value:
sendEvent(name: "attributeName", value : "newValue")

Read Attribute Value:
device.currentValue("attributeName")

@sburke781 is correct when dealing with attributes. The updateDataValue() creates/updates items in the data section of the device page and is usually used to hold semi-static information about the device, i.e. model number, FW Version, etc. For values that change often you want to use attributes.

1 Like

Is there a way to have Maps or arrays as part of the driver?

You can store JSON strings in attributes instead of maps, and a string attribute could be used to contain a list which is easily split to instantiate an array.

1 Like

I think you can also do:

state.PowerOn = true

and to read it:
def mydm = state.PowerOn

sendEvent(name: "PowerOn", value : "true will update")

will update the Device state.

That's my understanding

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.