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?

Ah,
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');
    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);

.
@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

Result:
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,
image
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.