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 whether you accept or reject these cookies being set.

API esios ree
#1
Hi everyone,

I have been trying to do a scheduled script to obtain data from the PVPC Active energy billing term default tolling (https://www.esios.ree.es/es/analisis/102...oupby=hour) every day.


Using the API provided by the web to download this information (https://api.esios.ree.es/) , I tried the next script:

Code:
https = require('ssl.https')
json = require('json')
ltn12 = require('ltn12')


payload =  ''

token ='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' 
Token=('Token token=\34'..token..'\34')
-- pedirlo automaticamente aqui: consultasios@ree.es


now = os.date('*t')

tom= os.time(os.date('*t')) + 86400 --mañana
--tom= os.time(os.date('*t')) --hoy

tomorrow = os.date("*t", tom)

day = tomorrow.day
month = tomorrow.month
year = tomorrow.year

--log (now, tom, tomorrow)

url = 'https://api.esios.ree.es/indicators/10229?start_date='..day..'-'..month..'-'..year..'T00%3A00&end_date='..day..'-'..month..'-'..year..'T23%3A50' --Término de facturación de energía activa del PVPC
log (url)

--  url = "https://api.esios.ree.es/indicators/10229?start_date=02-03-2020T00%3A00&end_date=02-03-2020T23%3A50" --Término de facturación de energía activa del PVPC


response_body = {}
 
body, code, hdrs, stat = https.request ({
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);
})

log ( code, response_body)

if (code == 200) then
  log('OK: '..tostring (code) .. ' Success on GET....' )
end

But i receive this log: 
* arg: 1
  * string: error:1409442E:lib(20):func(148):reason(1070)
* arg: 2
  * table:

I don't know what i'm doing wrong. I will appreciate any help.
Reply
#2
Install 2019 RC1 firmware. This error happens due to outdated socket/ssl libraries.
Reply
#3
(02.03.2020, 10:03)admin Wrote: Install 2019 RC1 firmware. This error happens due to outdated socket/ssl libraries.
I have installed the last firmware Sad
Reply
#4
Send me your full script including valid token via PM
Reply
#5
Your script works on 20191015 but produces an error on 20180828.

If you don't want to upgrade firmware then you can set protocol version to tlsv1_2 like this:
Code:
source = ltn12.source.string(payload);
  sink = ltn12.sink.table(response_body);
  protocol = 'tlsv1_2';
})
Reply
#6
(02.03.2020, 13:54)admin Wrote: Your script works on 20191015 but produces an error on 20180828.

If you don't want to upgrade firmware then you can set protocol version to tlsv1_2 like this:
Code:
 
  source = ltn12.source.string(payload);
  sink = ltn12.sink.table(response_body);
  protocol = 'tlsv1_2';
})
Thanks a lot Admin. Now it works Big Grin
Reply
#7
Original script was designed by me....

I did it for share of course
-- =======================================================================================
-- =======================================================================================
-- =======================================================================================
-- =======================================================================================
-- ====  ejemplo 20.3      Término de facturación de energía activa del PVPC    =========
-- =======================================================================================
-- =======================================================================================
-- =======================================================================================
-- ===============================SCHNEIDER=ELECTRIC=esteve=budia=2018====================
Reply
#8
(15.05.2020, 12:45)esteve.budia Wrote: Original script was designed by me....

I did it for share of course
-- =======================================================================================
-- =======================================================================================
-- =======================================================================================
-- =======================================================================================
-- ====  ejemplo 20.3      Término de facturación de energía activa del PVPC    =========
-- =======================================================================================
-- =======================================================================================
-- =======================================================================================
-- ===============================SCHNEIDER=ELECTRIC=esteve=budia=2018====================
Hi Esteve

There is one thing that I do not understand about your script, it is assumed that what is downloaded from ESIOS is a json, so it should be converted to a lua table with json.decode (value), where value would be response_body, but you have it from another shape.
In the log that is obtained, put:

Code:
Event for OFF TOTAL (0/0/1) 02.09.2020 19:22:10
* arg: 1
  * number: 200
