Hi everyone,
I have an RGB lamp (for example, an IKEA lamp), and I’m trying to create a sunrise effect at home to wake up in a pleasant way.
However, my current approach isn’t working as expected. Has anyone done something similar before? I’d love to code this myself and would appreciate any advice or suggestions.
Thanks in advance!
Simulate Sunrise
const currentTime = flow.get('current_time') || 0;
const totalTime = flow.get('total_time') || 1800; // 30 minutes
const brightnessSchedule = [
{ time: 0, brightness: 1, colorTemp: 2200 },
{ time: 300, brightness: 20, colorTemp: 2500 },
{ time: 600, brightness: 35, colorTemp: 2800 },
{ time: 720, brightness: 42, colorTemp: 3000 },
{ time: 840, brightness: 48, colorTemp: 3100 },
{ time: 900, brightness: 50, colorTemp: 3200 },
{ time: 1200, brightness: 60, colorTemp: 3500 },
{ time: 1500, brightness: 65, colorTemp: 3800 },
{ time: 1800, brightness: 70, colorTemp: 4000 }
];
const dawnToday = flow.get("dawnToday") || 0;
const now = Date.now();
if (currentTime >= totalTime || now > dawnToday + totalTime * 1000) {
node.status({ fill: "green", shape: "dot", text: "Sunrise completed 🌞 Light OFF" });
msg.payload = { service: "turn_off", entity_id: "light.main_door_light" };
flow.set('current_time', 0);
return msg;
}
let brightness = 1, colorTemp = 2200;
for (let i = 0; i < brightnessSchedule.length; i++) {
if (currentTime >= brightnessSchedule[i].time) {
brightness = brightnessSchedule[i].brightness;
colorTemp = brightnessSchedule[i].colorTemp;
}
}
flow.set('current_time', currentTime + 60);
msg.payload = { brightness: brightness, color_temp: colorTemp };
node.status({ fill: "blue", shape: "ring", text: `Brightness: ${brightness}%, Temp: ${colorTemp}K` });
node.debug(`🌅 Updating sunrise: Brightness ${brightness}%, Color Temp ${colorTemp}K`);
return msg;
Check Dawn Time
if (!msg.data || !msg.data.attributes || !msg.data.attributes.today || !msg.data.attributes.tomorrow) {
node.error("⚠️ Missing 'today' or 'tomorrow' attribute in sensor.home_sun_dawn. Please check the sensor configuration.", msg);
return null;
}
const dawnToday = new Date(msg.data.attributes.today).getTime();
const dawnTomorrow = new Date(msg.data.attributes.tomorrow).toLocaleString();
const now = Date.now();
const sunriseDuration = 30 * 60 * 1000;
const endSunriseTime = dawnToday + sunriseDuration;
flow.set("dawnToday", dawnToday);
flow.set("total_time", sunriseDuration / 1000);
flow.set("current_time", 0);
if (now >= dawnToday && now <= endSunriseTime) {
const remainingMinutes = Math.round((endSunriseTime - now) / 60000);
node.status({ fill: "green", shape: "dot", text: `🌅 Sunrise running | ${remainingMinutes} min left | Ends: ${new Date(endSunriseTime).toLocaleTimeString()}` });
return msg;
} else {
return null;
}