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

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

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

Better Logic Library can help here

1 Like

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

My flow

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

// 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)

// 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

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

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

See the ‘example-tag.js’ script

1 Like

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

if it work it works i dont touch it anymore :stuck_out_tongue_winking_eye:

1 Like

That’s a good practice!

1 Like