SDK question regarding setDeviceCapabilities

Apologies for a perhaps dump question (still very limited in knowledge about homey sdk and java scripting), but the here’s something I can’t get to work.

I am using the 433mhz example from the SDK pages, and got it working to receive a signal. However what I would like to do is upon receiving the signal, setting the device capability to on or off,

So in my device.js is the following;

mySignal.on("payload", function (payload, first) {
  console.log("Signal received")
  this.setCapabilityValue('onoff', false).catch(MyDevice.error);
});

and produces the following error:

Signal received
[HomeyClient] homey:manager:rf.onEvent Listener Error: TypeError: this.setCapabilityValue is not a function
    at Signal433.<anonymous> (/app/drivers/test/device.js:81:16)
    at Signal433.emit (node:events:525:35)
    at ManagerRF._onEvent (/node_modules/@athombv/homey-apps-sdk-v3/manager/rf.js:39:24)
    at /node_modules/@athombv/homey-apps-sdk-v3/lib/HomeyClient.js:111:13
    at Array.forEach (<anonymous>)
    at HomeyClient._onMessage (/node_modules/@athombv/homey-apps-sdk-v3/lib/HomeyClient.js:109:19)
    at EventEmitter.emit (node:events:513:28)
    at /homey-app-runner/lib/App.js:157:26
    at new Promise (<anonymous>)
    at /homey-app-runner/lib/App.js:156:18

How would it be possible to access the setDeviceCapability in that function?

Change function (payload, first) to (payload, first) => your this scope it not bound to the surrounding scope. When using an arrow function the this is bound to scope it was defined in.

1 Like

Many thanks for this!

To extend the question a bit more; a similar error is given when you try to access the capabilities from the app.js How would you deal with this outside its own device class? i.e if I would like to check a device state in the app.js ?

this.homey.drivers.getDriver('someid').getDevice(deviceData)

or

this.homey.drivers.getDriver('someid').getDevices()

and find the device.

Much appreciated!