Homey Pro - homey-api package

Hello Homey Community,

I am using the homey-api package (NPM NODE/JS) to create my own dashboard. I am using the Angular IO Framework to load homey-api.

I am able to successfully connect to my Homey and access the list of my devices, but I am unable to subscribe to the device.update event as stated in the documentation. I am not encountering any JS errors or otherwise, but a device status update does not trigger any event on my end.

 private async init() {
    try {
      // Connect to Homey API
      const homeyApi = await HomeyAPI.createLocalAPI({
        address: environment.homey_ip,
        token: environment.homey_token,
        debug: null
      });
      console.log('Connected to Homey locally'); // OK HERE

      // Load devices
      const devices = await homeyApi.devices.getDevices();
      console.log('Loaded devices:', devices); // OK HERE, LIST OF DEVICE IS AVAILABLE

      // Subscribe to device update events globally
      homeyApi.devices.on('device.update', (updatedDevice:any) => {
        console.log('Global device.update event triggered:', updatedDevice); // KO NEVER PASS HERE
        // Add your update logic here
      });
      console.log('Subscribed to global device update events');

      // Subscribe to specific device updates
      const specificDeviceId = 'b515b4ca-5570-4a9b-bef8-XXX';
      const device = await homeyApi.devices.getDevice({ id: specificDeviceId });

      if (device) {
        console.log('Device found:', device); // OK HERE, DEVICE IS AVAILABLE
        device.on('device.update', (updatedDevice:any) => {
          console.log('Specific device.update event triggered:', updatedDevice);  // KO NEVER PASS HERE
          // Add your update logic here
        });
        console.log('Subscribed to specific device updates');
      } else {
        console.error('Device not found');
      }

    } catch (error) {
      console.error('Error initializing Homey API:', error);
    }
  }

Please find attached my code so that you can better understand my issue.

Thank you in advance for your help and response.

Best regards,

Before you receive these events you need to use connect() (which is one of those things that isn’t documented anywhere AFAIK):

await homeyApi.devices.connect();
homeyApi.devices.on('device.update', ...);

Also, there is no event device.update on Device instances, only update.

FWIW, these events aren’t triggered on capability changes, only on underlying device changes (for instance when a capability has been added or deleted from the device, or when a device becomes ready).

1 Like

Hello,

Thank you very much for your prompt response. It is greatly appreciated, thank you!

Is there a way to monitor status changes in this case?

A light turns on, my alarm changes status, a door opens… all the changes of state?

Thank you in advance,
SĂ©bastien

There’s an undocumented event capability that you can use:

device.on('capability', ({ capabilityId, value }) => {
  console.log(`Capability "${ capabilityId }" changed to "${ value }"`);
});

Thanks so much for your return.

I tried this code, but not working

      await this.homeyApi.devices.connect(); // Fetch all devices.

    // Get device
    const device = await this.homeyApi.devices.getDevice({id: 'b515b4ca-5570-4a9b-bef8-752a48141acb'});

    // Test 1
    device.on('capability', (event:any) => {
      console.log('NOT WORKGING');
    })

    // Test 2
    this.homeyApi.devices.on('capability', (device: any) => {
      // Subscribe to device update events.
      console.log('NOT WORKGING'); // NOT WORKING DON'T HAVE THIS LOG ON CHANGE STATUS DEVICE.
      if (this.subscribedDevices.has(device.id)) {
        // Check if the device is subscribed.
        this.deviceDataService.updateDevice(device); // Update the device data.
      }
    });
    this.homeyApi.devices.on('update', (device: any) => {
      // Subscribe to device update events.
      console.log('WORKING'); // NOT WORKING DON'T HAVE THIS LOG ON CHANGE STATUS DEVICE.
      if (this.subscribedDevices.has(device.id)) {
        // Check if the device is subscribed.
        this.deviceDataService.updateDevice(device); // Update the device data.
      }
    });

Best regards,

It’s OK ! Thanks.

It’s not possible to event capability for all devices for have all devices updated some times ?

// Get device
const device = await this.homeyApi.devices.getDevice({id: 'fd0fef76-ef51-44af-b5ad-ee6caffb835e'});
device.makeCapabilityInstance('onoff', (value:any) => {
  console.log('Device onoff changed to:', value);  // OK
});

// Test 1
device.on('capability', (event:any) => {
  console.log(event); // OK
})

Apologies, it should be this:

const device = await this.homeyApi.devices.getDevice({id: 'b515b4ca-5570-4a9b-bef8-752a48141acb'});

device.on('capability', (event:any) => {
  console.log('NOT WORKGING');
}).connect();

Thank you very much for your invaluable help. I now have a functional foundation that allows me to move forward with my interface!

A thousand thanks!