# Script taking long time

**URL:** <https://community.homey.app/t/script-taking-long-time/52940>\
**Category:** Questions & Help\
**Created:** [October 10, 2021, 3:04pm UTC](https://community.homey.app/t/script-taking-long-time/52940 "2021-10-10T15:04:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Johan\_Gustafsson](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/johan_gustafsson/32/16719_2.png) [@Johan\_Gustafsson](https://community.homey.app/u/Johan_Gustafsson)\
**Post date:** [October 10, 2021, 3:04pm UTC](https://community.homey.app/t/script-taking-long-time/52940/1 "2021-10-10T15:04:00Z")

</div>

Hi  
I’m not any good at java script but have basic knowledge of Html, PHP and C++ and some understanding of python. With that said I manage to come up with a script toggling my lights, triggered by a switch in a flow.  
However the issue is that it takes like 5s from I push the button until The lights change

Hardware  
wall switch, brand Hue ( cabinet)  
Ceiling light, brand Hue (plafond)  
Wall plug, brand Fibaro double switch

Is it because bad scripting or is it because I use a flow to trigger every thing.

The next state of the script is to incorporate some logic to evaluate if it day or night.  
if it night and the button is pressed only dim the ceiling to 10%  
If it day turn on the wall switch and dim the ceiling to 100%  
Can somebody point on the right direction?

The script so far

let plafond = await Homey.devices.getDevice({id : ‘de53b6b0-8032-4ca3-acd0-3054d9265565’});  
let cabinet = await Homey.devices.getDevice({id : ‘68b5cf24-13a6-4e76-9823-cc7e9946c568’});

if (plafond.capabilitiesObj.onoff.value)  
{ // Turn the light on by setting the capability `onoff` to `true`  
await plafond.setCapabilityValue(‘onoff’, false)  
.then(() =\> log(‘OK’))  
.catch(error =\> log(`Error:`, error));  
await cabinet.setCapabilityValue(‘onoff’, false)  
.then(() =\> log(‘OK’))  
.catch(error =\> log(`Error:`, error));  
} else {  
// Turn the light off by setting the capability `onoff` to `false`  
await cabinet.setCapabilityValue(‘onoff’, true)  
.then(() =\> log(‘OK’))  
.catch(error =\> log(`Error:`, error));  
await plafond.setCapabilityValue(‘onoff’, true)  
.then(() =\> log(‘OK’))  
.catch(error =\> log(`Error:`, error));  
}

---

<div class="post-metadata">

**Author:** ![JPe4619](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jpe4619/32/9017_2.png) [@JPe4619](https://community.homey.app/u/JPe4619)\
**Post date:** [October 10, 2021, 11:18pm UTC](https://community.homey.app/t/script-taking-long-time/52940/2 "2021-10-10T23:18:08Z")

</div>

You can run and debug your script in HomeyScript, there are also many script examples, for logging, it can be usefull to use different texts, so you know what line has been executed.

---

<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:** [October 11, 2021, 5:56am UTC](https://community.homey.app/t/script-taking-long-time/52940/3 "2021-10-11T05:56:06Z")

</div>

A few things to test to find out where the delays are coming from:

- run the script standalone from the Homeyscript editor;
- run a similar flow without the script (switching the devices from the flow instead of from the script);
- add a few log lines to your script, for instance before and after each `await` line, to see how long those take:

```auto
console.log('before plafond', new Date());
await plafond.setCapabilityValue('onoff', false);
console.log('after plafond', new Date());

```