* arg: 2
  * table:
   [1]
    * string: {"indicator":{"name":"Término de facturación de energía activa del PVPC peaje por defecto suma componentes","short_name":"PVPC T. Defecto","id":10229,"composited":true,"step_type":"linear","disaggregated":false,"magnitud":[{"name":"Precio","id":23}],"tiempo":[{"name":"Hora","id":4}],"geos":[{"geo_id":3,"geo_name":"España"}],"values_updated_at":"2020-09-01T20:25:03.000+02:00","values":[{"value":102.04,"datetime":"2020-09-02T00:00:00.000+02:00","datetime_utc":"2020-09-01T22:00:00Z","tz_time":"2020-09-01T22:00:00.000Z","geo_id":3,"geo_name":"España"},{"value":98.29,"datetime":"2020-09-02T01:00:00.000+02:00","datetime_utc":"2020-09-01T23:00:00Z","tz_time":"2020-09-01T23:00:00.000Z","geo_id":3,"geo_name":"España"},{"value":96.52,"datetime":"2020-09-02T02:00:00.000+02:00","datetime_utc":"2020-09-02T00:00:00Z","tz_time":"2020-09-02T00:00:00.000Z","geo_id":3,"geo_name":"España"},{"value":95.27,"datetime":"2020-09-02T03:00:00.000+02:00","datetime_utc":"2020-09-02T01:00:00Z","tz_time":"2020-09-02T01:00:00.000Z","geo_id":3,"geo_name":"España"},{"value":96.4,"datetime":"2020-09-02T04:00:00.000+02:00","datetime_utc":"2020-09-02T02:00:00Z","tz_time":"2020-09-02T02:00:00.000Z","geo_id":3,"geo_name":"España"},{"value":99.22,"datetime":"2020-09-02T05:00:00.000+02:00","datetime_utc":"2020-09-02T03:00:00Z","tz_time":"2020-09-02T03:00:00.000Z","geo_id":3,"geo_name":"España"},{"value":104.97,"datetime":"2020-09-02T06:00:00.000+02:00","datetime_utc":"2020-09-02T04:00:00Z","tz_time":"2020-09-02T04:00:00.000Z","geo_id":3,"geo_name":"España"},{"value":104.29,"datetime":"2020-09-02T07:00:00.000+02:00","datetime_utc":"2020-09-02T05:00:00Z","tz_time":"2020-09-02T05:00:00.000Z","geo_id":3,"geo_name":"España"},{"value":104.63,"datetime":"2020-09-02T08:00:00.000+02:00","datetime_utc":"2020-09-02T06:00:00Z","tz_time":"2020-09-02T06:00:00.000Z","geo_id":3,"geo_name":"España"},{"value":104.91,"datetime":"2020-09-02T09:00:00.000+02:00","datetime_utc":"2020-09-02T07:00:00Z","tz_time":"2020-09-02T07:00:00.000Z","geo_id":3,"geo_name":"España"},{"value":102.06,"datetime":"2020-09-02T10:00:00.000+02:00","datetime_utc":"2020-09-02T08:00:00Z","tz_time":"2020-09-02T08:00:00.000Z","geo_id":3,"geo_name":"España"},{"value":102.28,"datetime":"2020-09-02T11:00:00.000+02:00","datetime_utc":"2020-09-02T09:00:00Z","tz_time":"2020-09-02T09:00:00.000Z","geo_id":3,"geo_name":"España"},{"value":103.16,"datetime":"2020-09-02T12:00:00.000+02:00","datetime_utc":"2020-09-02T10:00:00Z","tz_time":"2020-09-02T10:00:00.000Z","geo_id":3,"geo_name":"España"},{"value":101.96,"datetime":"2020-09-02T13:00:00.000+02:00","datetime_utc":"2020-09-02T11:00:00Z","tz_time":"2020-09-02T11:00:00.000Z","geo_id":3,"geo_name":"España"},{"value":102.02,"datetime":"2020-09-02T14:00:00.000+02:00","datetime_utc":"2020-09-02T12:00:00Z","tz_time":"2020-09-02T12:00:00.000Z","geo_id":3,"geo_name":"España"},{"value":102.52,"datetime":"2020-09-02T15:00:00.000+02:00","datetime_utc":"2020-09-02T13:00:00Z","tz_time":"2020-09-02T13:00:00.000Z","geo_id":3,"geo_name":"España"},{"value":105.05,"datetime":"2020-09-02T16:00:00.000+02:00","datetime_utc":"2020-09-02T14:00:00Z","tz_time":"2020-09-02T14:00:00.000Z","geo_id":3,"geo_name":"España"},{"value":106.52,"datetime":"2020-09-02T17:00:00.000+02:00","datetime_utc":"2020-09-02T15:00:00Z","tz_time":"2020-09-02T15:00:00.000Z","geo_id":3,"geo_name":"España"},{"value":106.65,"datetime":"2020-09-02T18:00:00.000+02:00","datetime_utc":"2020-09-02T16:00:00Z","tz_time":"2020-09-02T16:00:00.000Z","geo_id":3,"geo_name":"España"},{"value":108.91,"datetime":"2020-09-02T19:00:00.000+02:00","datetime_utc":"2020-09-02T17:00:00Z","tz_time":"2020-09-02T17:00:00.000Z","geo_id":3,"geo_name":"España"},{"value":110.41,"datetime":"2020-09-02T20:00:00.000+02:00","datetime_utc":"2020-09-02T18:00:00Z","tz_time":"2020-09-02T18:00:00.000Z","geo_id":3,"geo_name":"España"},{"value":111.91,"datetime":"2020-09-02T21:00:00.000+02:00","datetime_utc":"2020-09-02T19:00:00Z","tz_time":"2020-09-02T19:00:00.000Z","geo_id":3,"geo_name":"España"},{"value":109.16,"datetime":"2020-09-02T22:00:00.000+02:00","datetime_utc":"2020-09-02T20:00:00Z","tz_time":"2020-09-02T20:00:00.000Z","geo_id":3,"geo_name":"España"},{"value":104.25,"datetime":"2020-09-02T23:00:00.000+02:00","datetime_utc":"2020-09-02T21:00:00Z","tz_time":"2020-09-02T21:00:00.000Z","geo_id":3,"geo_name":"España"}]}}

