This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

API esios ree
#19
Hello.
Something similar I thought it would be a memory limitation.
You're right thanks for reminding me.
In the end, doing tests, I managed to fine-tune the script, with the update to PVPC 2.0TD.
Asking ESISO about the problem of updating the data at the time they indicated that the request must contain the date, otherwise the data of the current day is shown. So I was wrong that the date is not necessary.
This is the final script.

Code:
-- Incluir las librerías necesarias
https = require('ssl.https')
json = require('json')
ltn12 = require('ltn12')

payload =  ''

token ='token ESIOS'
Token=('Token token=\34'..token..'\34')

-- Obtención de la fecha para la petición
mañana_sin_formato = os.time(os.date('*t')) + 86400     --Hoy más 86400 segundos (+1 día)
mañana_con_formato = os.date("*t", mañana_sin_formato)
dia = mañana_con_formato.day
mes = mañana_con_formato.month
año = mañana_con_formato.year


-- Url donde está el PVPC 2.0TD con su correspondiente indicador 1001 y filtro para solo datos peninsulares
url = 'https://api.esios.ree.es/indicators/1001?start_date='..dia..'-'..mes..'-'..año..'T00%3A00&end_date='..dia..'-'..mes..'-'..año..'T23%3A50&geo_ids[]=8741'

-- Petición
response_body = {}

body, code, hdrs, stat = https.request ({ --Llamada HTTP formada por URL+metodo+cabecera
url = url;
  method = 'GET';
headers = {
        ['Accept'] = 'application/json; application/vnd.esios-api-v1+json';
        ['Content-Type'] = 'application/json';
        ['Host'] = 'api.esios.ree.es';
        ['Authorization'] = Token;
        ['Cookie'] = '';
};
    source = ltn12.source.string(payload);
    sink = ltn12.sink.table(response_body);
})

-- Comprobación que la recepción en correcta
if type(response_body) ~= 'table' then                                                            -- Si el contenido de la variable es distinto a una tabla entonces
  alert('Fallo al cargar la información')                                                        
elseif code == 200 then                                                                            -- Si la llamada devuelve el codigo 200, todo OK
  log(code .. ' Datos obtenidos con éxito')
elseif code == 401 then                                                                            -- Si la llamada devuelve el codigo de error 401 o 404                                                                                                                    
  alert(code .. ' Fallo con la llave de la API')                                                -- Mensajes de error
elseif code == 404 then
  alert(code .. ' Fallo datos no encontrados')
  return
end

-- Tratamiento de los datos recibidos uniéndolos en una sola cadena y decodificando el Json
response = table.concat(response_body)

data = json.pdecode(response)
log(data)
--Hora de actualización de los datos
año, mes, dia, hora, minutos, segundos = string.match(data.indicator.values_updated_at, '(%d+)-(%d+)-(%d+)T(%d+):(%d+):(%d+)')
fecha_hora_ordenada = string.format('%s-%s-%s, %s:%s:%s', dia, mes, año, hora, minutos, segundos)
grp.write('32/2/21', fecha_hora_ordenada)

--Utilizamos los datos de la PVCP obtenidos, se divide entre 1000 para obtener kWh--
--Hora 00
grp.write('32/2/22', data.indicator.values[1].value/1000)


--Hora 01
grp.write('32/2/23', data.indicator.values[2].value/1000)
Edit.
If a Z is added to the end of each date / time group, the request is made in UTC.

Greetings
Reply


Messages In This Thread
API esios ree - by JoseJimenez94 - 02.03.2020, 10:00
RE: API esios ree - by admin - 02.03.2020, 10:03
RE: API esios ree - by JoseJimenez94 - 02.03.2020, 11:06
RE: API esios ree - by admin - 02.03.2020, 11:13
RE: API esios ree - by admin - 02.03.2020, 13:54
RE: API esios ree - by JoseJimenez94 - 02.03.2020, 15:47
RE: API esios ree - by esteve.budia - 15.05.2020, 12:45
RE: API esios ree - by JRP - 02.09.2020, 17:24
RE: API esios ree - by admin - 03.09.2020, 06:29
RE: API esios ree - by JRP - 03.09.2020, 08:08
RE: API esios ree - by admin - 03.09.2020, 08:25
RE: API esios ree - by JRP - 03.09.2020, 08:34
RE: API esios ree - by admin - 03.09.2020, 08:36
RE: API esios ree - by JRP - 03.09.2020, 08:40
RE: API esios ree - by JRP - 10.09.2020, 10:44
RE: API esios ree - by JRP - 16.06.2021, 16:52
RE: API esios ree - by JRP - 16.06.2021, 19:01
RE: API esios ree - by admin - 18.06.2021, 05:27
RE: API esios ree - by JRP - 21.06.2021, 14:50
RE: API esios ree - by JRP - 23.06.2021, 09:20
RE: API esios ree - by admin - 23.06.2021, 09:21
RE: API esios ree - by JRP - 23.06.2021, 10:59
RE: API esios ree - by jose_dli - 16.02.2023, 08:01
RE: API esios ree - by admin - 16.02.2023, 12:54
RE: API esios ree - by jose_dli - 16.02.2023, 14:33
RE: API esios ree - by admin - 16.02.2023, 14:37
RE: API esios ree - by admin - 17.02.2023, 09:25
RE: API esios ree - by jose_dli - 17.02.2023, 11:57
RE: API esios ree - by jose_dli - 21.02.2023, 13:10

Forum Jump: