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.

Minimum value + hour and maximum value + hour of PVPC rate
#1
Hello

I obtain, through the Esios API, the hourly price of electricity in Spain.

A table like this is contained.

Code:
* 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: 2022-08-26T22:00:00Z
       ["geo_id"]
        * number: 8741
       ["value"]
        * number: 492.82
       ["tz_time"]
        * string: 2022-08-26T22:00:00.000Z
       ["geo_name"]
        * string: Península
       ["datetime"]
        * string: 2022-08-27T00:00:00.000+02:00
     [2]
      * table:
       ["datetime_utc"]
        * string: 2022-08-26T23:00:00Z
       ["geo_id"]
        * number: 8741
       ["value"]
        * number: 497.35
       ["tz_time"]
        * string: 2022-08-26T23:00:00.000Z
       ["geo_name"]
        * string: Península
       ["datetime"]
        * string: 2022-08-27T01:00:00.000+02:00
     [3]
      * table:
       ["datetime_utc"]
        * string: 2022-08-27T00:00:00Z
       ["geo_id"]
        * number: 8741
       ["value"]
        * number: 494.44
       ["tz_time"]
        * string: 2022-08-27T00:00:00.000Z
       ["geo_name"]
        * string: Península
       ["datetime"]
        * string: 2022-08-27T02:00:00.000+02:00
     [4]
      * table:
       ["datetime_utc"]
        * string: 2022-08-27T01:00:00Z
       ["geo_id"]
        * number: 8741
       ["value"]
        * number: 508.86
       ["tz_time"]
        * string: 2022-08-27T01:00:00.000Z
       ["geo_name"]
        * string: Península
       ["datetime"]
        * string: 2022-08-27T03:00:00.000+02:00
     [5]
      * table:
       ["datetime_utc"]
        * string: 2022-08-27T02:00:00Z
       ["geo_id"]
        * number: 8741
       ["value"]
        * number: 531.02
       ["tz_time"]
        * string: 2022-08-27T02:00:00.000Z
       ["geo_name"]
        * string: Península
       ["datetime"]
        * string: 2022-08-27T04:00:00.000+02:00
     [6]
      * table:
       ["datetime_utc"]
        * string: 2022-08-27T03:00:00Z
       ["geo_id"]
        * number: 8741
       ["value"]
        * number: 553.12
       ["tz_time"]
        * string: 2022-08-27T03:00:00.000Z
       ["geo_name"]
        * string: Península
       ["datetime"]
        * string: 2022-08-27T05:00:00.000+02:00
     [7]
      * table:
       ["datetime_utc"]
        * string: 2022-08-27T04:00:00Z
       ["geo_id"]
        * number: 8741
       ["value"]
        * number: 568.16
       ["tz_time"]
        * string: 2022-08-27T04:00:00.000Z
       ["geo_name"]
        * string: Península
       ["datetime"]
        * string: 2022-08-27T06:00:00.000+02:00
     [8]
      * table:
       ["datetime_utc"]
        * string: 2022-08-27T05:00:00Z
       ["geo_id"]
        * number: 8741
       ["value"]
        * number: 557.06
       ["tz_time"]
        * string: 2022-08-27T05:00:00.000Z
       ["geo_name"]
        * string: Península
       ["datetime"]
        * string: 2022-08-27T07:00:00.000+02:00
     [9]
      * table:
       ["datetime_utc"]
        * string: 2022-08-27T06:00:00Z
       ["geo_id"]
        * number: 8741
       ["value"]
        * number: 507.94
       ["tz_time"]
        * string: 2022-08-27T06:00:00.000Z
       ["geo_name"]
        * string: Península
       ["datetime"]
        * string: 2022-08-27T08:00:00.000+02:00
     [10]
      * table:
       ["datetime_utc"]
        * string: 2022-08-27T07:00:00Z
       ["geo_id"]
        * number: 8741
       ["value"]
        * number: 445.72
       ["tz_time"]
        * string: 2022-08-27T07:00:00.000Z
       ["geo_name"]
        * string: Península
       ["datetime"]
        * string: 2022-08-27T09:00:00.000+02:00
     [11]
      * table:
       ["datetime_utc"]
        * string: 2022-08-27T08:00:00Z
       ["geo_id"]
        * number: 8741
       ["value"]
        * number: 405.18
       ["tz_time"]
        * string: 2022-08-27T08:00:00.000Z
       ["geo_name"]
        * string: Península
       ["datetime"]
        * string: 2022-08-27T10:00:00.000+02:00
     [12]
      * table:
       ["datetime_utc"]
        * string: 2022-08-27T09:00:00Z
       ["geo_id"]
        * number: 8741
       ["value"]
        * number: 389.13
       ["tz_time"]
        * string: 2022-08-27T09:00:00.000Z
       ["geo_name"]
        * string: Península
       ["datetime"]
        * string: 2022-08-27T11:00:00.000+02:00
     [13]
      * table:
       ["datetime_utc"]
        * string: 2022-08-27T10:00:00Z
       ["geo_id"]
        * number: 8741
       ["value"]
        * number: 381.83
       ["tz_time"]
        * string: 2022-08-27T10:00:00.000Z
       ["geo_name"]
        * string: Península
       ["datetime"]
        * string: 2022-08-27T12:00:00.000+02:00
     [14]
      * table:
       ["datetime_utc"]
        * string: 2022-08-27T11:00:00Z
       ["geo_id"]
        * number: 8741
       ["value"]
        * number: 381.23
       ["tz_time"]
        * string: 2022-08-27T11:00:00.000Z
       ["geo_name"]
        * string: Península
       ["datetime"]
        * string: 2022-08-27T13:00:00.000+02:00
     [15]
      * table:
       ["datetime_utc"]
        * string: 2022-08-27T12:00:00Z
       ["geo_id"]
        * number: 8741
       ["value"]
        * number: 385.12
       ["tz_time"]
        * string: 2022-08-27T12:00:00.000Z
       ["geo_name"]
        * string: Península
       ["datetime"]
        * string: 2022-08-27T14:00:00.000+02:00
     [16]
      * table:
       ["datetime_utc"]
        * string: 2022-08-27T13:00:00Z
       ["geo_id"]
        * number: 8741
       ["value"]
        * number: 391.42
       ["tz_time"]
        * string: 2022-08-27T13:00:00.000Z
       ["geo_name"]
        * string: Península
       ["datetime"]
        * string: 2022-08-27T15:00:00.000+02:00
     [17]
      * table:
       ["datetime_utc"]
        * string: 2022-08-27T14:00:00Z
       ["geo_id"]
        * number: 8741
       ["value"]
        * number: 400.21
       ["tz_time"]
        * string: 2022-08-27T14:00:00.000Z
       ["geo_name"]
        * string: Península
       ["datetime"]
        * string: 2022-08-27T16:00:00.000+02:00
     [18]
      * table:
       ["datetime_utc"]
        * string: 2022-08-27T15:00:00Z
       ["geo_id"]
        * number: 8741
       ["value"]
        * number: 407.64
       ["tz_time"]
        * string: 2022-08-27T15:00:00.000Z
       ["geo_name"]
        * string: Península
       ["datetime"]
        * string: 2022-08-27T17:00:00.000+02:00
     [19]
      * table:
       ["datetime_utc"]
        * string: 2022-08-27T16:00:00Z
       ["geo_id"]
        * number: 8741
       ["value"]
        * number: 414.83
       ["tz_time"]
        * string: 2022-08-27T16:00:00.000Z
       ["geo_name"]
        * string: Península
       ["datetime"]
        * string: 2022-08-27T18:00:00.000+02:00
     [20]
      * table:
       ["datetime_utc"]
        * string: 2022-08-27T17:00:00Z
       ["geo_id"]
        * number: 8741
       ["value"]
        * number: 491.04
       ["tz_time"]
        * string: 2022-08-27T17:00:00.000Z
       ["geo_name"]
        * string: Península
       ["datetime"]
        * string: 2022-08-27T19:00:00.000+02:00
     [21]
      * table:
       ["datetime_utc"]
        * string: 2022-08-27T18:00:00Z
       ["geo_id"]
        * number: 8741
       ["value"]
        * number: 544.13
       ["tz_time"]
        * string: 2022-08-27T18:00:00.000Z
       ["geo_name"]
        * string: Península
       ["datetime"]
        * string: 2022-08-27T20:00:00.000+02:00
     [22]
      * table:
       ["datetime_utc"]
        * string: 2022-08-27T19:00:00Z
       ["geo_id"]
        * number: 8741
       ["value"]
        * number: 555.8
       ["tz_time"]
        * string: 2022-08-27T19:00:00.000Z
       ["geo_name"]
        * string: Península
       ["datetime"]
        * string: 2022-08-27T21:00:00.000+02:00
     [23]
      * table:
       ["date...


What I want is to save in virtual objects, the following:

The lowest price and the corresponding hour.
The highest price and the time to which it corresponds.

How could I do it?

I already have in virtual objects each price value.

In simpler tables I think I could do it with math.max or by sorting the table and keeping the first and last value. But in such a complex table and with the time, I don't know how to do it.

Greetings
Reply
#2
Try this (data is decoded JSON response):
Code:
values = data.indicator.values

table.sort(values, function(a, b)
  return a.value < b.value
end)

min = values[1]
minvalue = min.value
minhour = min.datetime:match('T(%d+)')

log(minvalue, minhour)

max = values[#values]
maxvalue = max.value
maxhour = max.datetime:match('T(%d+)')

log(maxvalue, maxhour)
Reply
#3
thanks, i will try it
greeting
Reply
#4
It works perfectly, thanks

Greeting
Reply


Forum Jump: