Homey Community Forum

Homeyscript sleep / wait / countdown app

Hi there,

I hope you can help me :slight_smile:
I need a sleep function in HomeScript, found this:
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 :confused:
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

Moved to Developers.

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:

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');
}();

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)

You don’t have to use _.forEach:

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

Or mark the handler function as async:

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

:smiley: 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 :confused:

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.