# Help with registerCapabilityListener

**URL:** <https://community.homey.app/t/help-with-registercapabilitylistener/10099>\
**Category:** Developers\
**Created:** [March 5, 2019, 12:02am UTC](https://community.homey.app/t/help-with-registercapabilitylistener/10099 "2019-03-05T00:02:28Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Le\_Cactus](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/le_cactus/32/9727_2.png) [@Le\_Cactus](https://community.homey.app/u/Le_Cactus)\
**Post date:** [March 5, 2019, 12:02am UTC](https://community.homey.app/t/help-with-registercapabilitylistener/10099/1 "2019-03-05T00:02:28Z")

</div>

Hi,

Is there a better example than the SDK offers for registering a listener for a capability change event? Within my app I want to act on certain changes in capabilities on some of my devices and in Homey 1.x it was simple by using the device.on method.

Now, when trying to implement the registerCapabilityListener on Homey 2.0 I can’t get it working; it’s trowing me errors like ‘device.registerCapabilityListener is not a function’, and the old device.on method just doesn’t seem to work anymore. Any hints/examples are much appreciated.

TIA, cheers,  
/Cacti

---

<div class="post-metadata">

**Author:** ![Jamie](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jamie/32/5274_2.png) [@Jamie](https://community.homey.app/u/Jamie)\
**Post date:** [March 5, 2019, 2:36am UTC](https://community.homey.app/t/help-with-registercapabilitylistener/10099/2 "2019-03-05T02:36:46Z")

</div>

It isn’t a better example, but it is a real world usage.

```auto

    /**
     * Initialises the capability listener.
     *
     * Basically : Registers every capability the group (MultipleCapabilityListener) has, so
     * when any of the group capabilities are changed, the function is called which sets the
     * value of all of the devices to said value.
     *
     * As this is only listening for capabilities (which cant be changed in the settings), we never have to reload this.
     *
     * @returns {Promise<void>}
     */
    async initGroupListener() {

        /**
         * Register all of the capabilities at once with a (async) call back.
         *
         * values : An object with the changed capability values, e.g. { dim: 0.5 }
         * options : An object with optional properties, per capability, e.g. { dim: { duration: 300 } }
         *
         * Increase the time out - as large groups will require more time. Especially via 3rd Party server (alexa/google)
         */
        return this.registerMultipleCapabilityListener(this.capabilities, async (values, options) => {
            return this.updateDevicesCapabilities(values, options);
        }, 1000);
    }

```

Where :

```auto

 this.capabilities = await this.getCapabilities();

```

and

```auto
    /**
     * Updates the devices capabilities called from the groups capability listener.
     *
     * @param values
     * @returns {Promise<boolean>}
     */
    async updateDevicesCapabilities(values) {

        // Do stuff 

    } 

```

---

<div class="post-metadata">

**Author:** ![Le\_Cactus](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/le_cactus/32/9727_2.png) [@Le\_Cactus](https://community.homey.app/u/Le_Cactus)\
**Post date:** [March 5, 2019, 6:47am UTC](https://community.homey.app/t/help-with-registercapabilitylistener/10099/3 "2019-03-05T06:47:10Z")

</div>

Thanks Jamie!

I’ll see if I can get that working.

Regards  
/Cacti

---

<div class="post-metadata">

**Author:** ![DaneedeKruyff](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/daneedekruyff/32/964_2.png) [@DaneedeKruyff](https://community.homey.app/u/DaneedeKruyff)\
**Post date:** [March 5, 2019, 7:01am UTC](https://community.homey.app/t/help-with-registercapabilitylistener/10099/4 "2019-03-05T07:01:42Z")

</div>

Here is another example for listening in on a particular capability

> <https://github.com/daneedk/com.uc.heimdall/blob/d76805ecce5d1edbc13b70a34729051ecc2c6604/app.js#L213>

This gets called from here while cycling through all devices.

> <https://github.com/daneedk/com.uc.heimdall/blob/d76805ecce5d1edbc13b70a34729051ecc2c6604/app.js#L184>

---

<div class="post-metadata">

**Author:** ![Le\_Cactus](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/le_cactus/32/9727_2.png) [@Le\_Cactus](https://community.homey.app/u/Le_Cactus)\
**Post date:** [March 5, 2019, 7:08pm UTC](https://community.homey.app/t/help-with-registercapabilitylistener/10099/5 "2019-03-05T19:08:36Z")

</div>

Thanks guys. All is well now!

Cheers,  
/Cacti

---

<div class="post-metadata">

**Author:** ![Le\_Cactus](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/le_cactus/32/9727_2.png) [@Le\_Cactus](https://community.homey.app/u/Le_Cactus)\
**Post date:** [January 31, 2021, 3:50pm UTC](https://community.homey.app/t/help-with-registercapabilitylistener/10099/6 "2021-01-31T15:50:27Z")

</div>

Hi,

Another question, how do I delete/remove/detach/destroy a listener? I use makeCapabilityInstance to attach a listener to specific capabilities, but what if I want to detach a listener? I can’t find any examples, and I’m unable to figure out how to implement the destroy method myself 🙂

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [January 31, 2021, 3:55pm UTC](https://community.homey.app/t/help-with-registercapabilitylistener/10099/7 "2021-01-31T15:55:56Z")

</div>

Looks like a capability instance has [a `destroy()` method](https://developer.athom.com/docs/api/HomeyAPI.ManagerDevices.Device.CapabilityInstance.html#destroy) that you can call:

```auto
const instance = device.makeCapabilityInstance('onoff', handler);
…
instance.destroy();

```

---

<div class="post-metadata">

**Author:** ![Le\_Cactus](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/le_cactus/32/9727_2.png) [@Le\_Cactus](https://community.homey.app/u/Le_Cactus)\
**Post date:** [January 31, 2021, 4:14pm UTC](https://community.homey.app/t/help-with-registercapabilitylistener/10099/8 "2021-01-31T16:14:21Z")

</div>

Thanks, but I don’t see how I can use that (I’m not an experienced programmer)

I use this to attach a listener:

```
device.makeCapabilityInstance(params.capability,function(device, state) {
	this.stateChange(device,state,params.capability, 0)
}.bind(this, device));

```

I can do this:

```
const instance = device.makeCapabilityInstance(params.capability,function(device, state) {
	this.stateChange(device,state,params.capability, 0)
}.bind(this, device));

```

It also attached the listener, but instance.destroy() doesn’t detaches it.

Thanks for your input.

Edit: saw that it made no sense 🙂

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [January 31, 2021, 6:38pm UTC](https://community.homey.app/t/help-with-registercapabilitylistener/10099/9 "2021-01-31T18:38:01Z")

</div>

I can’t reproduce, using this code:

```auto
    const api = await HomeyAPI.forCurrentHomey();
    const device = await api.devices.getDevice({ id : DEVICE_ID });

    const listener = device.makeCapabilityInstance('meter_power', value => {
      console.log('listener update', value);
    });

    setTimeout(() => {
      console.log('destroying listener');
      listener.destroy();
    }, 5000);

```

It creates a capability listener for `meter_power` for a specific device. After 5 seconds, as expected, `listener update` messages stop from being logged.

---

<div class="post-metadata">

**Author:** ![Le\_Cactus](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/le_cactus/32/9727_2.png) [@Le\_Cactus](https://community.homey.app/u/Le_Cactus)\
**Post date:** [January 31, 2021, 11:45pm UTC](https://community.homey.app/t/help-with-registercapabilitylistener/10099/10 "2021-01-31T23:45:13Z")

</div>

Hi,

I have two functions one for attaching the eventlistener, and one for destroying it. I guess that is the reason this solution isn’t working for me; the instance created in the first function doesn’t seem to be available in the second function. And because I have many listeners I can’t make them global I think.

Thanks for testing though.

---

<div class="post-metadata">

**Author:** ![Le\_Cactus](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/le_cactus/32/9727_2.png) [@Le\_Cactus](https://community.homey.app/u/Le_Cactus)\
**Post date:** [January 31, 2021, 11:54pm UTC](https://community.homey.app/t/help-with-registercapabilitylistener/10099/11 "2021-01-31T23:54:24Z")

</div>

Solved it by creating a global array to store all these ‘instances’. Maybe it isn’t the best solution, but it works for me.

Thanks.

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [February 1, 2021, 6:30am UTC](https://community.homey.app/t/help-with-registercapabilitylistener/10099/12 "2021-02-01T06:30:39Z")

</div>

Assuming that you create these listeners inside a class instance, you could create an instance property (`this.listeners` for example) to store them in.

---

<div class="post-metadata">

**Author:** ![Jens\_De\_Smet](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jens_de_smet/32/33978_2.png) [@Jens\_De\_Smet](https://community.homey.app/u/Jens_De_Smet)\
**Post date:** [July 20, 2021, 7:40pm UTC](https://community.homey.app/t/help-with-registercapabilitylistener/10099/14 "2021-07-20T19:40:41Z")

</div>

> [@DaneedeKruyff](#):
>
> `if ( device.data.id === 'sMode' ) {`

Ik zie in deze regel code wel iets, maar ik zit nog vast.

hier heb ik deze regel ingeplaats:

```auto
this.registerCapabilityListener('my_enum_capability', async (value) => {
      this.log('Changes to :', value);

      if ( driver.data.id === 'sMode' ) {}
    });

```

maar er komt een rood lijntje onder driver

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/1/e/1e1189d918f970d4fb582210e4e387c58bc19db4.png)

dit zou de oplossing zijn voor mijn probleem,  
ik heb namelijk meerder apparaten in een device en ik kan niet uitlezen welke aan of uit gaat er gaat gewoon een aan of uit.

Met vriendelijke groeten,  
Jens De Smet 😀 .

---

<div class="post-metadata">

**Author:** ![MarcoG](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcog/32/36375_2.png) [@MarcoG](https://community.homey.app/u/MarcoG)\
**Post date:** [July 21, 2021, 4:54am UTC](https://community.homey.app/t/help-with-registercapabilitylistener/10099/15 "2021-07-21T04:54:35Z")

</div>

I guess you’re in the device.js. If you’re using sdk3 you could try “this.driver…” etc.

---

<div class="post-metadata">

**Author:** ![Jens\_De\_Smet](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jens_de_smet/32/33978_2.png) [@Jens\_De\_Smet](https://community.homey.app/u/Jens_De_Smet)\
**Post date:** [July 21, 2021, 5:00am UTC](https://community.homey.app/t/help-with-registercapabilitylistener/10099/16 "2021-07-21T05:00:08Z")

</div>

![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/1/d/1d811381551fd79750a4201a08c6711f6358d86e.png)

Still a red line under data I want to be able to read the id.

Thanks for the effort anyway.

---

<div class="post-metadata">

**Author:** ![MarcoG](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcog/32/36375_2.png) [@MarcoG](https://community.homey.app/u/MarcoG)\
**Post date:** [July 21, 2021, 5:07am UTC](https://community.homey.app/t/help-with-registercapabilitylistener/10099/17 "2021-07-21T05:07:39Z")

</div>

Did you declare the ‘data’ var in your driver class?  
Like:  
class MyDriver extends Homey.Driver {  
data = {}

---

<div class="post-metadata">

**Author:** ![Jens\_De\_Smet](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jens_de_smet/32/33978_2.png) [@Jens\_De\_Smet](https://community.homey.app/u/Jens_De_Smet)\
**Post date:** [July 21, 2021, 5:11am UTC](https://community.homey.app/t/help-with-registercapabilitylistener/10099/18 "2021-07-21T05:11:31Z")

</div>

Yes I did that.

![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/9/d/9dde277a819c1174343a3b4c115f6c8489d6eb19.png)

---

<div class="post-metadata">

**Author:** ![MarcoG](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcog/32/36375_2.png) [@MarcoG](https://community.homey.app/u/MarcoG)\
**Post date:** [July 21, 2021, 5:13am UTC](https://community.homey.app/t/help-with-registercapabilitylistener/10099/19 "2021-07-21T05:13:08Z")

</div>

Especially the “data = {}” part within the class.

---

<div class="post-metadata">

**Author:** ![Jens\_De\_Smet](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jens_de_smet/32/33978_2.png) [@Jens\_De\_Smet](https://community.homey.app/u/Jens_De_Smet)\
**Post date:** [July 21, 2021, 5:16am UTC](https://community.homey.app/t/help-with-registercapabilitylistener/10099/20 "2021-07-21T05:16:04Z")

</div>

![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/3/c/3c14997960c2f5b7597afbcddd26bc878bb3434e.png)

![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/1/c/1c86ae0e759c1332dd23b3c1fb694d88282ade82.png)

---

<div class="post-metadata">

**Author:** ![Adrian\_Rockall](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/adrian_rockall/32/13209_2.png) [@Adrian\_Rockall](https://community.homey.app/u/Adrian_Rockall)\
**Post date:** [July 21, 2021, 7:16am UTC](https://community.homey.app/t/help-with-registercapabilitylistener/10099/21 "2021-07-21T07:16:52Z")

</div>

To access data you need to get it first:

```auto
let devData = this.getData();
if (devData.id === 'sMode'){}

```

The data setup during pairing is stored in the device instance.

Edit: Are you sure you want the id there though as that should be a unique identifier per device?

[Next page](https://community.homey.app/t/help-with-registercapabilitylistener/10099.md?page=2)
