# DIY circadian rhythm based lighting

**URL:** <https://community.homey.app/t/diy-circadian-rhythm-based-lighting/33348>\
**Category:** Developers\
**Tags:** app, homeyscript, flow\
**Created:** [August 7, 2020, 1:17pm UTC](https://community.homey.app/t/diy-circadian-rhythm-based-lighting/33348 "2020-08-07T13:17:53Z")\
**Posts on this page:** 1\
**Showing post:** 16

<div class="post-metadata">

**Author:** ![DirkG](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/dirkg/32/118897_2.png) [@DirkG](https://community.homey.app/u/DirkG)\
**Post date:** [May 12, 2021, 1:37pm UTC](https://community.homey.app/t/diy-circadian-rhythm-based-lighting/33348/16 "2021-05-12T13:37:44Z")

</div>

Great script and thanks for sharing it!  
In the meantime, I got it working with the help of @clipse_M.

The marked lines in the screenshot are probably intended to limit the dimming level to the upper and lower range, right?

 ![IMG_3098](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/f/5/f5cd07efe8aaf1126c2ec3f3fb328b743fee9d5e.jpeg)

Is this also possible with the temperature?

Because I have absolutely no programming knowledge, I would be happy if someone could change the script.

**EDIT**

I did it myself. If anyone is interested here is the code:

```
/**
* Adjust Color temperature and Brightness based on time of day
* as if it were April 16th, a nice long spring day in southern Sweden
*/
const VARIABLE_PREFIX = 'CircadianRhythm_';
const MIN_BRIGHTNESS = 0.35;
const MAX_BRIGHTNESS = 0.75;
const MIN_TEMPERATURE = 0.3;
const MAX_TEMPERATURE = 0.8;

const sys = await Homey.system.getInfo();

// hack because sys date no longer returns local time, but rather UTC :(
const regex = /(\d\d):(\d\d):(\d\d)/;
const timeMatches = sys.dateHuman.match(regex)

const solarNoon = 0.546 // on April 16 is 13:06

// defaults
let temperature = MIN_TEMPERATURE; // Homey color temperature: 0 (coolest, midday) to 1 (warmest, sunset/sunrise)
let brightness = MIN_BRIGHTNESS; // [0-1]

// used standard normal distribution curve to derive equation [https://en.wikipedia.org/wiki/Normal_distribution#Standard_normal_distribution]
// equations adjusted for color temperature and brightness [https://codepen.io/suhajdab/pen/wvzpqdQ?editors=0010]

getTemperaturePoint = x => {
const r = 0.20; // adjust curve width
return Math.pow(Math.E, -0.3 * Math.pow((x - solarNoon) / r, 4));
}
getBrightnessPoint = x => {
const r = 0.28; // adjust curve width
return Math.min(1 * Math.pow(Math.E, -0.3 * Math.pow((x - solarNoon) / r, 8)), 1);
}

// round to 2 decimal places to avoid 'invalid token error' when reading in a flow
const roundDecimals = (num, places = 2) => {
return Math.round(num * Math.pow(10, places)) / Math.pow(10, places);
}

const dayStart = new Date(sys.date).setHours(0, 0);
const dayEnd = new Date(sys.date).setHours(23, 59);

const now = new Date(sys.date).setHours(timeMatches[1]); // adjust for borked timezones in Homey v5
const dayProgress = roundDecimals((now - dayStart) / (dayEnd - dayStart), 4);

console.log('At', new Date(now).toLocaleTimeString([], { hour12: false }));
console.log('Day progress:\t\t', `${Math.round(dayProgress * 100)}%`);

// use Better Logic app to store values and use later in Flows
let BLApp = await Homey.apps.getApp({ id: 'net.i-dev.betterlogic' });

// color temperature transitions from 1 to 0 to 1 during the day
temperature = roundDecimals((MAX_TEMPERATURE - (MAX_TEMPERATURE - MIN_TEMPERATURE) * getTemperaturePoint(dayProgress)));
console.log(`${VARIABLE_PREFIX}_temperature:\t`, temperature);

// brightness transitions from MIN to MAX to MIN during day
brightness = roundDecimals((MIN_BRIGHTNESS + (MAX_BRIGHTNESS - MIN_BRIGHTNESS) * getBrightnessPoint(dayProgress)));
console.log(`${VARIABLE_PREFIX}_brightness:\t`, brightness);

BLApp.apiPut(`${VARIABLE_PREFIX}temperature/${temperature}`);
BLApp.apiPut(`${VARIABLE_PREFIX}brightness/${brightness}`);

return true;
```

---

_[View the full topic](https://community.homey.app/t/diy-circadian-rhythm-based-lighting/33348)._
