Dim with duration?

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.

is described here:

Can registerCapabilityListener be used in Homeyscript?

sorry I missed that it is for HomeyScript,
This is the Apps SDK.

I think you might want to try this:

The script:

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

@Fabian_Annequin I think you like this one :upside_down_face:

1 Like

So there is now way to instruct Homey to dim over a duration? You have to code the steps to dim over time manually?

I got the idea you wanted a Homeyscript solution :smile: 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:
Screenshot from 2022-08-11 20-55-50

Screenshot from 2022-08-11 20-58-23

But, duration is not a capability:
This is the same Ikea light @ developer page:

So that’s probably why ‘duration’ is not available in Homeyscript

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.

Yess! Thanks!! :heart_eyes:

1 Like

There is an app for that. Transitions App voor Homey | Homey

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 :wink:
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.