Logic Machine Forum
Day/Night astro object - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Day/Night astro object (/showthread.php?tid=2754)

Day/Night astro object - alexll - 28.07.2020

Hi there,

I'm looking for an object to know if it's day or night based on the sunrise / sunset calculation of the LM to use in some scripts (or just the best way to do that).

Can you help me? Thanks in advance.

RE: Day/Night astro object - AlexLV - 28.07.2020

Hi alexll,

Now it possible to do easier than in mine variant. You need enter your site where device is located your coordinates, (Utilities --> Time and Date). Than sunrise and sunset times will be automatically calculated and available in scheduler. And in scheduler you can use as example sunrise time and add offset (+ or - time to switch on/off something)..

You can try to use part of my script Resident script, sleep 60 seconds):

time = os.time()
latitude = 52.879110 --Site Coordinates
longitude = 23.128610 --Site Coordinates
sunrise, sunset = rscalc(latitude, longitude)

sunrise_hour = math.floor(sunrise / 60)
sunrise_minute = sunrise % 60

sunset_hour = math.floor(sunset / 60)
sunset_minute = sunset % 60

sunrise_text = string.format('%02d:%02d', sunrise_hour, sunrise_minute)
sunset_text = string.format('%02d:%02d', sunset_hour, sunset_minute)
Day_Length = sunset - sunrise
Day_Length_hour = math.floor(Day_Length / 60)
Day_Length_minute = Day_Length % 60
Day_Length_text = string.format('%02d:%02d', Day_Length_hour, Day_Length_minute)
grp.update('33/1/20', Day_Length_text) -- Length of the day in text way. Put in your group
--log (Day_Length_hour)
--log (Day_Length_minute)
date = os.date('*t')
now = date.hour * 60 + date.min
OutsideLightOn = sunset + 60 -- Time after one hour after sunset. You can change it...
OutsideLightOff = sunrise - 60 --Time one hour before sunrise. You can change it..
if now > OutsideLightOn then
  OutsideLightTxt = 'Outside Light is On' -- text status of outside lighting
  grp.update('33/1/11', 1) -- Switch On group of outside lighting
  grp.update('33/1/12', OutsideLightTxt) --Lighting status text way
if (now > (OutsideLightOff - 2) and now < OutsideLightOff) then
  OutsideLightTxt = 'Outside Light switched Off'
grp.update('33/1/11', 0) -- Switch off outside lighting
  grp.update('33/1/12', OutsideLightTxt) --Text wat status of lighting
-- from 0:00 to sunrise
if now < sunrise then
  daynight = 'Before sunrise (Night)'
-- from sunset to 23:59
elseif now > sunset then
  daynight = 'After sunset (Time till midnight)'
  daynight = 'Day'
Also you will have statuss - of the day time - Night, Day, ot time till midnight after sunset..



RE: Day/Night astro object - alexll - 29.07.2020

Thanks for the quick reply. I'm going to try that.