This code set a given dim level for a light device:
device.setCapabilityValue('dim', dim)
But how do I get it done with a duration? Like dimming from current level to requested target level over a time of 5 minutes, like on the flow cards. This is for Homeyscript.
//Dim++ a light in steps from current dimlevel to 'myDimlevel'
// present the arguments like the next JSON line, and adjust the values to your needs:
// {"myLight":"Lamp Bank R","myDimlevel":0.45,"waitForDimlevel":1000,"myDimlevelStep":0.01}
// Note: String values should be surrounded with ""
let myArgs = JSON.parse(args[0]);
console.log(`A check of the entered arguments: myLight=${myArgs.myLight}, myDimlevel=${myArgs.myDimlevel}, waitForDimlevel=${myArgs.waitForDimlevel}, myDimlevelStep=${myArgs.myDimlevelStep}`)
if (myArgs.myLight == '' || myArgs.myLight == undefined ) {
console.log ('Missing argument, enter a device name at flowcard argument field');
return(false);
} else {
// OPTIONAL: Turn the light on, to be able to get the current dim value.
// This is needed for Ikea lights for example.
// But not for KaKu/CoCo and Tuya wifi lights
let device_id
const devices1 = await Homey.devices.getDevices({ filter: {name: myArgs.myLight} });
_.forEach(devices1, device => {
device.setCapabilityValue('onoff', true)
log(` --> ${device.name} was turned on`)
});
log(`\Wait for 2000 millseconds for light to return its dim level`);
await wait(2000)
.then(() => log('OK'))
.catch(error => log(`Whoops an Error!:`, error));
// Get all devices
const devices = await Homey.devices.getDevices();
// Loop over all devices
for (const device of Object.values(devices)) {
if (device.name == myArgs.myLight) {
// If this device is a light (class)
// Or this is a 'What's plugged in?'-light (virtualClass)
if (device.class === 'light' || device.virtualClass === 'light' || device.class == 'button') {
// Set currentDimlvl to the entered myDimlevelStart value
currentDimlvl = device.capabilitiesObj.dim.value;
// Just to inform you while testing
await log(`\nCurrent Dim level: ${currentDimlvl} \nWait for ${myArgs.waitForDimlevel}ms between steps of ${myArgs.myDimlevelStep} `)
// this works, but myDimlevelStep has to be rounded
for (let step = Number(currentDimlvl); step <= myArgs.myDimlevel; step += Number(myArgs.myDimlevelStep)) {
await wait(myArgs.waitForDimlevel);
step = Math.round((step)*100)/100; // round to 0.00
log(`\nDimming ${device.name} to ${step}`)
// increasing the dim level
await device.setCapabilityValue('dim', step)
.then(() => log('OK'))
.catch(error => log(`Whoops an Error!:`, error));
// just in case, quit if 'step' has become more than '1'
if (step > 1)
break;
return(step);
}
}
}
}
}
return(true);
I got the idea you wanted a Homeyscript solution but I now understand you looked for a sort of ādurationā command.
I only know you ādurationā is not available in general.
I.e. for Hue and Ikea lights and probably some other brands it is available;
For, like, KaKu/CoCo and Tuya wifi lights it is not.
In the Advanced flow editor it is a bit hidden, youāll have to right-click the action card, it then shows the duration option:
Result:
.
But, duration is not a capability:
This is the same Ikea light @ developer page:
Yep depends on the device,
Some drivers implement it fe if the device can do it self.
if it isnāt implemented you need to create flows with fe Transitions or create a HomeyScript.
I know, itās present in the Chronograph app too.
The downside is, it has to start a flow for every step. High chance of āthis flow is disabled while it triggered too oftenā kind of errors.
But those apps are perfect for a wake-up light f.i., while thereās more time between the steps.
Yes, of course
Actually, when this thread starts, i scribed also reply here (but not posted) - recommending to use a devices internal properties/logic to do the dim. Yes, many of dimmers have possibility to set dim step and time between steps, so actually needed to set next dim level and (may-be only first time) step & time. But, as i told, not posted the reply, because max duration of such sleep dim is usually around one to couple of minutes (max 100 (%) steps and usually one byte for step counts. Quite limited possibilities for really long wake-up, but quite sufficient for other purposes - i use for example for āevacuationā from TV-room. When switching off TV then lights start dim down and givinā ~15sec to go into next room.