# Question regarding setInterval

**URL:** https://community.homey.app/t/question-regarding-setinterval/61138
**Category:** Developers
**Created:** [February 27, 2022, 11:04pm UTC](https://community.homey.app/t/question-regarding-setinterval/61138 "2022-02-27T23:04:20Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![reyntjensw](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/reyntjensw/32/34189_2.png) [@reyntjensw](https://community.homey.app/u/reyntjensw)
#### Post date: [February 27, 2022, 11:04pm UTC](https://community.homey.app/t/question-regarding-setinterval/61138/1 "2022-02-27T23:04:20Z")

</div>

Good evening

I’m having issues with understanding the usage of setInterval. (and just to be sure, I’m a complete Nodejs newbie)  
I have created a driver that is polling data from an api so this is my device.js file

```auto
    async onInit() {
        this.log('MyDevice has been initialized');

        if (this.hasCapability('measure_outside_temperature') === false) {
            // You need to check if migration is needed
            // do not call addCapability on every init!
            await this.addCapability('measure_outside_temperature');
        }
   
        const settings = this.getSettings();
        let username = this.homey.settings.get('username');
        let password = this.homey.settings.get('password');
        this.wemApi = new WemApi(username, password);
        await this.wemApi.initializeSession();
        let temperature = await this.wemApi.getIndoorTemperature(this.getData().id)
        this.setCapabilityValue('measure_temperature', temperature.NumericValue).catch(this.error);
        let outsideTemperature = await this.wemApi.getOutsideTemperature(this.getData().id)
        this.setCapabilityValue('measure_outside_temperature', outsideTemperature.NumericValue).catch(this.error);
    }

```

I’ve already tried replacing this with

```auto
    async onInit() {
        this.log('MyDevice has been initialized');

        if (this.hasCapability('measure_outside_temperature') === false) {
            // You need to check if migration is needed
            // do not call addCapability on every init!
            await this.addCapability('measure_outside_temperature');
        }
        this.homey.setInterval(async () => {
            await this.getData();
        }, 60000);
}
async getData() {
        try {
            const settings = this.getSettings();
            let username = this.homey.settings.get('username');
            let password = this.homey.settings.get('password');
            this.wemApi = new WemApi(username, password);
            await this.wemApi.initializeSession();
            let temperature = await this.wemApi.getIndoorTemperature(this.getData().id)
            this.setCapabilityValue('measure_temperature', temperature.NumericValue).catch(this.error);
            let outsideTemperature = await this.wemApi.getOutsideTemperature(this.getData().id)
            this.setCapabilityValue('measure_outside_temperature', outsideTemperature.NumericValue).catch(this.error);
            // this.setUnavailable();
        } catch {

        }
    }

```

But at this point I’m noticing that getData() is triggered almost constantly.

A second question is where the setInterval is put in the most ideal place?  
Should I put it in the app.js file or can it be put in the driver itself?

---

<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 28, 2022, 6:17am UTC](https://community.homey.app/t/question-regarding-setinterval/61138/2 "2022-02-28T06:17:33Z")

</div>

> [@reyntjensw](#):
>
> But at this point I’m noticing that getData() is triggered almost constantly.

That’s because you named your method to match an existing SDK method, and now you’re calling your own method recursively:

```auto
let temperature = await this.wemApi.getIndoorTemperature(this.getData().id)

```

> [@](#):
>
> A second question is where the setInterval is put in the most ideal place?  
> Should I put it in the app.js file or can it be put in the driver itself?

You’re now logging in every 60 seconds. If sessions can live longer, log in and create a session in `app.js` and use that from your _device_ (not driver) implementations:

```auto
// app.js
onInit() {
  let username = this.homey.settings.get('username');
  let password = this.homey.settings.get('password');
  this.wemApi = new WemApi(username, password);
  await this.wemApi.initializeSession();
}

// device.js
let temperature = await this.homey.app.wemApi.getIndoorTemperature(this.getData().id)

```

---

<div class="post-metadata">

### Author: ![reyntjensw](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/reyntjensw/32/34189_2.png) [@reyntjensw](https://community.homey.app/u/reyntjensw)
#### Post date: [February 28, 2022, 10:12am UTC](https://community.homey.app/t/question-regarding-setinterval/61138/3 "2022-02-28T10:12:42Z")

</div>

Hi Robert

Thanks for the info, one last issue I have is that the info is not getting updated in the app.  
I tried using setSettings call but the values did not get updated.

I do see the new values in the console tho.

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 28, 2022, 11:23am UTC](https://community.homey.app/t/question-regarding-setinterval/61138/4 "2022-02-28T11:23:58Z")

</div>

What info?

---

<div class="post-metadata">

### Author: ![reyntjensw](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/reyntjensw/32/34189_2.png) [@reyntjensw](https://community.homey.app/u/reyntjensw)
#### Post date: [February 28, 2022, 11:53am UTC](https://community.homey.app/t/question-regarding-setinterval/61138/5 "2022-02-28T11:53:22Z")

</div>

I’m querying an api to get the latest reading from a thermostat, but the latest readings do not appear in the app. When I check the readings, it say that it was updated 4 hours ago (even if the data is queried every minute)  
 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/8/2/823d0b920eb486f144a4ae73afa70d7c04a6ab1c.png)

For now this is the current code I have

```auto
    async onInit() {
        this.log('MyDevice has been initialized');

        if (this.hasCapability('measure_temperature.outside') === false) {
            // You need to check if migration is needed
            // do not call addCapability on every init!
            await this.addCapability('measure_temperature.outside');
        }
        this.homey.setInterval(async () => {
            await this.getProductionData();
        }, 60000);
        let username = this.homey.settings.get('username');
        let password = this.homey.settings.get('password');
        this.wemApi = new WemApi(username, password);
        await this.wemApi.initializeSession();
    }

    async getProductionData() {
        try {
            let temperature = await this.wemApi.getIndoorTemperature(this.getData().id)
            this.setCapabilityValue('measure_temperature', temperature.NumericValue).catch(this.error);
            let outsideTemperature = await this.wemApi.getOutsideTemperature(this.getData().id)
            this.setCapabilityValue('measure_temperature.outside', outsideTemperature.NumericValue).catch(this.error);
        } catch {

        }
    }

```

---

<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 28, 2022, 12:01pm UTC](https://community.homey.app/t/question-regarding-setinterval/61138/6 "2022-02-28T12:01:05Z")

</div>

Are you sure you’re getting updated values from the API? Also, empty `catch` statements will swallow all errors, so at least put a `this.log()` or `this.error()` inside them to provide feedback:

```auto
try {
  …
} catch(e) {
  this.log(e);
}
```

---

<div class="post-metadata">

### Author: ![reyntjensw](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/reyntjensw/32/34189_2.png) [@reyntjensw](https://community.homey.app/u/reyntjensw)
#### Post date: [February 28, 2022, 12:18pm UTC](https://community.homey.app/t/question-regarding-setinterval/61138/7 "2022-02-28T12:18:11Z")

</div>

The session was created to late and that is why the data was not shown.  
Thanks for hinting the catch part!

---

<div class="post-metadata">

### Author: ![reyntjensw](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/reyntjensw/32/34189_2.png) [@reyntjensw](https://community.homey.app/u/reyntjensw)
#### Post date: [March 4, 2022, 6:10am UTC](https://community.homey.app/t/question-regarding-setinterval/61138/8 "2022-03-04T06:10:42Z")

</div>

Hi Robert

I did some changes and released my first version but for some reason the temperatures are not being updated when it is installed (and not run into production).  
When the app starts, there are initial values, but afterwards no new updates appear.

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/a/f/af8707ba8adb882bd573d74bc88ecd37aea9d73a.jpeg)

When running in development, the api is showing the values in the console [Homey-Wemportal/device.js at main · reyntjensw/Homey-Wemportal · GitHub](https://github.com/reyntjensw/Homey-Wemportal/blob/main/drivers/burner/device.js#L42)

There must still be something I’m doing wrong.  
Do you know what?

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: [March 4, 2022, 6:23am UTC](https://community.homey.app/t/question-regarding-setinterval/61138/9 "2022-03-04T06:23:16Z")

</div>

No idea why it’s not working, the code looks good (especially if the correct values are also being shown in console). Does the issue with the values not updating happen on all platforms (mobile, web)? Do they also not update when you stop and restart the app (or force-reload the web app)?

---

<div class="post-metadata">

### Author: ![reyntjensw](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/reyntjensw/32/34189_2.png) [@reyntjensw](https://community.homey.app/u/reyntjensw)
#### Post date: [March 4, 2022, 6:27am UTC](https://community.homey.app/t/question-regarding-setinterval/61138/10 "2022-03-04T06:27:53Z")

</div>

Just checked, no updates are coming in when the app is restarted.  
Is there a way to write some logs to Homey so I can do some debugging when it is installed?

---

<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: [March 4, 2022, 6:46am UTC](https://community.homey.app/t/question-regarding-setinterval/61138/11 "2022-03-04T06:46:06Z")

</div>

> [@reyntjensw](#):
>
> Is there a way to write some logs to Homey so I can do some debugging when it is installed?

Nope. You can start a V8 inspector in your app by adding this to `onInit` in your app:

```auto
require('inspector').open(9229, '0.0.0.0');

```

Then connect to it from a Chrome browser by navigating to `chrome://inspect`

That allows you to the debug code (set breakpoints, inspect the stack, etc).

---

<div class="post-metadata">

### Author: ![reyntjensw](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/reyntjensw/32/34189_2.png) [@reyntjensw](https://community.homey.app/u/reyntjensw)
#### Post date: [March 7, 2022, 8:29am UTC](https://community.homey.app/t/question-regarding-setinterval/61138/12 "2022-03-07T08:29:06Z")

</div>

I’ve added the inspector to the code, but in the background the routines are still running and returning the correct values.  
It looks like the values are just not being updated in the interface, but I have no clue why it is not working.

---

<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: [March 7, 2022, 8:30am UTC](https://community.homey.app/t/question-regarding-setinterval/61138/13 "2022-03-07T08:30:23Z")

</div>

I assume that other apps _are_ showing their updated values in the interface?

---

<div class="post-metadata">

### Author: ![reyntjensw](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/reyntjensw/32/34189_2.png) [@reyntjensw](https://community.homey.app/u/reyntjensw)
#### Post date: [March 7, 2022, 9:20am UTC](https://community.homey.app/t/question-regarding-setinterval/61138/14 "2022-03-07T09:20:04Z")

</div>

They are indeed updating as it should.  
I also did a PTP on the Homey but that did not resolve the issue

---

<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: [March 7, 2022, 11:32am UTC](https://community.homey.app/t/question-regarding-setinterval/61138/15 "2022-03-07T11:32:21Z")

</div>

Perhaps ask around on the #developers channel of the [Homey Community Slack](https://slack.athom.com/).

---

<div class="post-metadata">

### Author: ![reyntjensw](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/reyntjensw/32/34189_2.png) [@reyntjensw](https://community.homey.app/u/reyntjensw)
#### Post date: [March 9, 2022, 9:54am UTC](https://community.homey.app/t/question-regarding-setinterval/61138/16 "2022-03-09T09:54:27Z")

</div>

I think I found the issue, when adding a await in front of the `this.setCapabilityValue` this issue got resolved.

---

<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: [March 9, 2022, 10:16am UTC](https://community.homey.app/t/question-regarding-setinterval/61138/17 "2022-03-09T10:16:10Z")

</div>

That sounds like `setCapabilityValue()` has an implementation issue… 🤔 It shouldn’t really matter if you wait for the promise to complete or not.

But glad to read you solved your problems 😃
