Help with registerCapabilityListener

You will obviously need something that generates an event to call your code. That could be a flow action card for example (or by polling for information from the physical device if you had one).

In the flow action you set the capability value. So, in your app onInit function you would register the flow card something like this:

 let change_my_capability_action = new Homey.FlowCardAction( 'change_my_capability_action' );
 my_action 
   .register()
   .registerRunListener( async ( args, state ) =>
   {
      return await args.device.triggerCapabilityListener( 'enum_no_on_off_capability', args.on_off ); 
   });

app.json

  "flow": {
    "actions": [
        {
          "title": {
          "en": "Switch On or Off"
        },
        "titleFormatted": {
          "en": "Turn [[on_off ]] the lights"
        },
        "args": [
          {
            "type": "device",
            "name": "device",
            "filter": "driver_id=myDeviceID"
          },
          {
            "type": "dropdown",
            "name": "on_off ",
            "values": [
              {
                "id": "off",
                "title": {
                  "en": "Off"
                }
              },
              {
                "id": "on",
                "title": {
                  "en": "On"
                }
              }
            ]
          }
        ],
        "id": "change_my_capability_action"
      },
    ]
  }
1 Like

triggerCapabilityListener might not work there as you have define the capability as not settable, in which case you would use
args.device.setCapabilityValue('enum_no_on_off_capability, args.on_off);

But that won’t trigger the capability listener.

1 Like

That’s not really what I meant but I see something in it to use later I mean if AE_I1 changes , instead of that line that comes in the CMD that turns on another device within the same driver eg Q1

{
         name: 'Q1 (AE)',
         dates: {
           id: 'AE_Q1',
         },
         capabilities: [
           "my-enum_no_on_off_capability"
         ],
         energy: {
           approximation: {
             usageConstant: 0, // in Watts
           },
         },
       },

Therefore it is enum_no_on_off_capability because it is not allowed to be turned on and off by the user.

But By the app itself when I1 eg turns on.

This is what I meant.

OK, still the same basic code applies to set a value, but you first need to locate the devices instance.
You could use:

let otherDevice = this.driver.getDevice({id: 'AE_Q1'});
otherDevice.setCapabilityValue('enum_no_on_off_capability', value);
1 Like