Subscribe using device id instead of device name

Is there a way to subscribe using the device id instead of the device name?

Like:

    subscribe(deviceID, "temperature", eventHandler)

I have an app that creates the subscribe but if the device gets renamed, the device no longer reports subscribed events. Thus, the app user is unaware the subscribe is no longer working as original set.

I was hoping if I could use device id it would prevent this.

Subscription works by (a reference to) the device object, not the name. Renaming will have no effect. If you are seeing this, something else is going on. Sharing a minimal example that demonstrates the problem you see would be helpful to figure it out what that is in your case.

1 Like

The device name (label) should not change anything with subscribe.

Are you not using an input to assign the device wrapper to a setting name?

        section("<b>Stove Hood Light</b>") {

            input (
              name: "stoveLight", 
              type: "capability.switch", 
              title: "Select Stove Hood Light Switch Device (optional)", 
              required: false, 
              multiple: false,
              submitOnChange: true             
            )
        }

The setting named "stoveLight" is not dependent on the label name.

subscribe(stoveLight, "switch", stoveLightHandler)

If you want to create the setting directly without an input, you can just use update setting, which does use the deviceID.

app.updateSetting("stoveLight",[value:"814",type:"capability.switch"]);

I don't get how your subscriptions are dependent on label, that is not part of a subscription.

1 Like

Here is what I am doing

    TempDevices.each
    {
        strName = it.getLabel()
        if(strName == null) { strName = it.getName() }
        subscribe(it, "temperature", eventHandler) 
    }

I have tried this but it is not creating a subscription event and not causing an error.

        strDeviceName = it.getName()
        subscribe(strDeviceName, "temperature", eventHandler)
           - have tried with quotes and with $ sign, same results

What is TempDevices? Can you share a complete, minimal "working" app that demonstrates the problem? What you have looks like it should work, assuming TempDevices is a standard device input with multiple: true, but seeing a complete app (with just enough to demonstrate) would tell us for sure.

You can also just use it.displayName, which is basically the result of it.label ?: it.name, but that isn't related to your problem -- just looks like your goal with that part. :slight_smile:

What I thought after 2 days of working on this it is NOT an issue with subscribe and the way I am using it!!!!

Sorry, and thanks for all of your help. As I read your replies this made me dig deeper into the code and I have discovered the real problem that I can correct.

Sorry again.

Can someone please close this Topic?

1 Like