Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var sunrise = "06:00"; //Hard-coded sunrise
- var sunset = "18:00"; //Hard-coded sunset
- var color_min = 2700; // Color temp at sunrise and sunset
- var color_max = 6500; // Color temp at noon
- function map(x, in_min, in_max, out_min, out_max)
- {
- return Math.round((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min);
- }
- //If light is operating in RGB, do not set the color_temp except if the the light
- //has just been turned on.
- if (msg.topic != "turn_on") {
- if (msg.payload.attributes.color_mode != "color_temp") {
- return;
- }
- }
- var today = new Date();
- var today_date = today.getFullYear() + '-' + (today.getMonth()+1) + '-' + today.getDate();
- var today_time = today.getHours() + ":" + String(today.getMinutes()).padStart(2, '0');
- var unix_sunrise = new Date(today_date + ' ' + sunrise).getTime() / 1000;
- var unix_sunset = new Date(today_date + ' ' + sunset).getTime() / 1000;
- var unix_now = new Date().getTime() / 1000;
- var brightness = ""
- var color_temp = color_min;
- var day_progress = (unix_now-unix_sunrise) / (unix_sunset-unix_sunrise);
- if (unix_now >= unix_sunrise && unix_now <= unix_sunset)
- {
- var color_sine = Math.sin(((360 * day_progress)-90) * (Math.PI/180));
- color_temp = map(color_sine, -1.0, 1.0, color_min, color_max);
- }
- day_progress = Math.round(day_progress * 100); //Round to a whole number
- node.status({text: color_temp + "K @ " + today_time + " (" + day_progress + "%)" });
- /*msg.payload = {domain: "light", service: "turn_on",
- data: {
- entity_id: msg.payload.entity_id,
- kelvin: color_temp, }
- };
- */
- //msg.payload.domain = "light";
- //msg.payload.service = "turn_on";
- //msg.payload.data = msg.payload.data || {}
- msg.payload.data.kelvin = color_temp
- return msg;
Advertisement
Add Comment
Please, Sign In to add comment