Device Attributes

Can anyone tell me the difference between explicitly defining a custom attribute in a device driver vs. generically identifying them?

For example:

attribute "TotalCount", "number"

-VS-

attribute "variable", "number"

I'm not entirely sure what you mean, though given you are asking the question, you may not be able to explain it any more than you have.... Is there an example or somewhere you have seen those terms used that prompted your question? Or is there something you are trying to do in your own code that you are not sure about?

The latter.

These two driver codes function the exact same (albeit, based on limited testing):

Explicitly Defined

`/**
*

  • Sensor Groups+_Virtual Contact Sensor
  • Copyright 2022 Ryan Elliott
  • Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge,
  • publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  • The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  • THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  • LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  • v1.0 RLE Creation
  • v1.1 RLE Added list attribute to show triggered devices
  • v1.2 RLE Added threshold attribute
    */

metadata {
definition(
name: "Sensor Groups+_Virtual Contact Sensor",
namespace: "rle.sg+",
author: "Ryan Elliott")
{
capability "ContactSensor"
capability "Actuator"

	attribute "contact", "enum", ["closed", "open"]
	attribute "TotalCount", "number"
	attribute "TotalOpen", "number"
	attribute "TotalClosed", "number"
	attribute "OpenList", "string"
	attribute "OpenThreshold", "number"
}

}

def installed() {
log.warn "installed..."
}

def updated() {
log.info "updated..."
}`

Compared to:

Generic

`/**
*

  • Sensor Groups+_OmniSensor+
  • Copyright 2022 Ryan Elliott
  • Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge,
  • publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  • The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  • THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  • LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  • v1.0 RLE Creation
  • v1.1 RLE Added list attribute to show triggered devices
  • v1.2 RLE Added threshold attribute
    */

metadata {
definition(
name: "Sensor Groups+_OmniSensor+",
namespace: "rle.sg+",
author: "Ryan Elliott")
{
capability "ContactSensor"

	attribute "variable", "string"
	attribute "variable", "number"
}

}

def installed() {
log.warn "installed..."
}

def updated() {
log.info "updated..."
}`

I'm using an app to send events to set the custom attributes (TotalOpen,TotalCount,TotalClosed) and it functions just fine with either driver code.

Ah, I think I know what you mean. In your App code are you attempting to set the device attributes with the names TotalOpen, etc?

Correct: https://raw.githubusercontent.com/FriedCheese2006/SensorGroupsPlus/main/ChildApps/ContactSensorPlus_ChildApp.groovy

Check the device page, do the attributes remain in the list, for the device using the driver with them named as "variable"? I would expect they would appear, but then quickly disappear, because they do not match.

Nope...you're right. I thought I had switched one of the child apps to use the generic one and hadn't. That's what I get for toying with this stuff so late.

All good, glad you sorted it out.

There isn't a "generic" one. With attribute "variable" versus attribute "totalCount" or whatnot, the only difference is what you name them (and, in turn, how they display in the UI and how you have to refer to them in apps or elsewhere in your driver). Also, regarding your second example specifically, you shouldn't use two attributes with the same name, even if they are different types. (I'd be surprised if this even works in the first place, but it won't work well when trying to use them even if it doesn't totally break.)

Some capabilities require specific attribute names. There actually is a capability "Variable" that does, as used by a hub variable connector of type "Variable," but if you aren't using that in your driver (and most people wouldn't), you're free to use an attribute of that name if you think it's a good one for your use. Attribute names can be whatever you want (as long as it only contain valid characters).

4 Likes

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