# Time calculation from sun down time to end time to show seconds countdown for turn off lights and show time on dashboard

**URL:** <https://community.homey.app/t/time-calculation-from-sun-down-time-to-end-time-to-show-seconds-countdown-for-turn-off-lights-and-show-time-on-dashboard/96134>\
**Category:** Questions & Help\
**Tags:** app, dashboard, homey-pro, local-time\
**Created:** [November 28, 2023, 7:43pm UTC](https://community.homey.app/t/time-calculation-from-sun-down-time-to-end-time-to-show-seconds-countdown-for-turn-off-lights-and-show-time-on-dashboard/96134 "2023-11-28T19:43:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kevin\_Ponsen](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/kevin_ponsen/32/92414_2.png) [@Kevin\_Ponsen](https://community.homey.app/u/Kevin_Ponsen)\
**Post date:** [November 28, 2023, 7:43pm UTC](https://community.homey.app/t/time-calculation-from-sun-down-time-to-end-time-to-show-seconds-countdown-for-turn-off-lights-and-show-time-on-dashboard/96134/1 "2023-11-28T19:43:19Z")

</div>

i have read many option on forum but i cant figure it out how or what to do to get it to work  
i want to calculate time from start sundown to self set end time 22:30

used variables are  
text / (Tijd Week) (set to 22:30)  
text / (Tijd Weekend) (set to 00:30)  
number / (Tuin Timer) (set in seconds)  
text / (Tijd Nu)

**IF** (Sundown in 0 min) **then** Set (Tijd Nu) with current time  
**IF** weekday **then** (Tijd Week) - (Tijd Nu) = result in minutes (\*60) = seconds  
**else** (Tijd Weekend) - (Tijd Nu) = result in minutes (\*60) = seconds  
**then** Set Result seconds to (Timer 1)

for example  
sundown 18:30 (time)=18:30  
end time 22:30 (endtime)  
remaining time 22:30 - 18.30 = 4 hour \* 3600 = 14400 sec (timer set 14400)

end result is then something like this  
 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/b/4/b4b5a4ac2a54cdc1817ec2a91afa467215a0aff5.png)

can someone please explain me how to make something like this or know what i can use for this to

---

<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:** [November 29, 2023, 2:53am UTC](https://community.homey.app/t/time-calculation-from-sun-down-time-to-end-time-to-show-seconds-countdown-for-turn-off-lights-and-show-time-on-dashboard/96134/2 "2023-11-29T02:53:24Z")

</div>

Better Logic Library can help here

> [@\[APP\]\[Pro\] Better Logic Library - For Users](https://community.homey.app/t/app-pro-better-logic-library-for-users/71876/100):
>
> BLL expressions within any text (variable) I am having more and more fun with BLL. The below was not obvious to me so I share here the power of the BLL card “Set variable to text”. [image] This means you can add one or more texts and BLL expressions into any text variable, pass the variable to a central flow where you process it with a single BLL card. This saves (me) several formatting cards in various flows.

> [@\[APP\]\[Pro\] Better Logic Library - For Users](https://community.homey.app/t/app-pro-better-logic-library-for-users/71876/28):
>
> Well, you can kind off, but not in the easy ‘formatting’ way: because i didn’t find a all-around solutions independend of locale and timezone. date('DATETIME', null, 'en') You will get a tag, you can use the last number if it’s like this. But if you get this, you will probably get the same result as me: So, the complete solution would be: new Date().setHours(18-Number.parseInt(\_.last(date('DATETIME', null, 'en'))),0,0,0) - Date.now() \< 15\*60\*1000; (this is true when less the…

> [@The Flow Exchange(r) - Exchange Your Flows with Others!](https://community.homey.app/t/the-flow-exchange-r-exchange-your-flows-with-others/68981/72):
>
> Time Calculator With the re-invented Better Logic Library app, we can calculate with time a bit easier without Homeyscript. Apps used: [Better Logic Library](https://homey.app/a/net.i-dev.betterlogic)Other things people need to do to get it working correct? Any questions can be asked in this topic: [https://community.homey.app/t/app-pro-better-logic-library-for-users/71876](https://community.homey.app/t/app-pro-better-logic-library-for-users/71876) TEF: [tef:FLOWS:"H4sIAAAAAAACA7VayXIbyRH9FUbHHGYiVHLtC472ZQ7jk3wjGYxasiiEsd…

---

<div class="post-metadata">

**Author:** ![Kevin\_Ponsen](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/kevin_ponsen/32/92414_2.png) [@Kevin\_Ponsen](https://community.homey.app/u/Kevin_Ponsen)\
**Post date:** [November 29, 2023, 4:07pm UTC](https://community.homey.app/t/time-calculation-from-sun-down-time-to-end-time-to-show-seconds-countdown-for-turn-off-lights-and-show-time-on-dashboard/96134/3 "2023-11-29T16:07:48Z")

</div>

thanks for you reply after 2 hours of trying i found a solution with excuteable code ect

My flow

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/b/b/bb8ffa0c3cee69bb5e2a7d6863df5f0a03f8cfb2.png)

First (voer // get the current time date and time)

```auto
// Get the current date and time
var currentDate = new Date();
var currentTimeInSeconds = Math.round((currentDate.getHours() * 3600) + currentDate.getMinutes() * 60) + (currentDate.getSeconds());

// Set the result in a Homey variable of type number
setTagValue("Tijd Nu", {type: "number", title: "Tijd Nu"}, currentTimeInSeconds);

// Log the result (optional)
console.log("Current Time in Seconds: " + currentTimeInSeconds);

```

Second (Voer // Define the time in hours and minutes)

```auto
// Define the time in hours and minutes
var weekhours = 22;
var weekminutes = 30;
var weekendhours = 00;
var weekendminutes = 30;

// Calculate the total seconds
var totalSecondsweek = (weekhours * 3600) + (weekminutes * 60);
var totalSecondsweekend = (weekendhours * 3600) + (weekendminutes * 60);

setTagValue("Tijd Week", {type: "number", title: "Tijd Week"}, totalSecondsweek);
setTagValue("Tijd Weekend", {type: "number", title: "Tijd Weekend"}, totalSecondsweekend);

// Log the result (optional)
console.log("22:30 in seconds: " + totalSecondsweek);
console.log("00:30 in seconds: " + totalSecondsweekend);

```

ALERT this is the normal time (no winter time for dutch)  
if you need this you have to add 3600 sec somewhere you want

i hope someone can use it good luck all

result  
 ![dashboard](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/f/7/f7e78057239f24fe3135c3f1d82b3b92eec5dd62.gif)

---

<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:** [November 29, 2023, 9:50pm UTC](https://community.homey.app/t/time-calculation-from-sun-down-time-to-end-time-to-show-seconds-countdown-for-turn-off-lights-and-show-time-on-dashboard/96134/4 "2023-11-29T21:50:39Z")

</div>

> [@Kevin\_Ponsen](#):
>
> ```auto
> // Set the result in a Homey variable of type number
> setTagValue("Tijd Nu", {type: "number", title: "Tijd Nu"}, currentTimeInSeconds);
> 
> ```

One hint, writing/updating homeyscript variables is simplified a lot:

```auto
await tag('My String', 'abcdef');
await tag('My Number', 123);
await tag('My Boolean', true);

```

See the ‘example-tag.js’ script

---

<div class="post-metadata">

**Author:** ![Kevin\_Ponsen](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/kevin_ponsen/32/92414_2.png) [@Kevin\_Ponsen](https://community.homey.app/u/Kevin_Ponsen)\
**Post date:** [December 4, 2023, 5:24pm UTC](https://community.homey.app/t/time-calculation-from-sun-down-time-to-end-time-to-show-seconds-countdown-for-turn-off-lights-and-show-time-on-dashboard/96134/5 "2023-12-04T17:24:58Z")

</div>

thanks i am no programmer so im glad it works so i dont go editing it before i f\*\*\* it up 😃

if it work it works i dont touch it anymore 😜

---

<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:** [December 4, 2023, 6:41pm UTC](https://community.homey.app/t/time-calculation-from-sun-down-time-to-end-time-to-show-seconds-countdown-for-turn-off-lights-and-show-time-on-dashboard/96134/6 "2023-12-04T18:41:44Z")

</div>

That’s a good practice!
