# 2 device crontasks and latest added one is run twice, 1st is ignored

**URL:** https://community.homey.app/t/2-device-crontasks-and-latest-added-one-is-run-twice-1st-is-ignored/9350
**Category:** Developers
**Created:** [February 18, 2019, 12:59pm UTC](https://community.homey.app/t/2-device-crontasks-and-latest-added-one-is-run-twice-1st-is-ignored/9350 "2019-02-18T12:59:15Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Edwin\_Biemond](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/edwin_biemond/32/72686_2.png) [@Edwin\_Biemond](https://community.homey.app/u/Edwin_Biemond)
#### Post date: [February 18, 2019, 12:59pm UTC](https://community.homey.app/t/2-device-crontasks-and-latest-added-one-is-run-twice-1st-is-ignored/9350/1 "2019-02-18T12:59:15Z")

</div>

Hi devs,

I hope you can help me with this. It is probably something stupid. I added 2 devices of the same type with a different api key and both need to have a crontask which runs every 5 min. With 1 device there is no problem and with 2 devices , the latest is running twice and 1st I don’t see it anymore

thanks

the code [https://github.com/biemond/com.athom.zevercloud/blob/master/drivers/plant/device.js#L21](https://github.com/biemond/com.athom.zevercloud/blob/master/drivers/plant/device.js#L21)

the output with the double [ManagerDrivers] [plant] [1] refresh now 1:45 PM

````auto
2019-02-18 13:40:08 [log] [MyApp] ZeverCloud app is running...
2019-02-18 13:40:08 [log] [ManagerDrivers] [plant] Plant driver has been inited
2019-02-18 13:41:00 [log] [ManagerDrivers] [plant] Plant id jxpyf
2019-02-18 13:41:09 [log] [ManagerDrivers] [plant] [0] plant has been inited
2019-02-18 13:41:09 [log] [ManagerDrivers] [plant] [0] key abc
2019-02-18 13:41:09 [log] [ManagerDrivers] [plant] [0] device settings {"apikey":"abc"}
2019-02-18 13:41:10 [log] [ManagerDrivers] [plant] [0] device added: g2ur4
2019-02-18 13:41:10 [log] [ManagerDrivers] [plant] [0] The task has not been registered yet, registering task: zeversolar_plant_g2ur4
2019-02-18 13:41:10 [log] [ManagerDrivers] [plant] [0] pollSeverCloud settings {"apikey":"abc"}
url /api/v1/getPlantOverview?key=abc
2019-02-18 13:41:10 [log] [ManagerDrivers] [plant] ZeverCloud - Pairing is finished (done or aborted) 
2019-02-18 13:41:11 [log] [ManagerDrivers] [plant] [0] refresh now 1:41 PM
2019-02-18 13:41:11 [log] [ManagerDrivers] [plant] [0] Received data
2019-02-18 13:41:11 [log] [ManagerDrivers] [plant] [0] object {"sid":8025,"ludt":"2019-02-18 13:41:24","E-Today":{"unit":"kWh","value":5.5},"E-Month":{"unit":"kWh","value":81.22},"E-Total":{"unit":"MWh","value":4.78},"TotalYield":{"unit":"€","value":3343.64},"CO2Avoided":{"unit":"T","value":3.82},"Power":{"unit":"kW","value":1.38}}
2019-02-18 13:41:11 [log] [ManagerDrivers] [plant] [0] update date 2019-02-18 13:41:24
2019-02-18 13:41:11 [log] [ManagerDrivers] [plant] [0] triggerPowerAbove1000WFlow

2019-02-18 13:41:26 [log] [ManagerDrivers] [plant] Plant id vakha
2019-02-18 13:41:54 [log] [ManagerDrivers] [plant] [1] plant has been inited
2019-02-18 13:41:54 [log] [ManagerDrivers] [plant] [1] key cde
2019-02-18 13:41:54 [log] [ManagerDrivers] [plant] [1] device settings {"apikey":"cde"}
2019-02-18 13:41:54 [log] [ManagerDrivers] [plant] [1] device added: zl6o5
2019-02-18 13:41:54 [log] [ManagerDrivers] [plant] [1] The task has not been registered yet, registering task: zeversolar_plant_zl6o5
2019-02-18 13:41:54 [log] [ManagerDrivers] [plant] [1] pollSeverCloud settings {"apikey":"cde"}
url /api/v1/getPlantOverview?key=cde
2019-02-18 13:41:55 [log] [ManagerDrivers] [plant] [1] refresh now 1:41 PM
2019-02-18 13:41:55 [log] [ManagerDrivers] [plant] [1] Received data
2019-02-18 13:41:55 [log] [ManagerDrivers] [plant] [1] object {"sid":53121,"ludt":"2019-02-18 13:41:30","E-Today":{"unit":"kWh","value":2.3},"E-Month":{"unit":"kWh","value":25.36},"E-Total":{"unit":"kWh","value":304.37},"TotalYield":{"unit":"€","value":212.99},"CO2Avoided":{"unit":"kg","value":243.5},"Power":{"unit":"W","value":232}}
2019-02-18 13:41:55 [log] [ManagerDrivers] [plant] [1] update date 2019-02-18 13:41:30
2019-02-18 13:41:55 [log] [ManagerDrivers] [plant] [1] triggerPowerAbove100WFlow
2019-02-18 13:41:55 [log] [ManagerDrivers] [plant] ZeverCloud - Pairing is finished (done or aborted) 
2019-02-18 13:45:00 [log] [ManagerDrivers] [plant] [1] pollSeverCloud settings {"apikey":"cde"}
url /api/v1/getPlantOverview?key=cde
2019-02-18 13:45:00 [log] [ManagerDrivers] [plant] [1] pollSeverCloud settings {"apikey":"cde"}
url /api/v1/getPlantOverview?key=cde

2019-02-18 13:45:00 [log] [ManagerDrivers] [plant] [1] refresh now 1:45 PM
2019-02-18 13:45:00 [log] [ManagerDrivers] [plant] [1] Received data
2019-02-18 13:45:00 [log] [ManagerDrivers] [plant] [1] object {"sid":53121,"ludt":"2019-02-18 13:41:30","E-Today":{"unit":"kWh","value":2.3},"E-Month":{"unit":"kWh","value":25.36},"E-Total":{"unit":"kWh","value":304.37},"TotalYield":{"unit":"€","value":212.99},"CO2Avoided":{"unit":"kg","value":243.5},"Power":{"unit":"W","value":232}}
2019-02-18 13:45:00 [log] [ManagerDrivers] [plant] [1] update date 2019-02-18 13:41:30

2019-02-18 13:45:02 [log] [ManagerDrivers] [plant] [1] refresh now 1:45 PM
2019-02-18 13:45:02 [log] [ManagerDrivers] [plant] [1] Received data
2019-02-18 13:45:02 [log] [ManagerDrivers] [plant] [1] object {"sid":53121,"ludt":"2019-02-18 13:41:30","E-Today":{"unit":"kWh","value":2.3},"E-Month":{"unit":"kWh","value":25.36},"E-Total":{"unit":"kWh","value":304.37},"TotalYield":{"unit":"€","value":212.99},"CO2Avoided":{"unit":"kg","value":243.5},"Power":{"unit":"W","value":232}}
2019-02-18 13:45:02 [log] [ManagerDrivers] [plant] [1] update date 2019-02-18 13:41:clock1030: ```
````

---

<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 18, 2019, 1:38pm UTC](https://community.homey.app/t/2-device-crontasks-and-latest-added-one-is-run-twice-1st-is-ignored/9350/2 "2019-02-18T13:38:56Z")

</div>

Instead of this:

```auto
task.on('run', () => this.pollSeverCloud(settings))

```

Can you try this:

```auto
task.on('run', settings => this.pollSeverCloud(settings))

```

---

<div class="post-metadata">

### Author: ![Edwin\_Biemond](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/edwin_biemond/32/72686_2.png) [@Edwin\_Biemond](https://community.homey.app/u/Edwin_Biemond)
#### Post date: [February 18, 2019, 2:27pm UTC](https://community.homey.app/t/2-device-crontasks-and-latest-added-one-is-run-twice-1st-is-ignored/9350/3 "2019-02-18T14:27:29Z")

</div>

> [@robertklep](#):
>
> settings =\> this.pollSeverCloud(settings))

thanks, almost there but that is already better.  
only need to solve that it updates the right device. I see 2 times plant 1 instead of also 0 with 1

Do I need to add more like add device to the crontask.

thanks again.

---

<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 18, 2019, 2:37pm UTC](https://community.homey.app/t/2-device-crontasks-and-latest-added-one-is-run-twice-1st-is-ignored/9350/4 "2019-02-18T14:37:41Z")

</div>

So even though both `run` handlers run “inside” of device 1, you get the settings for device 0 in one handler and for device 1 in the other handler?

---

<div class="post-metadata">

### Author: ![Edwin\_Biemond](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/edwin_biemond/32/72686_2.png) [@Edwin\_Biemond](https://community.homey.app/u/Edwin_Biemond)
#### Post date: [February 18, 2019, 2:41pm UTC](https://community.homey.app/t/2-device-crontasks-and-latest-added-one-is-run-twice-1st-is-ignored/9350/5 "2019-02-18T14:41:52Z")

</div>

correct

all is in plant 1 , settings is from plant 0 and 1 of plant 1

````auto
2019-02-18 15:40:00 [log] [ManagerDrivers] [plant] [1] pollSeverCloud device name zeversolar_plant_1p5bc
2019-02-18 15:40:00 [log] [ManagerDrivers] [plant] [1] pollSeverCloud device settings {"id":"1p5bc","apikey":"bbbbbbb"}
node_modules settings {"id":"cqhi8","apikey":"aaaaaa"}
node_modules url /api/v1/getPlantOverview?key=aaaaaa

2019-02-18 15:40:00 [log] [ManagerDrivers] [plant] [1] pollSeverCloud settings {"id":"1p5bc","apikey":"bbbbbbb"}
2019-02-18 15:40:00 [log] [ManagerDrivers] [plant] [1] pollSeverCloud device name zeversolar_plant_1p5bc
2019-02-18 15:40:00 [log] [ManagerDrivers] [plant] [1] pollSeverCloud device settings {"id":"1p5bc","apikey":"bbbbbbb"}
node_modules settings {"id":"1p5bc","apikey":"bbbbbbb"}
node_modules url /api/v1/getPlantOverview?key=bbbbbbb

2019-02-18 15:40:01 [log] [ManagerDrivers] [plant] [1] refresh now 3:40 PM
2019-02-18 15:40:01 [log] [ManagerDrivers] [plant] [1] Received data
2019-02-18 15:40:01 [log] [ManagerDrivers] [plant] [1] object {"sid":8025,"ludt":"2019-02-18 15:41:24","E-Today":{"unit":"kWh","value":7.4},"E-Month":{"unit":"kWh","value":83.32},"E-Total":{"unit":"MWh","value":4.78},"TotalYield":{"unit":"€","value":3345.11},"CO2Avoided":{"unit":"T","value":3.82},"Power":{"unit":"W","value":583}}
2019-02-18 15:40:01 [log] [ManagerDrivers] [plant] [1] update date 2019-02-18 15:41:24

2019-02-18 15:40:02 [log] [ManagerDrivers] [plant] [1] refresh now 3:40 PM
2019-02-18 15:40:02 [log] [ManagerDrivers] [plant] [1] Received data
2019-02-18 15:40:02 [log] [ManagerDrivers] [plant] [1] object {"sid":53121,"ludt":"2019-02-18 15:37:30","E-Today":{"unit":"kWh","value":2.6},"E-Month":{"unit":"kWh","value":25.76},"E-Total":{"unit":"kWh","value":304.67},"TotalYield":{"unit":"€","value":213.27},"CO2Avoided":{"unit":"kg","value":243.74},"Power":{"unit":"W","value":185}}```
````

---

<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 18, 2019, 2:48pm UTC](https://community.homey.app/t/2-device-crontasks-and-latest-added-one-is-run-twice-1st-is-ignored/9350/6 "2019-02-18T14:48:56Z")

</div>

That’s very strange, because it would mean that the `run` handler for one task is _also_ being called for the other task.

Out of curiosity, can you try this code? I assume that you’re running firmware v2, which I’m not, so can’t test myself:

```auto
const Homey = require('homey');

module.exports = class MyApp extends Homey.App {

  async onInit() {
    this.log('MyApp is running');
    const task1 = await this.task('task1');
    const task2 = await this.task('task2');

    task1.on('run', settings => {
      this.log('RUN#1', settings);
    });

    task2.on('run', settings => {
      this.log('RUN#2', settings);
    });
  }

  async task(name) {
    try {
      return await Homey.ManagerCron.getTask(name);
      this.log('YES', name);
    } catch(e) {
      this.log('NO ', name);
      return await Homey.ManagerCron.registerTask(name, '* * * * *', { name })
    }
  }
}

```

(run that instead of your normal `app.js`)

---

<div class="post-metadata">

### Author: ![Edwin\_Biemond](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/edwin_biemond/32/72686_2.png) [@Edwin\_Biemond](https://community.homey.app/u/Edwin_Biemond)
#### Post date: [February 18, 2019, 2:52pm UTC](https://community.homey.app/t/2-device-crontasks-and-latest-added-one-is-run-twice-1st-is-ignored/9350/7 "2019-02-18T14:52:48Z")

</div>

````auto
2019-02-18 15:51:57 [log] [MyApp] MyApp is running
2019-02-18 15:51:57 [log] [MyApp] NO task1
2019-02-18 15:51:57 [log] [ManagerDrivers] [plant] Plant driver has been inited
2019-02-18 15:51:57 [log] [MyApp] NO task2
2019-02-18 15:52:00 [log] [MyApp] RUN#2 { name: 'task2' }
2019-02-18 15:52:00 [log] [MyApp] RUN#2 { name: 'task1' }```
````

---

<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 18, 2019, 2:58pm UTC](https://community.homey.app/t/2-device-crontasks-and-latest-added-one-is-run-twice-1st-is-ignored/9350/8 "2019-02-18T14:58:31Z")

</div>

Okay, that looks like a bug to me! It’s triggering `run` on the second task only.

---

<div class="post-metadata">

### Author: ![Edwin\_Biemond](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/edwin_biemond/32/72686_2.png) [@Edwin\_Biemond](https://community.homey.app/u/Edwin_Biemond)
#### Post date: [February 18, 2019, 3:00pm UTC](https://community.homey.app/t/2-device-crontasks-and-latest-added-one-is-run-twice-1st-is-ignored/9350/9 "2019-02-18T15:00:54Z")

</div>

thanks for all your help, really great.

what’s next I can report this issue somewhere.

---

<div class="post-metadata">

### Author: ![Edwin\_Biemond](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/edwin_biemond/32/72686_2.png) [@Edwin\_Biemond](https://community.homey.app/u/Edwin_Biemond)
#### Post date: [February 18, 2019, 3:09pm UTC](https://community.homey.app/t/2-device-crontasks-and-latest-added-one-is-run-twice-1st-is-ignored/9350/10 "2019-02-18T15:09:46Z")

</div>

I see this one

> <https://github.com/athombv/homey-apps-sdk-issues/issues/50>

---

<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 18, 2019, 3:15pm UTC](https://community.homey.app/t/2-device-crontasks-and-latest-added-one-is-run-twice-1st-is-ignored/9350/11 "2019-02-18T15:15:59Z")

</div>

Yeah, that’s the same problem ☹