It is supposed to be a table, but it only has one field [1] and what it contains is a string with all the result? How do I keep for example the short name of PVPC to assign it, for example to a virtual object? Or prices, which is the important thing of course.
Should it be string manipulation functions? Some example?

Thank you
Reply
#9
You need to combine table items to a single string and the decode it from JSON to Lua table. This example will log the short name and the first value of values entry.
Code:
response = table.concat(response_body)
data = require('json').pdecode(response)

log(data.indicator.short_name)
log(data.indicator.values[ 1 ].value)
Reply
#10
(03.09.2020, 06:29)admin Wrote: You need to combine table items to a single string and the decode it from JSON to Lua table. This example will log the short name and the first value of values entry.
Code:
response = table.concat(response_body)
data = require('json').pdecode(response)

log(data.indicator.short_name)
log(data.indicator.values[ 1 ].value)
Hello
It works perfectly, thanks.

The meaning of this part is that you get a table with a single field, we transform it into a string (json) and decode it into a table. I thought that from Esios the json was obtained directly as it happens in other APIs and that is why I did not understand how to use the result.

By the way, why is it due to use this form data = require ('json'). pdecode (response) instead of json.decode (response) and why in safe mode?

Thank you very much for your time.
Reply
#11
decode will just stop the script if JSON is not valid. pdecode should be used if you want your script to continue in case of an error, this is sometimes more convenient if you want to log the error. The most correct approach is to check that the decoded data is a table and it contains all the required fields.

Code:
if type(data) == 'table' and type(data.indicator) == 'table' then
  log(data.indicator.short_name)
end
Reply
#12
(03.09.2020, 08:25)admin Wrote: decode will just stop the script if JSON is not valid. pdecode should be used if you want your script to continue in case of an error, this is sometimes more convenient if you want to log the error. The most correct approach is to check that the decoded data is a table and it contains all the required fields.

Code:
if type(data) == 'table' and type(data.indicator) == 'table' then
  log(data.indicator.short_name)
end
OK understood.

This is how it is done in the script that I am modifying for Openweather, I can do it here too.

One last question, if the value in example [1] which is 105.43 is wh and I want it in kWh, I divide it by 1000, like this grp.write ('32 / 2/21 ', data.indicator.values [ 1] .value / 1000), the problem is the DPT of the object, since I see that the largest for this case is 14. 4 bytes of decimal number. The problem is that it only gives me 3 of the 5 decimal places I need. How could I do it, pass it to a string for example?
Thank you very much for clarifying my doubts.
Reply
#13
You can set Decimal places to 5 in Visualization parameters for your object
Reply
#14
(03.09.2020, 08:36)admin Wrote: You can set Decimal places to 5 in Visualization parameters for your object
Thanks, perfect, fast and accurate.

a greeting
Reply
#15
Hello

This is my final script, but only with two values, the rest is obtained the same by modifying the index [].
Thank you very much for the help.

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

payload =  ''

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

-- Obtención de la fecha para la petición
tom = os.time(os.date('*t')) + 86400     --Hoy más 86400 segundos (+1 día)
tomorrow = os.date("*t", tom)
day = tomorrow.day
month = tomorrow.month
year = tomorrow.year


-- Url donde está el PVPC con su correspondiente indicador 10229
url = 'https://api.esios.ree.es/indicators/10229?start_date='..day..'-'..month..'-'..year..'T00%3A00&end_date='..day..'-'..month..'-'..year..'T23%3A50' --Término de facturación de energía activa del PVPC

-- Petición
response_body = {}
 
