# Sunrise/sunset and timezone in Homey code

**URL:** <https://community.homey.app/t/sunrise-sunset-and-timezone-in-homey-code/143330>\
**Category:** Questions & Help\
**Created:** [September 23, 2025, 12:17pm UTC](https://community.homey.app/t/sunrise-sunset-and-timezone-in-homey-code/143330 "2025-09-23T12:17:21Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![portal](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/portal/32/125248_2.png) [@portal](https://community.homey.app/u/portal)\
**Post date:** [September 23, 2025, 12:17pm UTC](https://community.homey.app/t/sunrise-sunset-and-timezone-in-homey-code/143330/1 "2025-09-23T12:17:22Z")

</div>

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.

```auto
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

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

```

---

<div class="post-metadata">

**Author:** ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)\
**Post date:** [September 23, 2025, 1:26pm UTC](https://community.homey.app/t/sunrise-sunset-and-timezone-in-homey-code/143330/2 "2025-09-23T13:26:32Z")

</div>

> [@portal](#):
>
> but it keep using UTC timesone and not local time zone. Is there a setting I have missed out?

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

> ![Screenshot_20250923_152442_Chrome](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/2/3/23141006be6edc8967f790ff64ad1c3a320c6ba0.jpeg)

---

<div class="post-metadata">

**Author:** ![portal](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/portal/32/125248_2.png) [@portal](https://community.homey.app/u/portal)\
**Post date:** [September 23, 2025, 1:46pm UTC](https://community.homey.app/t/sunrise-sunset-and-timezone-in-homey-code/143330/3 "2025-09-23T13:46:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![DirkH](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/dirkh/32/78952_2.png) [@DirkH](https://community.homey.app/u/DirkH)\
**Post date:** [September 23, 2025, 1:54pm UTC](https://community.homey.app/t/sunrise-sunset-and-timezone-in-homey-code/143330/4 "2025-09-23T13:54:05Z")

</div>

Hi @portal ,  
if the Sun Event App does not fit your needs, probably [CronJob](https://homey.app/en-de/app/dev.schweiger.cron-job/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.

---

<div class="post-metadata">

**Author:** ![Ruurd\_van\_der\_Noord](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/ruurd_van_der_noord/32/71093_2.png) [@Ruurd\_van\_der\_Noord](https://community.homey.app/u/Ruurd_van_der_Noord)\
**Post date:** [September 23, 2025, 4:00pm UTC](https://community.homey.app/t/sunrise-sunset-and-timezone-in-homey-code/143330/5 "2025-09-23T16:00:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)\
**Post date:** [September 23, 2025, 8:17pm UTC](https://community.homey.app/t/sunrise-sunset-and-timezone-in-homey-code/143330/6 "2025-09-23T20:17:18Z")

</div>

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.

 ![Screenshot_20250923_221559_Firefox](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/b/e/be993ef91501b714f8be45ed50adc278f97f9e47.jpeg)

---

<div class="post-metadata">

**Author:** ![OH2TH](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/oh2th/32/111414_2.png) [@OH2TH](https://community.homey.app/u/OH2TH)\
**Post date:** [September 24, 2025, 7:32am UTC](https://community.homey.app/t/sunrise-sunset-and-timezone-in-homey-code/143330/7 "2025-09-24T07:32:49Z")

</div>

And as prayer times was mentioned, there is an app for that too [Prayers Alert App for Homey | Homey](https://homey.app/a/com.prayerssapp)

---

<div class="post-metadata">

**Author:** ![portal](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/portal/32/125248_2.png) [@portal](https://community.homey.app/u/portal)\
**Post date:** [September 24, 2025, 8:00am UTC](https://community.homey.app/t/sunrise-sunset-and-timezone-in-homey-code/143330/8 "2025-09-24T08:00:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![portal](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/portal/32/125248_2.png) [@portal](https://community.homey.app/u/portal)\
**Post date:** [September 24, 2025, 8:01am UTC](https://community.homey.app/t/sunrise-sunset-and-timezone-in-homey-code/143330/9 "2025-09-24T08:01:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![portal](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/portal/32/125248_2.png) [@portal](https://community.homey.app/u/portal)\
**Post date:** [September 24, 2025, 8:03am UTC](https://community.homey.app/t/sunrise-sunset-and-timezone-in-homey-code/143330/10 "2025-09-24T08:03:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DirkH](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/dirkh/32/78952_2.png) [@DirkH](https://community.homey.app/u/DirkH)\
**Post date:** [September 24, 2025, 1:02pm UTC](https://community.homey.app/t/sunrise-sunset-and-timezone-in-homey-code/143330/11 "2025-09-24T13:02:33Z")

</div>

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
