Hello,
I have a need to turn on th elights at sunset and turn them on sunrise. TGhe timings are a bit different than the official sunsrise and sunset, and hence I decided to import a table for the full year. Below code contains a sample of the table.
I have noticed that the lights turn on at teh right time (sunset), however it turn off a few hours before the sunrise. When I run the code in Test, I get the following (the time was 13:07 - the code showes the time is 2 hours behind):
Now (UTC): 2025-09-23T12:07:25.684Z
Today’s date: 23/09/2025
Schedule entries for today: 1
Checking entry: ON 19:39, OFF 07:22
TurnOnTime (adjusted UTC): 2025-09-23T17:39:00.000Z, TurnOffTime: 2025-09-24T05:22:00.000Z
Turning OFF the light.
Question 1: Now, I ave corrected this in teh code, and it works for turning on the lights, but does not work for turning off. I am missing something, but I am not sure what it is. I am also surprised that my Homey Pro device does not have the right timezone. I have been in teh Setting ssection in the app and added my city where I live, but it keep using UTC timesone and not local time zone. Is there a setting I have missed out?
Question 2: I would like to change the code as such that if the lights are already on at sunset, then the code does nothing, and it waiting until the time is to turn off the lights and then turn them off. What I am doing at the moment is to run this code in Flow and chacking every 30 second to make sure that I get the right time. This also means that if I wan to turn off the lights 2 hours aftrer sunset, then they will turn on again within 30 seconds. I hope I am able to explain my question.
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
const schedule = \[
{ date: ‘01/01/2025’, turn_on_time: ‘15:55’, turn_off_time: ‘09:29’ },
{ date: ‘02/01/2025’, turn_on_time: ‘15:56’, turn_off_time: ‘09:28’ },
{ date: ‘03/01/2025’, turn_on_time: ‘15:58’, turn_off_time: ‘09:28’ },
{ date: ‘04/01/2025’, turn_on_time: ‘15:59’, turn_off_time: ‘09:27’ },
\];
const Square = ‘xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx’;
const Circle = ‘xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx’;
const Stripe = ‘xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx’;
const Globe = ‘xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx’;
const now = new Date();
const today = now.toLocaleDateString(‘en-GB’); // Format: DD/MM/YYYY
console.log(`Now (UTC): ${now.toISOString()}`);
console.log(`Today's date: ${today}`);
const todaySchedule = schedule.filter(entry => entry.date === today);
console.log(`Schedule entries for today: ${todaySchedule.length}`);
let shouldTurnOn = false;
for (const entry of todaySchedule) {
const \[day, month, year\] = entry.date.split(‘/’);
const \[onHour, onMinute\] = entry.turn_on_time.split(‘:’).map(Number);
const \[offHour, offMinute\] = entry.turn_off_time.split(‘:’).map(Number);
// Adjust for UTC+2 (Norway summer time)
const turnOnTime = new Date(Date.UTC(year, month - 1, day, onHour - 2, onMinute));
let turnOffTime = new Date(Date.UTC(year, month - 1, day, offHour - 2, offMinute));
// If off time is earlier than on time, it means it’s on the next day
if (turnOffTime <= turnOnTime) {
turnOffTime.setUTCDate(turnOffTime.getUTCDate() + 1);
}
console.log(`Checking entry: ON ${entry.turn_on_time}, OFF ${entry.turn_off_time}`);
console.log(`TurnOnTime (adjusted UTC): ${turnOnTime.toISOString()}, TurnOffTime: ${turnOffTime.toISOString()}`);
if (now >= turnOnTime && now < turnOffTime) {
shouldTurnOn = true;
break;
}
}
console.log(shouldTurnOn ? ‘Turning ON the light.’ : ‘Turning OFF the light.’);
// \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
await Homey.devices.setCapabilityValue({
deviceId: Stripe,
capabilityId: ‘onoff’,
value: shouldTurnOn,
});
// \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
await Homey.devices.setCapabilityValue({
deviceId: Globe,
capabilityId: ‘onoff’,
value: shouldTurnOn,
});
if (shouldTurnOn) {
await Homey.devices.setCapabilityValue({
deviceId: Globe,
capabilityId: ‘dim’,
value: 1.0,
});
}
// \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
await Homey.devices.setCapabilityValue({
deviceId: Square,
capabilityId: ‘onoff’,
value: shouldTurnOn,
});
if (shouldTurnOn) {
await Homey.devices.setCapabilityValue({
deviceId: Square,
capabilityId: ‘dim’,
value: 1.0,
});
}
// \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
await Homey.devices.setCapabilityValue({
deviceId: Circle,
capabilityId: ‘onoff’,
value: shouldTurnOn,
});
if (shouldTurnOn) {
await Homey.devices.setCapabilityValue({
deviceId: Circle,
capabilityId: ‘dim’,
value: 1.0,
});
}

