Script taking long time

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

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));

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.

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:
console.log('before plafond', new Date());
await plafond.setCapabilityValue('onoff', false);
console.log('after plafond', new Date());