———————————————————————————————————-
— Script : script_time_sunset_sunrise.lua

— Objectif : Update virtual devices (Text) with sunrise and sunset values
—            Permet d’afficher l’heure du levé et du couché du soleil

— Auteur : Raphael
— Date : 19/03/2017
— Version : 1.0
———————————————————————————————————-
— Script parameters
———————————————————————————————————-
Debug = « NO »                    — Turn debugging on (« YES ») or off (« NO »)
                                        — YES to force Sunrise
———————————————————————————————————-
— Script functions
———————————————————————————————————-
local function WhichSeason() — Calcul de la saison
     local tNow = os.date(« *t »)
     local dayofyear = tNow.yday
     local season
     if (dayofyear >= 79) and (dayofyear < 172) then season = « Printemps »
     elseif (dayofyear >= 172) and (dayofyear < 266) then season = « Ete »
     elseif (dayofyear >= 266) and (dayofyear < 355) then season = « Automne »
     else season = « Hiver »
     end
     return season
end
local function IsWeekend()  — [0-6 = Sunday-Saturday]
     local dayNow = tonumber(os.date(« %w »))
     local weekend
     if (dayNow == 0) or (dayNow == 6) then weekend = « True » else weekend = « False » end
     return weekend
end
———————————————————————————————————-
— IDX of virtual devices
local sunrise_idx = ‘5’ — Heure du lever du soleil
local sunset_idx = ‘6’  — Heure du coucher du soleil
———————————————————————————————————-
— CommandArray
———————————————————————————————————-
commandArray = {}
    time = os.date(« *t »)
    — Trigger at 00:05 everyday
    if (time.hour == 0 and time.min == 5) or Debug== »YES » then
        local val1 = timeofday[‘SunriseInMinutes’]
        local sunrise_txt = string.format(‘%02d:%02d’, math.floor(val1 / 60), val1 % 60)
        commandArray[1] = { [‘UpdateDevice’] = sunrise_idx .. ‘|0|’ .. sunrise_txt }
        local val2 = timeofday[‘SunsetInMinutes’]
        local sunset_txt = string.format(‘%02d:%02d’, math.floor(val2 / 60), val2 % 60)
        commandArray[2] = { [‘UpdateDevice’] = sunset_idx .. ‘|0|’ .. sunset_txt }

        if Debug== »YES » then  — Affichage des infos si Mode Debug = YES
            local season  = WhichSeason();
            print (‘saison ‘..season)
            print(‘Leve du soleil: ‘..sunrise_txt..’ / Couche du soleil: ‘..sunset_txt)
            if IsWeekend== »True » then print « Samedi Dimanche » else print « Lundi Mardi Mercredi Jeudi Vendredi  » end
        end
    end
return commandArray