Domoticz is reporting a 503 error, api.open.meteo is not accessible, but 10000 hits are available and Ive had some sussesvol sessions, but few
Anyone have the same problem?
Code: Select all
return {
active = true,
on = {
timer = { "every 30 minutes" },
httpResponses = { 'openmeteo_cloud_api' }
},
execute = function(domoticz, item)
domoticz.log(
"Cloud trigger: timer="..tostring(item.isTimer)..
" http="..tostring(item.isHTTPResponse),
domoticz.LOG_STATUS)
-------------------------------------------------
-- CONFIG
-------------------------------------------------
local var = domoticz.variables("CloudForecast_LastUpdate")
local lastUpdate = var and tonumber(var.value) or 0
local now = os.time()
local retryVar = domoticz.variables("CloudForecast_LastAttempt")
local lastAttempt = retryVar and tonumber(retryVar.value) or 0
domoticz.log("Time Now : "..now.. " Time LastAttempt : " .. lastAttempt .. " Time- LastAttempt : " .. now-lastAttempt, domoticz.LOG_STATUS)
if (now - lastAttempt) < 1800 then
domoticz.log("Cloud API retry delay active", domoticz.LOG_DEBUG)
return
end
-------------------------------------------------
-- STEP 1: THROTTLED REQUEST
-------------------------------------------------
if item.isTimer then
-- ⛔ SKIP IF DATA IS STILL FRESH (15 min cache)
if (now - lastUpdate) < 900 then
domoticz.log("Cloud API skipped (cached)", domoticz.LOG_DEBUG)
return
end
if retryVar then
retryVar.set(now)
end
local URL = "https://api.open-meteo.com/v1/forecast?latitude=51.46&longitude=5.45&hourly=shortwave_radiation,cloud_cover&timezone=auto&tilt=55"
domoticz.openURL({
url = URL,
method = 'GET',
timeout = 30,
callback = 'openmeteo_cloud_api',
})
domoticz.log("Cloud API request sent", domoticz.LOG_STATUS)
return
end
-------------------------------------------------
-- STEP 2: RESPONSE HANDLING
-------------------------------------------------
if not item.isHTTPResponse then return end
domoticz.log("HTTP status: " .. tostring(item.statusCode), domoticz.LOG_STATUS)
if item.statusCode == 503 then
-- domoticz.variables("CloudForecast_LastAttempt").set(os.time())
domoticz.log(
"Open-Meteo overloaded. Keeping previous forecast.",
domoticz.LOG_STATUS
)
return
end
if item.statusCode ~= 200 then
domoticz.log(
"Weather download failed: "..tostring(item.statusCode),
domoticz.LOG_ERROR
)
domoticz.log(tostring(item.data), domoticz.LOG_ERROR)
return
end
if item.headers then
for k,v in pairs(item.headers) do
domoticz.log(k.." = "..tostring(v), domoticz.LOG_ERROR)
end
end
-------------------------------------------------
-- HARD VALIDATION
-------------------------------------------------
local body = tostring(item.data or ""):match("^%s*(.*)")
if body == "" or body:sub(1,1) ~= "{" then
domoticz.log("Invalid response: " .. body, domoticz.LOG_ERROR)
return
end
-------------------------------------------------
-- SAFE PARSE
-------------------------------------------------
local ok, data = pcall(domoticz.utils.fromJSON, item.data)
if not ok then
domoticz.log("JSON parse failed", domoticz.LOG_ERROR)
return
end
domoticz.log("JSON parsed successfully", domoticz.LOG_STATUS)
domoticz.log(item.data, domoticz.LOG_STATUS)
-------------------------------------------------
-- FIELD CHECK
-------------------------------------------------
if not data.hourly or not data.hourly.time or not data.hourly.cloud_cover or not data.hourly.shortwave_radiation then
domoticz.log("Cloud API missing required fields", domoticz.LOG_ERROR)
--domoticz.log(item.data, domoticz.LOG_ERROR)
return
end
-------------------------------------------------
-- BUILD CLEAN FORECAST
-------------------------------------------------
local out = {}
local max = math.min(24, #data.hourly.time)
for i = 1, max do
local t = data.hourly.time[i]
local c = data.hourly["cloud_cover"][i]
local r = data.hourly["shortwave_radiation"][i]
if t and c ~= nil then
out[#out+1] = {
t = t,
c = tonumber(c) or 50,
rad = tonumber(r) or 0
}
end
end
if #out == 0 then
domoticz.log("Cloud API empty result", domoticz.LOG_ERROR)
return
end
-------------------------------------------------
-- SAVE FORECAST
-------------------------------------------------
if var then
var.set(os.time())
else
domoticz.log("Missing CloudForecast_LastUpdate variable", domoticz.LOG_ERROR)
end
local store = domoticz.variables("CloudForecast")
if store then
store.set(domoticz.utils.toJSON(out))
end
-------------------------------------------------
-- LOG
-------------------------------------------------
domoticz.log(string.format(
"Cloud API updated OK (%d entries)",
#out
), domoticz.LOG_STATUS)
end
}