Getting device name,label in driver?

How can I get the device name/label in the driver to set a predefined preference?

I have an input preference called Service, I'd like to default it to the device name. Can I do this from the driver?

thanks
david

Do you mean when you are joining a device to the hub? Or adding a device from an app?

In my case I am adding a virtual device for an app.

david

Yes, you can set the device name and the device label from within the app, in the addChildDevice method. See this page for details:

https://docs.hubitat.com/index.php?title=App_Object

I have a driver, that I use when I go into device->create. I pick that driver:

I then have some preferences I set, and I would like to set Label to be the device label set: apprise

Sorry if you answered this, I just don't understand how child device fixes this.

thanks,
david

I'm sorry, I'm not understanding your question. What is the problem? Did you set the values per the document I linked to?

I guess I'm confused as to the data model naming. I thought it was App->device->driver.

Here is what I'm doing today. I just want to make it a bit more robust and automated to reduced setup issues.

Reading the documentation it seemed to me that this was a way for an App to get information about its devices. In my case the App is the Notification app, it uses my device and my device uses my driver.

The device name is also the tag into the Apprise database where all the different notification services are defined. Apprise runs in a docker on a local server. It could also be run in a VM, rPi whatever you like. The goal is to keep all my data local.

Today I just manually set the Label as shown in the previous picture. This allows someone to set it incorrectly and things won't work. I would like to be able to set it automatically when the device is created.

And I may just be completely off base and your answer was the answer I'm seeking, I just don't know enough to realize it.

thanks for the help,
david

Since it is your own driver, just set defaultValue when defining input:
input(name: "label", type: "string", title: "Label", defaultValue: "Apprise”)

I hope this helps!

When the app creates the device, it can define what the name and label of the device should be.

The method to create a child device:
addChildDevice(String namespace, String typeName, String deviceNetworkId, Map properties = [:])

You have to include the namespace and typeName is the driver's name and then the network ID. The properties map can include whether the device is a component, the name and the label. So, can you give me an example of how you want your device named, and I can show you how you would put that into this context. Does this clarify how it works?

I got to devices->add virtual device.

From that I add a device name, pick my driver apprise and hit save device. When it comes back I have additional setup to do, inputs defined in my driver.

  1. add the apprise server IP address
  2. add a Label. This is what I want to auto fill from the device name that I just saved. If I can get this in the driver I can also do checks to make sure the label exists on the server, and create it if it doesn't already exist.
  3. set the Service. The valid services are set as state.validservices and displayed on the setup form after setting 1 and 2 and hitting save. I wanted to make this an auto derived multiple ENUM type, but that isn't possible, so I stuck with just storing it and using cut-n-paste to fill it out.

After this the device is complete and I then run the Notification app and create a notification using this virtual device.

david

You are using addChildDevice incorrectly. You have to set the name, type and label when you create the device from the app. Or are you creating the device by hand? If so, it's not a child device. It's just a virtual device.

I'm creating the device by hand. I didn't see how to create one from the Notification app.

If I created it by hand, can I access the name from the driver?

david

Do you mean can the device driver rename the device that is using it? No. Why would you want to do that? How would the driver know what the name of the device should be. The parent app or device is the only thing that can name a device, when it is created. Otherwise you have to change the device name manually. There is no way to change it via the settings within the driver.

@Sigogglin, I'm not sure if I understand your use case completely, but I have a similar use-case based on how I interpreted your question.

So, I tried out a few of these methods that I found in the Device Object documentation:

def listDeviceInfo()
{
    logDebug ("getName() = ${device.getName()}")
    logDebug ("getLabel() = ${device.getLabel()}")
    logDebug ("getDispayName() = ${device.getDisplayName()}")
    logDebug ("getDeviceNetworkId() = ${device.getDeviceNetworkId()}")
}


def setDeviceName(name)
{
    device.setName(name)
}

def setDeviceLabel(label)
{
    device.setLabel(name)
}

def setDeviceDisplayName(dn)
{
    device.setDisplayName(dn)
}

def listDeviceData()
{
    logDebug("getData() = ${device.getData()}")
}

def updateDeviceData(name, value)
{
    device.updateDataValue(name, value)
} 

Here are my findings:

  • device.setName() sets the "Device Name" field under Device Information
  • device.setLabel() doesn't seem to set anything interesting
    ** Actually, it seems to clear the "Device Label" field under Device Information (on 2.1.3.128)
  • device.setDeviceDisplayName() sets the "Device Label" field under Device Information
  • device.updateDataValue(label, *value*) can be used to set the device label to value in the Device Details > Data section.
    ** And just for completeness, you can get at the info in the Device Details > Data section with device.getData()

I only tried these on a child device that I created from @stephack 's Virtual Container driver, so I'm not sure how some of the parent/child relationships that you and @Ryan780 have been discussing might affect these behaviors.

2 Likes

@Sigogglin maybe an example will help similar to what you are trying to accomplish. I have a parent/child concept for an integration with Twilio cloud notification service. You upload both drivers, create a virtual device using the container driver and once created you can create child notification devices from that parent driver. This differs from the stock Twilio driver where you have to enter your Twilio API keys in every notification device versus my solution you enter it once and the child notification devices refer back to the parent. Looking at your screenshots above it appears you require something similar. You can view the code here:
https://github.com/mlritchie/Hubitat/tree/master/Drivers/Twilio%20SMS