# setCapability of device on receiving mqtt message

**URL:** <https://community.homey.app/t/setcapability-of-device-on-receiving-mqtt-message/64315>\
**Category:** Developers\
**Created:** [June 2, 2022, 8:17am UTC](https://community.homey.app/t/setcapability-of-device-on-receiving-mqtt-message/64315 "2022-06-02T08:17:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fvrdam](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/fvrdam/32/47643_2.png) [@fvrdam](https://community.homey.app/u/fvrdam)\
**Post date:** [June 2, 2022, 8:17am UTC](https://community.homey.app/t/setcapability-of-device-on-receiving-mqtt-message/64315/1 "2022-06-02T08:17:41Z")

</div>

I’m trying to set a device capability value when a message is received through mqtt.  
Here is the device.js code:

```auto
'use strict';

const { Device } = require('homey');
const DeviceApi = require('./device-api');

class MyDevice extends Device {

  /**
   * onInit is called when the device is initialized.
   */
  async onInit() {
    this.log('Device has been initialized');
    this.mqttconn = await DeviceApi.connectMQTT();
    this.mqttconn.on('connect', function() {
      console.log('connected');
    });
    this.mqttconn.subscribe('tswoonkamer/target_temperature', { qos: 1 });
    this.mqttconn.on('message', function( topic, message, packet) {
      const msg = JSON.parse(message);
      console.log(msg.temp);
      this.setCapabilityValue('measure_temperature', value); // Fails horribly..
    });

  }

```

I’m getting a this.setCapabilityValue is not a function error when a message is received.  
Pretty sure this is me not understanding something fundamental, so any pointer in the right direction would be great.

---

<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:** [June 2, 2022, 10:14am UTC](https://community.homey.app/t/setcapability-of-device-on-receiving-mqtt-message/64315/2 "2022-06-02T10:14:57Z")

</div>

> [@fvrdam](#):
>
> Pretty sure this is me not understanding something fundamental

Correct: in normal functions, `this` will be bound to the function, not to the outside (in other words, `this` _inside_ the function is not the same as `this` _outside_ the function).

The easiest fix is to use arrow functions, which don’t have `this` binding:

```auto
    this.mqttconn.on('message', ( topic, message, packet) => {
      const msg = JSON.parse(message);
      console.log(msg.temp);
      this.setCapabilityValue('measure_temperature', value); // Fails horribly..
    });

```

---

<div class="post-metadata">

**Author:** ![fvrdam](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/fvrdam/32/47643_2.png) [@fvrdam](https://community.homey.app/u/fvrdam)\
**Post date:** [June 2, 2022, 1:22pm UTC](https://community.homey.app/t/setcapability-of-device-on-receiving-mqtt-message/64315/3 "2022-06-02T13:22:27Z")

</div>

Thank you !! Was running in the dark blindfolded.

This totally fixed it.
