# Dim with duration?

**URL:** <https://community.homey.app/t/dim-with-duration/67210>\
**Category:** Questions & Help\
**Tags:** homeyscript\
**Created:** [August 11, 2022, 12:49pm UTC](https://community.homey.app/t/dim-with-duration/67210 "2022-08-11T12:49:08Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![henit](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/henit/32/27054_2.png) [@henit](https://community.homey.app/u/henit)\
**Post date:** [August 11, 2022, 12:49pm UTC](https://community.homey.app/t/dim-with-duration/67210/1 "2022-08-11T12:49:09Z")

</div>

This code set a given dim level for a light device:

```auto
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.

---

<div class="post-metadata">

**Author:** ![Dijker](https://avatars.discourse-cdn.com/v4/letter/d/b9bd4f/32.png) [@Dijker](https://community.homey.app/u/Dijker)\
**Post date:** [August 11, 2022, 1:00pm UTC](https://community.homey.app/t/dim-with-duration/67210/2 "2022-08-11T13:00:43Z")

</div>

is described here:

> **[Capabilities](https://apps.developer.homey.app/the-basics/devices/capabilities#duration)**
>
> A capability is a programmatic representation of a device's state.

---

<div class="post-metadata">

**Author:** ![henit](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/henit/32/27054_2.png) [@henit](https://community.homey.app/u/henit)\
**Post date:** [August 11, 2022, 1:23pm UTC](https://community.homey.app/t/dim-with-duration/67210/3 "2022-08-11T13:23:29Z")

</div>

Can registerCapabilityListener be used in Homeyscript?

---

<div class="post-metadata">

**Author:** ![Dijker](https://avatars.discourse-cdn.com/v4/letter/d/b9bd4f/32.png) [@Dijker](https://community.homey.app/u/Dijker)\
**Post date:** [August 11, 2022, 1:36pm UTC](https://community.homey.app/t/dim-with-duration/67210/4 "2022-08-11T13:36:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)\
**Post date:** [August 11, 2022, 3:03pm UTC](https://community.homey.app/t/dim-with-duration/67210/5 "2022-08-11T15:03:39Z")

</div>

I think you might want to try this:

 ![Screenshot from 2022-08-11 17-01-34](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/d/6/d6cc0503cd01a4d9def84b3c485fa6867aa5a0e8.png)

The script:

```auto
//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 🙃

---

<div class="post-metadata">

**Author:** ![henit](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/henit/32/27054_2.png) [@henit](https://community.homey.app/u/henit)\
**Post date:** [August 11, 2022, 5:16pm UTC](https://community.homey.app/t/dim-with-duration/67210/6 "2022-08-11T17:16:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)\
**Post date:** [August 11, 2022, 6:57pm UTC](https://community.homey.app/t/dim-with-duration/67210/7 "2022-08-11T18:57:38Z")

</div>

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:  
 ![Screenshot from 2022-08-11 20-55-50](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/5/a/5a6b35a747e2ce1c9504580606d4424e1c6afd7e.png)

Result:  
 ![Screenshot from 2022-08-11 20-58-23](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/6/0/60e7d1fcd80369d6e7c51bd0ee8ea1165e42b146.png)

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

 ![Screenshot from 2022-08-11 21-03-46](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/4/3/43164e40c65ecc28701fc66d1e43ed7917b90fb1.png)

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

---

<div class="post-metadata">

**Author:** ![Dijker](https://avatars.discourse-cdn.com/v4/letter/d/b9bd4f/32.png) [@Dijker](https://community.homey.app/u/Dijker)\
**Post date:** [August 11, 2022, 7:03pm UTC](https://community.homey.app/t/dim-with-duration/67210/8 "2022-08-11T19:03:40Z")

</div>

Yep depends on the device,  
 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/2/a/2a71bf5768680b420a1c644712ce2a3a25c20a5e.png)  
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.

---

<div class="post-metadata">

**Author:** ![Fabian\_Annequin](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/fabian_annequin/32/30342_2.png) [@Fabian\_Annequin](https://community.homey.app/u/Fabian_Annequin)\
**Post date:** [August 18, 2022, 2:55pm UTC](https://community.homey.app/t/dim-with-duration/67210/9 "2022-08-18T14:55:56Z")

</div>

Yess! Thanks!! 😍

---

<div class="post-metadata">

**Author:** ![Ronald\_Otto](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/ronald_otto/32/51743_2.png) [@Ronald\_Otto](https://community.homey.app/u/Ronald_Otto)\
**Post date:** [August 18, 2022, 2:58pm UTC](https://community.homey.app/t/dim-with-duration/67210/10 "2022-08-18T14:58:43Z")

</div>

There is an app for that. [Transitions App voor Homey | Homey](https://homey.app/nl-nl/app/name.klep.transitions/Transitions/)

---

<div class="post-metadata">

**Author:** ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)\
**Post date:** [August 18, 2022, 3:28pm UTC](https://community.homey.app/t/dim-with-duration/67210/11 "2022-08-18T15:28:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Cuprum](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/cuprum/32/41409_2.png) [@Cuprum](https://community.homey.app/u/Cuprum)\
**Post date:** [August 19, 2022, 4:16am UTC](https://community.homey.app/t/dim-with-duration/67210/12 "2022-08-19T04:16:55Z")

</div>

> [@Peter\_Kawa](#):
>
> 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.
