Code: Select all
-- collect begin- and endtimes of some astro events. dzVents >= 2.4.19 (domoticz V4.10717)
local scriptVar = 'astro dzVents'
return {
on = {
timer = { 'at 00:03', 'at 06:03' }, -- twice a day to be sure and 3 min past the hour because at the hour already quite busy
httpResponses = { scriptVar }
},
logging = {
level = domoticz.LOG_ERROR,
marker = scriptVar
},
execute = function(dz, item)
-- domoticz text devices. Comment lines for devices you don't have / want
local devKeys = { -- Device names -- Key in data
['Civil Twilight End'] = 'civil_twilight_end',
['Civil Twilight Start'] = 'civil_twilight_begin',
['Nautical Twilight End'] = 'nautical_twilight_begin',
['Nautical Twilight Start'] = 'nautical_twilight_end',
['Astronomical Twilight End'] = 'astronomical_twilight_begin',
['Astronomical Twilight Start'] = 'astronomical_twilight_end',
['Sunset'] = 'sunset',
['Sunrise'] = 'sunrise',
}
local function TZOffset() -- in seconds
return os.difftime(os.time(), os.time(os.date('!*t'))) + (dz.time.isdst and 3600 or 0)
end
local function makeTime(str)
local pattern = '(%d+)-(%d+)-(%d+)T(%d+):(%d+):(%d+)'
local runyear, runmonth, runday, runhour, runminute, runseconds = str:match(pattern)
local timestamp = os.time({year = runyear, month = runmonth, day = runday, hour = runhour, min = runminute, sec = runseconds})
return (os.date('%X',timestamp + TZOffset())):sub(1,5)
end
local function getAstroData()
url = 'https://api.sunrise-sunset.org/json' ..
'?lat=' .. dz.settings.location.latitude ..
'&lng=' .. dz.settings.location.longitude ..
'&formatted=0'
dz.openURL({ url = url, callback = scriptVar })
end
local function procesResults(t)
for dv, field in pairs(devKeys) do
if dz.devices(dv) and t[field] then
dz.devices(dv).updateText(makeTime(t[field]))
end
end
end
if item.isHTTPResponse then
if item.ok then
procesResults(item.json.results)
else
dz.log('problem retrieving information from sunrise-sunset.org \n' .. item.data, dz.LOG_ERROR)
end
else
getAstroData()
end
end
}