Sunrise/sunset and timezone in Homey code

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,
});
}

No. Homey’s time is UTC time since … years. In scripts you’ll have to do the math yourself. There are enough examples on this forum.

Why don’t you just use the Sun Events app? It’s very flexible.

2nd,
As you’ve probably noticed after posting your post, the code looks horrific and unreadable.
Please format code as “preformatted text”:
Add three backticks above and below the code: ```
Or,
Select all code, and tap this icon

Thank you for yoru reply. I didn’t realise that the code looked that bad. I will try to post it better.

I was not aware of the Sun Event app. However, as the sunrise (end of a prayer time) and sunset (start of a prayer time) is set, I find that there is difference in timing between what apps use and the set time I need. This is the reason why I did not use appls like Sun Event. I will see if there is possible to use own defined timings in the app.

Hi @portal ,
if the Sun Event App does not fit your needs, probably CronJob might be better for you. I don’t know about the complexity of prayer time, but if it changes just once or twice a month, that could be helpful.

To compare the time Homey uses against another time, you can pass the time Homey uses as an argument to a Homeyscript and evaluate that time.

Hi,.

I wanted to share Sun Events has way more events beside sunrise/set, plus you can use offsets (negative or positive) to fine tune the triggers.

And as prayer times was mentioned, there is an app for that too Prayers Alert App for Homey | Homey

Thank you! I am checking it out, however it looks like post installation it is not launching the configuration page. Will contact developer

Thank you for this. I have compared the sunset and sunrise versys the prayer times, and there is indeed a offset, but it is different during the year. I am now in communication with the developer to see if a table for full year can be uploaded as a subsititute for the standard table.

Thank you. There are 5 prayers per day, and the time changes every day. So the table is rather big :slight_smile: Further, as one move location, the prayer time schanges as well.

Hi @portal ,
I checked a bit about prayer time. It really seems as if they are linked to sun events, but not exactly. I found something about 18° for fadjr, but no explanation which angle is relevant. And probably it is fixed for some major places and minor points in the area have just to follow them. So probably either azimut or altitude of the sun event could help, if you pretend to be a special place. The alternative would be to read out the relevant website via a json handler or a https command, but I have no clue how to do this and which websites would work.
So have fun
Dirk