body, code, hdrs, stat = https.request ({
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 = require('json').decode(response)

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


--Hora 01
grp.write('32/2/22', data.indicator.values[ 2 ].value/1000)
Reply
#16
Hello
Due to the recent change in the electricity tariff in Spain, I have returned to this script to modify it.
Now the indicator is 1001 for the PVPC T 2.0TD.
I have removed the part that calculates the time as it is unnecessary.
I have simplified the url by removing the time range as it is not required.
The question is that now when making the records appear things that I do not understand.
1. The response_body log now contains two strings, the first one is cut into dat and the bottom one starts with etime and ends with three dots, with no more data.
Code:
table:
[1]
  * string: {"indicator":{"name":"Término de facturación de energía activa del PVPC 2.0TD","short_name":"PVPC T. 2.0TD","id":1001,"composited":false,"step_type":"linear","disaggregated":true,"magnitud":[{"name":"Precio","id":23}],"tiempo":[{"name":"Hora","id":4}],"geos":[{"geo_id":8741,"geo_name":"Península"},{"geo_id":8742,"geo_name":"Canarias"},{"geo_id":8743,"geo_name":"Baleares"},{"geo_id":8744,"geo_name":"Ceuta"},{"geo_id":8745,"geo_name":"Melilla"}],"values_updated_at":"2021-06-15T20:16:44.000+02:00","values":[{"value":125.65,"datetime":"2021-06-16T00:00:00.000+02:00","datetime_utc":"2021-06-15T22:00:00Z","tz_time":"2021-06-15T22:00:00.000Z","geo_id":8741,"geo_name":"Península"},{"value":125.65,"datetime":"2021-06-16T00:00:00.000+02:00","datetime_utc":"2021-06-15T22:00:00Z","tz_time":"2021-06-15T22:00:00.000Z","geo_id":8742,"geo_name":"Canarias"},{"value":125.65,"datetime":"2021-06-16T00:00:00.000+02:00","datetime_utc":"2021-06-15T22:00:00Z","tz_time":"2021-06-15T22:00:00.000Z","geo_id":8743,"geo_name":"Baleares"},{"value":125.65,"datetime":"2021-06-16T00:00:00.000+02:00","datetime_utc":"2021-06-15T22:00:00Z","tz_time":"2021-06-15T22:00:00.000Z","geo_id":8744,"geo_name":"Ceuta"},{"value":125.65,"datetime":"2021-06-16T00:00:00.000+02:00","datetime_utc":"2021-06-15T22:00:00Z","tz_time":"2021-06-15T22:00:00.000Z","geo_id":8745,"geo_name":"Melilla"},{"value":123.54,"datetime":"2021-06-16T01:00:00.000+02:00","datetime_utc":"2021-06-15T23:00:00Z","tz_time":"2021-06-15T23:00:00.000Z","geo_id":8741,"geo_name":"Península"},{"value":123.54,"datetime":"2021-06-16T01:00:00.000+02:00","datetime_utc":"2021-06-15T23:00:00Z","tz_time":"2021-06-15T23:00:00.000Z","geo_id":8742,"geo_name":"Canarias"},{"value":123.54,"datetime":"2021-06-16T01:00:00.000+02:00","datetime_utc":"2021-06-15T23:00:00Z","tz_time":"2021-06-15T23:00:00.000Z","geo_id":8743,"geo_name":"Baleares"},{"value":123.54,"datetime":"2021-06-16T01:00:00.000+02:00","datetime_utc":"2021-06-15T23:00:00Z","tz_time":"2021-06-15T23:00:00.000Z","geo_id":8744,"geo_name":"Ceuta"},{"value":123.54,"datetime":"2021-06-16T01:00:00.000+02:00","datetime_utc":"2021-06-15T23:00:00Z","tz_time":"2021-06-15T23:00:00.000Z","geo_id":8745,"geo_name":"Melilla"},{"value":122.17,"datetime":"2021-06-16T02:00:00.000+02:00","datetime_utc":"2021-06-16T00:00:00Z","tz_time":"2021-06-16T00:00:00.000Z","geo_id":8741,"geo_name":"Península"},{"value":122.17,"datetime":"2021-06-16T02:00:00.000+02:00","datetime_utc":"2021-06-16T00:00:00Z","tz_time":"2021-06-16T00:00:00.000Z","geo_id":8742,"geo_name":"Canarias"},{"value":122.17,"datetime":"2021-06-16T02:00:00.000+02:00","datetime_utc":"2021-06-16T00:00:00Z","tz_time":"2021-06-16T00:00:00.000Z","geo_id":8743,"geo_name":"Baleares"},{"value":122.17,"datetime":"2021-06-16T02:00:00.000+02:00","datetime_utc":"2021-06-16T00:00:00Z","tz_time":"2021-06-16T00:00:00.000Z","geo_id":8744,"geo_name":"Ceuta"},{"value":122.17,"datetime":"2021-06-16T02:00:00.000+02:00","datetime_utc":"2021-06-16T00:00:00Z","tz_time":"2021-06-16T00:00:00.000Z","geo_id":8745,"geo_name":"Melilla"},{"value":122.53,"datetime":"2021-06-16T03:00:00.000+02:00","datetime_utc":"2021-06-16T01:00:00Z","tz_time":"2021-06-16T01:00:00.000Z","geo_id":8741,"geo_name":"Península"},{"value":122.53,"datetime":"2021-06-16T03:00:00.000+02:00","datetime_utc":"2021-06-16T01:00:00Z","tz_time":"2021-06-16T01:00:00.000Z","geo_id":8742,"geo_name":"Canarias"},{"value":122.53,"datetime":"2021-06-16T03:00:00.000+02:00","datetime_utc":"2021-06-16T01:00:00Z","tz_time":"2021-06-16T01:00:00.000Z","geo_id":8743,"geo_name":"Baleares"},{"value":122.53,"datetime":"2021-06-16T03:00:00.000+02:00","datetime_utc":"2021-06-16T01:00:00Z","tz_time":"2021-06-16T01:00:00.000Z","geo_id":8744,"geo_name":"Ceuta"},{"value":122.53,"datetime":"2021-06-16T03:00:00.000+02:00","datetime_utc":"2021-06-16T01:00:00Z","tz_time":"2021-06-16T01:00:00.000Z","geo_id":8745,"geo_name":"Melilla"},{"value":122.7,"datetime":"2021-06-16T04:00:00.000+02:00","datetime_utc":"2021-06-16T02:00:00Z","tz_time":"2021-06-16T02:00:00.000Z","geo_id":8741,"geo_name":"Península"},{"value":122.7,"datetime":"2021-06-16T04:00:00.000+02:00","datetime_utc":"2021-06-16T02:00:00Z","tz_time":"2021-06-16T02:00:00.000Z","geo_id":8742,"geo_name":"Canarias"},{"value":122.7,"datetime":"2021-06-16T04:00:00.000+02:00","datetime_utc":"2021-06-16T02:00:00Z","tz_time":"2021-06-16T02:00:00.000Z","geo_id":8743,"geo_name":"Baleares"},{"value":122.7,"datetime":"2021-06-16T04:00:00.000+02:00","datetime_utc":"2021-06-16T02:00:00Z","tz_time":"2021-06-16T02:00:00.000Z","geo_id":8744,"geo_name":"Ceuta"},{"value":122.7,"datetime":"2021-06-16T04:00:00.000+02:00","datetime_utc":"2021-06-16T02:00:00Z","tz_time":"2021-06-16T02:00:00.000Z","geo_id":8745,"geo_name":"Melilla"},{"value":121.72,"datetime":"2021-06-16T05:00:00.000+02:00","datetime_utc":"2021-06-16T03:00:00Z","tz_time":"2021-06-16T03:00:00.000Z","geo_id":8741,"geo_name":"Península"},{"value":121.72,"datetime":"2021-06-16T05:00:00.000+02:00","datetime_utc":"2021-06-16T03:00:00Z","tz_time":"2021-06-16T03:00:00.000Z","geo_id":8742,"geo_name":"Canarias"},{"value":121.72,"datetime":"2021-06-16T05:00:00.000+02:00","datetime_utc":"2021-06-16T03:00:00Z","tz_time":"2021-06-16T03:00:00.000Z","geo_id":8743,"geo_name":"Baleares"},{"value":121.72,"datetime":"2021-06-16T05:00:00.000+02:00","datetime_utc":"2021-06-16T03:00:00Z","tz_time":"2021-06-16T03:00:00.000Z","geo_id":8744,"geo_name":"Ceuta"},{"value":121.72,"datetime":"2021-06-16T05:00:00.000+02:00","datetime_utc":"2021-06-16T03:00:00Z","tz_time":"2021-06-16T03:00:00.000Z","geo_id":8745,"geo_name":"Melilla"},{"value":122.72,"datetime":"2021-06-16T06:00:00.000+02:00","datetime_utc":"2021-06-16T04:00:00Z","tz_time":"2021-06-16T04:00:00.000Z","geo_id":8741,"geo_name":"Península"},{"value":122.72,"datetime":"2021-06-16T06:00:00.000+02:00","datetime_utc":"2021-06-16T04:00:00Z","tz_time":"2021-06-16T04:00:00.000Z","geo_id":8742,"geo_name":"Canarias"},{"value":122.72,"datetime":"2021-06-16T06:00:00.000+02:00","datetime_utc":"2021-06-16T04:00:00Z","tz_time":"2021-06-16T04:00:00.000Z","geo_id":8743,"geo_name":"Baleares"},{"value":122.72,"datetime":"2021-06-16T06:00:00.000+02:00","datetime_utc":"2021-06-16T04:00:00Z","tz_time":"2021-06-16T04:00:00.000Z","geo_id":8744,"geo_name":"Ceuta"},{"value":122.72,"datetime":"2021-06-16T06:00:00.000+02:00","datetime_utc":"2021-06-16T04:00:00Z","tz_time":"2021-06-16T04:00:00.000Z","geo_id":8745,"geo_name":"Melilla"},{"value":125.12,"datetime":"2021-06-16T07:00:00.000+02:00","datetime_utc":"2021-06-16T05:00:00Z","tz_time":"2021-06-16T05:00:00.000Z","geo_id":8741,"geo_name":"Península"},{"value":125.12,"datetime":"2021-06-16T07:00:00.000+02:00","datetime_utc":"2021-06-16T05:00:00Z","tz_time":"2021-06-16T05:00:00.000Z","geo_id":8742,"geo_name":"Canarias"},{"value":125.12,"datetime":"2021-06-16T07:00:00.000+02:00","datetime_utc":"2021-06-16T05:00:00Z","tz_time":"2021-06-16T05:00:00.000Z","geo_id":8743,"geo_name":"Baleares"},{"value":125.12,"datetime":"2021-06-16T07:00:00.000+02:00","datetime_utc":"2021-06-16T05:00:00Z","tz_time":"2021-06-16T05:00:00.000Z","geo_id":8744,"geo_name":"Ceuta"},{"value":125.12,"datetime":"2021-06-16T07:00:00.000+02:00","datetime_utc":"2021-06-16T05:00:00Z","tz_time":"2021-06-16T05:00:00.000Z","geo_id":8745,"geo_name":"Melilla"},{"value":161.78,"datetime":"2021-06-16T08:00:00.000+02:00","datetime_utc":"2021-06-16T06:00:00Z","tz_time":"2021-06-16T06:00:00.000Z","geo_id":8741,"geo_name":"Península"},{"value":161.78,"datetime":"2021-06-16T08:00:00.000+02:00","datetime_utc":"2021-06-16T06:00:00Z","tz_time":"2021-06-16T06:00:00.000Z","geo_id":8742,"geo_name":"Canarias"},{"value":161.78,"datetime":"2021-06-16T08:00:00.000+02:00","datetime_utc":"2021-06-16T06:00:00Z","tz_time":"2021-06-16T06:00:00.000Z","geo_id":8743,"geo_name":"Baleares"},{"value":161.78,"dat
[2]
  * string: etime":"2021-06-16T08:00:00.000+02:00","datetime_utc":"2021-06-16T06:00:00Z","tz_time":"2021-06-16T06:00:00.000Z","geo_id":8744,"geo_name":"Ceuta"},{"value":161.78,"datetime":"2021-06-16T08:00:00.000+02:00","datetime_utc":"2021-06-16T06:00:00Z","tz_time":"2021-06-16T06:00:00.000Z...

2. In the data log, I do not see any data, such as the long name, the short one. It is also cut in field 23, showing other ...
Code:
PVPC 16.06.2021 18:34:59
* table:
["indicator"]
  * table:
   ["composited"]
    * bool: false
   ["magnitud"]
    * table:
     [1]
      * table:
       ["name"]
        * string: Precio
       ["id"]
        * number: 23
   ["id"]
    * number: 1001
   ["tiempo"]
    * table:
     [1]
      * table:
       ["name"]
        * string: Hora
       ["id"]
        * number: 4
   ["step_type"]
    * string: linear
   ["short_name"]
    * string: PVPC T. 2.0TD
   ["values"]
    * table:
     [1]
      * table:
       ["datetime_utc"]
        * string: 2021-06-15T22:00:00Z
       ["geo_id"]
        * number: 8741
       ["value"]
        * number: 125.65
       ["tz_time"]
        * string: 2021-06-15T22:00:00.000Z
       ["geo_name"]
        * string: Península
       ["datetime"]
        * string: 2021-06-16T00:00:00.000+02:00
     [2]
      * table:
       ["datetime_utc"]
        * string: 2021-06-15T22:00:00Z
       ["geo_id"]
        * number: 8742
       ["value"]
        * number: 125.65
       ["tz_time"]
        * string: 2021-06-15T22:00:00.000Z
       ["geo_name"]
        * string: Canarias
       ["datetime"]
        * string: 2021-06-16T00:00:00.000+02:00
     [3]
      * table:
       ["datetime_utc"]
        * string: 2021-06-15T22:00:00Z
       ["geo_id"]
        * number: 8743
       ["value"]
        * number: 125.65
       ["tz_time"]
        * string: 2021-06-15T22:00:00.000Z
       ["geo_name"]
        * string: Baleares
       ["datetime"]
        * string: 2021-06-16T00:00:00.000+02:00
     [4]
      * table:
       ["datetime_utc"]
        * string: 2021-06-15T22:00:00Z
       ["geo_id"]
        * number: 8744
       ["value"]
        * number: 125.65
       ["tz_time"]
        * string: 2021-06-15T22:00:00.000Z
       ["geo_name"]
        * string: Ceuta
       ["datetime"]
        * string: 2021-06-16T00:00:00.000+02:00
     [5]
      * table:
       ["datetime_utc"]
        * string: 2021-06-15T22:00:00Z
       ["geo_id"]
        * number: 8745
       ["value"]
        * number: 125.65
       ["tz_time"]
        * string: 2021-06-15T22:00:00.000Z
       ["geo_name"]
        * string: Melilla
       ["datetime"]
        * string: 2021-06-16T00:00:00.000+02:00
     [6]
      * table:
       ["datetime_utc"]
        * string: 2021-06-15T23:00:00Z
       ["geo_id"]
        * number: 8741
       ["value"]
        * number: 123.54
       ["tz_time"]
        * string: 2021-06-15T23:00:00.000Z
       ["geo_name"]
        * string: Península
       ["datetime"]
        * string: 2021-06-16T01:00:00.000+02:00
     [7]
      * table:
       ["datetime_utc"]
        * string: 2021-06-15T23:00:00Z
       ["geo_id"]
        * number: 8742
       ["value"]
        * number: 123.54
       ["tz_time"]
        * string: 2021-06-15T23:00:00.000Z
       ["geo_name"]
        * string: Canarias
       ["datetime"]
        * string: 2021-06-16T01:00:00.000+02:00
     [8]
      * table:
       ["datetime_utc"]
        * string: 2021-06-15T23:00:00Z
       ["geo_id"]
        * number: 8743
       ["value"]
        * number: 123.54
       ["tz_time"]
        * string: 2021-06-15T23:00:00.000Z
       ["geo_name"]
        * string: Baleares
       ["datetime"]
        * string: 2021-06-16T01:00:00.000+02:00
     [9]
      * table:
       ["datetime_utc"]
        * string: 2021-06-15T23:00:00Z
       ["geo_id"]
        * number: 8744
       ["value"]
        * number: 123.54
       ["tz_time"]
        * string: 2021-06-15T23:00:00.000Z
       ["geo_name"]
        * string: Ceuta
       ["datetime"]
        * string: 2021-06-16T01:00:00.000+02:00
     [10]
      * table:
       ["datetime_utc"]
        * string: 2021-06-15T23:00:00Z
       ["geo_id"]
        * number: 8745
       ["value"]
        * number: 123.54
       ["tz_time"]
        * string: 2021-06-15T23:00:00.000Z
       ["geo_name"]
        * string: Melilla
       ["datetime"]
        * string: 2021-06-16T01:00:00.000+02:00
     [11]
      * table:
       ["datetime_utc"]
        * string: 2021-06-16T00:00:00Z
       ["geo_id"]
        * number: 8741
       ["value"]
        * number: 122.17
       ["tz_time"]
        * string: 2021-06-16T00:00:00.000Z
       ["geo_name"]
        * string: Península
       ["datetime"]
        * string: 2021-06-16T02:00:00.000+02:00
     [12]
      * table:
       ["datetime_utc"]
        * string: 2021-06-16T00:00:00Z
       ["geo_id"]
        * number: 8742
       ["value"]
        * number: 122.17
       ["tz_time"]
        * string: 2021-06-16T00:00:00.000Z
       ["geo_name"]
        * string: Canarias
       ["datetime"]
        * string: 2021-06-16T02:00:00.000+02:00
     [13]
      * table:
       ["datetime_utc"]
        * string: 2021-06-16T00:00:00Z
       ["geo_id"]
        * number: 8743
       ["value"]
        * number: 122.17
       ["tz_time"]
        * string: 2021-06-16T00:00:00.000Z
       ["geo_name"]
        * string: Baleares
       ["datetime"]
        * string: 2021-06-16T02:00:00.000+02:00
     [14]
      * table:
       ["datetime_utc"]
        * string: 2021-06-16T00:00:00Z
       ["geo_id"]
        * number: 8744
       ["value"]
        * number: 122.17
       ["tz_time"]
        * string: 2021-06-16T00:00:00.000Z
       ["geo_name"]
        * string: Ceuta
       ["datetime"]
        * string: 2021-06-16T02:00:00.000+02:00
     [15]
      * table:
       ["datetime_utc"]
        * string: 2021-06-16T00:00:00Z
       ["geo_id"]
        * number: 8745
       ["value"]
        * number: 122.17
       ["tz_time"]
        * string: 2021-06-16T00:00:00.000Z
       ["geo_name"]
        * string: Melilla
       ["datetime"]
        * string: 2021-06-16T02:00:00.000+02:00
     [16]
      * table:
       ["datetime_utc"]
        * string: 2021-06-16T01:00:00Z
       ["geo_id"]
        * number: 8741
       ["value"]
        * number: 122.53
       ["tz_time"]
        * string: 2021-06-16T01:00:00.000Z
       ["geo_name"]
        * string: Península
       ["datetime"]
        * string: 2021-06-16T03:00:00.000+02:00
     [17]
      * table:
       ["datetime_utc"]
        * string: 2021-06-16T01:00:00Z
       ["geo_id"]
        * number: 8742
       ["value"]
        * number: 122.53
       ["tz_time"]
        * string: 2021-06-16T01:00:00.000Z
       ["geo_name"]
        * string: Canarias
       ["datetime"]
        * string: 2021-06-16T03:00:00.000+02:00
     [18]
      * table:
       ["datetime_utc"]
        * string: 2021-06-16T01:00:00Z
       ["geo_id"]
        * number: 8743
       ["value"]
        * number: 122.53
       ["tz_time"]
        * string: 2021-06-16T01:00:00.000Z
       ["geo_name"]
        * string: Baleares
       ["datetime"]
        * string: 2021-06-16T03:00:00.000+02:00
     [19]
      * table:
       ["datetime_utc"]
        * string: 2021-06-16T01:00:00Z
       ["geo_id"]
        * number: 8744
       ["value"]
        * number: 122.53
       ["tz_time"]
        * string: 2021-06-16T01:00:00.000Z
       ["geo_name"]
        * string: Ceuta
       ["datetime"]
        * string: 2021-06-16T03:00:00.000+02:00
     [20]
      * table:
       ["datetime_utc"]
        * string: 2021-06-16T01:00:00Z
       ["geo_id"]
        * number: 8745
       ["value"]
        * number: 122.53
       ["tz_time"]
        * string: 2021-06-16T01:00:00.000Z
       ["geo_name"]
        * string: Melilla
       ["datetime"]
        * string: 2021-06-16T03:00:00.000+02:00
     [21]
      * table:
       ["datetime_utc"]
        * string: 2021-06-16T02:00:00Z
       ["geo_id"]
        * number: 8741
       ["value"]
        * number: 122.7
       ["tz_time"]
        * string: 2021-06-16T02:00:00.000Z
       ["geo_name"]
        * string: Península
       ["datetime"]
        * string: 2021-06-16T04:00:00.000+02:00
     [22]
      * table:
       ["datetime_utc"]
        * string: 2021-06-16T02:00:00Z
       ["geo_id"]
        * number: 8742
       ["value"]
        * number: 122.7
       ["tz_time"]
        * string: 2021-06-16T02:00:00.000Z
       ["geo_name"]
        * string: Canarias
       ["datetime"]
        * string: 2021-06-16T04:00:00.000+02:00
     [23]
      * table:
       ["datetime_utc"]
        * string: 2021-06-16T02:00:00Z
...


Doing a test with https://reqbin.com/curl, it works perfectly, returning a JSON with all the records from 00 to 23, of the present day. Of all the Spanish territory.

The code is
Code:
    curl "https://api.esios.ree.es/indicators/1001" -X GET \
    -H "Accept: application/json; application/vnd.esios-api-v1+json" \
    -H "Content-Type: application/json" \
    -H "Host: api.esios.ree.es" \
    -H "Authorization: Token token=\"Token de ESIOS\"" \
    -H "Cookie: "\
The script is:
Code:
-- Incluir las librerías necesarias
https = require('ssl.https')
json = require('json')
ltn12 = require('ltn12')

payload =  ''

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

-- Url donde está el PVPC con su correspondiente indicador 1001 PVPC T 2.0TD
url = 'https://api.esios.ree.es/indicators/1001'

-- Petición
response_body = {}

body, code, hdrs, stat = https.request ({
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);
})
log(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 = require('json').decode(response)
log(data)
--Utilizamos los datos de la PVCP obtenidos, se divide entre 1000 para obtener kWh--
--Hora 00
grp.write('32/2/21', data.indicator.values[ 1 ].value/1000)


--Hora 01
grp.write('32/2/22', data.indicator.values[ 2 ].value/1000)

Any ideas?

Greetings
Reply
#17
Hello
I have continued investigating and with some changes.
In the URL I have put a filter so that it only shows the data of mainland Spain, in this way the rest of the territories are not shown.
Code:
curl "https://api.esios.ree.es/indicators/1001?&geo_ids[]=8741" -X GET \
    -H "Accept: application/json; application/vnd.esios-api-v1+json" \
    -H "Content-Type: application/json" \
    -H "Host: api.esios.ree.es" \
    -H "Authorization: Token token=\"TOKEN ESIOS\"" \
    -H "Cookie: "\

Although the registry remains at the value [23] ... without showing it, the data is and agrees with the values shown by the ESIOS on its website.
This makes me think that the registry in Wiser has a limit when it comes to representing the data, even if they exist, is this for something in particular?
So the questions in the previous post have answered themselves.
1. The two strings are concatenated without problems.
2. the long name still does not appear but the rest of the data is after the ... shown in the registry.
Now I have a new problem, it is assumed that ESIOS updates its data at around 8:16 p.m. every day, on its website the tariff values are already displayed from 00 today 16 until 00 tomorrow 17.
But the API call returns the values of 16 and not 17, I understand that this will be up to the API and not the script.
However, I think that it would be necessary to include a mechanism that ensures that the data has been updated using the values_updated_at field.

Greetings.
P.S. I leave the previous post unchanged in case it is useful to someone.
Reply
#18
There's a limit of 8KB for log() output so you might not get the whole result there.
Another thing to consider is that Lua table key order is not defined so when calling log() you might get certain fields in a different order. This does not affect arrays.
Reply
#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
#20
Hi I'm still fine-tuning the script.
Is there an easy way to create the virtual objects en masse for the 24 hours of the tyarifa?
Assigning the values can be done in another way and in this way not having to repeat each assignment?

is there any way to access the full content of the answer? Due to the limitation of the size of the registry, I can't see the whole thing, to know the keys / value to use.
Greetings
Reply


Forum Jump: