# Homeyscript sleep / wait / countdown app

**URL:** https://community.homey.app/t/homeyscript-sleep-wait-countdown-app/13337
**Category:** Developers
**Created:** [May 19, 2019, 9:08am UTC](https://community.homey.app/t/homeyscript-sleep-wait-countdown-app/13337 "2019-05-19T09:08:12Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![adem\_p](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/adem_p/32/9485_2.png) [@adem\_p](https://community.homey.app/u/adem_p)
#### Post date: [May 19, 2019, 9:08am UTC](https://community.homey.app/t/homeyscript-sleep-wait-countdown-app/13337/1 "2019-05-19T09:08:12Z")

</div>

Hi there,

I hope you can help me 🙂  
I need a sleep function in HomeScript, found this:  
[https://lodash.com/docs/4.17.11#delay](https://lodash.com/docs/4.17.11#delay)  
And in the old forum:

```
async function wait (timeout) {
  return new Promise((resolve) => {
    _.delay(function(text) {
        resolve();
    }, timeout)
  })
}

```

and

```
function sleep(delay) {
    var start = new Date().getTime();
    while (new Date().getTime() < start + delay);
}

```

The sleep function is somehow completly ignored 😕  
With the wait function I get an error: wait can be only used as a async function… But I guess it is defined as a asyn func.

Does anyone have an example how to use a sleep in homescript, maybe in conjunction with countdown app?

regards adem

---

<div class="post-metadata">

### Author: ![Rocodamelshekima](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/rocodamelshekima/32/8209_2.png) [@Rocodamelshekima](https://community.homey.app/u/Rocodamelshekima)
#### Post date: [May 19, 2019, 9:14am UTC](https://community.homey.app/t/homeyscript-sleep-wait-countdown-app/13337/2 "2019-05-19T09:14:39Z")

</div>

Moved to Developers.

---

<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: [May 19, 2019, 9:28am UTC](https://community.homey.app/t/homeyscript-sleep-wait-countdown-app/13337/3 "2019-05-19T09:28:02Z")

</div>

That `sleep` function is blocking, which is a no-no in JavaScript. The `wait` function should work, provided that you use it in a function that _itself_ is marked `async`.

Try this:

```auto
async function wait (timeout) {
  return new Promise((resolve) => {
    _.delay(function(text) {
        resolve();
    }, timeout)
  })
}

// Alternative without lodash:
// async function wait(timeout) {
// return new Promise(resolve => setTimeout(resolve, timeout));
// }

void async function() {
  // This is where you can call `wait` and other `async` functions
  console.log(new Date(), 'waiting 5 seconds');
  await wait(5000);
  console.log(new Date(), 'waited 5 seconds');
}();

```

---

<div class="post-metadata">

### Author: ![adem\_p](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/adem_p/32/9485_2.png) [@adem\_p](https://community.homey.app/u/adem_p)
#### Post date: [May 19, 2019, 11:37am UTC](https://community.homey.app/t/homeyscript-sleep-wait-countdown-app/13337/4 "2019-05-19T11:37:41Z")

</div>

thank you robert I got it

> ```
> async function wait (timeout) {
> return new Promise((resolve) => {
> _.delay(function(text) {
> resolve();
> }, timeout)
> })
> }
> void async function() {
> // This is where you can call `wait` and other `async` functions
> console.log(new Date(), 'waiting 5 seconds');
> let devices = await Homey.devices.getDevices();
> 
> _.forEach(devices, device => {
> if(device.class != 'light') return;
> console.log(device.name);
> console.log(new Date(), 'waiting inside foreach');
> await wait(5000);
> console.log(new Date(), 'waited inside');
> device.setCapabilityValue('onoff', !device.state.onoff);
> });
> await wait(5000);
> console.log(new Date(), 'waited 5 seconds');
> }();
> return true;
> 
> ```

This is not working because the foreach is not a async function, so I have to iterate twice the devices or do some stuff in the first foreach… got it, tank you (top)

---

<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: [May 19, 2019, 11:46am UTC](https://community.homey.app/t/homeyscript-sleep-wait-countdown-app/13337/5 "2019-05-19T11:46:11Z")

</div>

> [@adem\_p](#):
>
> This is not working because the foreach is not a async function

You don’t have to use `_.forEach`:

```auto
for (let device of Object.values(devices)) {
  ...
}

```

Or mark the handler function as async:

```auto
_.forEach(devices, async (device) => {
  ...
});

```

---

<div class="post-metadata">

### Author: ![adem\_p](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/adem_p/32/9485_2.png) [@adem\_p](https://community.homey.app/u/adem_p)
#### Post date: [May 19, 2019, 11:59am UTC](https://community.homey.app/t/homeyscript-sleep-wait-countdown-app/13337/6 "2019-05-19T11:59:20Z")

</div>

😃 hihihi come to munich for a Radler  
nice, thank you.

maybe you have a solution for this also.  
i have a Flow, running a Script with Arguments. Is there a way to get the initial device, which has triggered the flow and to put it as a argument.Currently I’m writing by hand the device name. so I need to make this for every new flow, which will use this script.

it is used for motion detection, I would like to create a general script with the device.id or device.name of the initial triggered device.

any hint?

and sorry for the beginner questions 😕

---

<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: [May 19, 2019, 12:08pm UTC](https://community.homey.app/t/homeyscript-sleep-wait-countdown-app/13337/7 "2019-05-19T12:08:08Z")

</div>

Unless the driver for your motion detection device passes the device as a tag (which you can then pass as argument, if I’m not mistaken), I don’t think you can find out which device triggered the flow.
