Our national Weather Service has a rather complicated way of fetching info.
FIrst you access the API with a URL constructed of several parameters (type of data you want, stationID and API key):
https://opendata.aemet.es/opendata/api/ ... key=XXXXXX
That results in a JSON response like:
Code: Select all
{
descripcion: "éxito",
estado: 200,
datos: "https://opendata.aemet.es/opendata/sh/ff3de32d",
metadatos: "https://opendata.aemet.es/opendata/sh/8ca2e7e3",
}I have tried several approaches to fetch the data I want but so far all fail..
One method I used was running 2 separate scripts: 1 to fetch the "Datos" field and write it to a variable (both dzvents persisten and domoticz user variable) and then 2 minutes later a second one that calls the generated URL. With that one I always get nil data or invalid JSON result. When the URL has been fetched and I open it by copy/past from the variable I get a valid JSON with the data available.
Second method I have been trying is combining the URLs in one script (like below) but that does not give the results either.
It does get the "datos" field, which I know by both printing it to the log and writing it to the variable.
But I do not get it to trigger the part where the final data are fetched.
There are no errors shown, it just does not trigger.
Hope the many eyes here spot the (probably) silly mistake I am making.
The current script I have is
Code: Select all
return {
on = {
timer = { 'at *:26', 'at *:52' },
httpResponses = { 'aemeturl', 'aemetInfo' } -- matches callback string below
},
execute = function(domoticz, triggerItem)
if (triggerItem.isTimer) then
domoticz.openURL({
url = 'https://opendata.aemet.es/opendata/api/prediccion/especifica/playa/0301408/?api_key=MYKEY',
method = 'GET',
callback = 'aemeturl'
})
elseif (triggerItem.isHTTPResponse) then
local response = triggerItem
if (response.ok and response.isJSON and response.trigger == 'aemeturl') then
local Aemetlink = tostring(response.json.datos)
--processing
print('Aemetlink = '..Aemetlink)
domoticz.variables('Aemetlink').set(Aemetlink)
domoticz.openURL({
url = Aemetlink,
method = 'GET',
callback = 'aemetInfo'
})
--end processing
end
if (response.ok and response.isJSON and response.trigger == 'aemetInfo') then
local city = tostring(response.json.nombre) --location
print('City = '..city)
local tagua = domoticz.round((tonumber(response.json.prediccion.dia[1].tagua.valor1)),1) --visibility
print('Agua = '..tagua)
local wave = tostring(response.json.prediccion.dia[1].oleaje.descripcion2)
print('Oleaje = '..wave)
end
end
end
}