Posts: 19
Threads: 4
Joined: Jun 2020
Reputation:
0
13.02.2022, 15:38
(This post was last modified: 13.02.2022, 15:41 by Tue.)
Hello there
I have a table looeking like this (but up to 40 lines):
local tbl = {
{month = 2, day = 13, hour = 9, price_raw = 0.12 , price_total= 0.42},
{month = 2, day = 13, hour = 10, price_raw = 0.22 , price_total= 0.49},
{month = 2, day = 13, hour = 11, price_raw = 0.23 , price_total= 0.43},
{month = 2, day = 13, hour = 12, price_raw = 0.11 , price_total= 0.44},
{month = 2, day = 13, hour = 13, price_raw = 0.13 , price_total= 0.48},
{month = 2, day = 13, hour = 14, price_raw = 0.19 , price_total= 0.38},
{month = 2, day = 13, hour = 15, price_raw = 0.14 , price_total= 0.41},
{month = 2, day = 13, hour = 16, price_raw = 0.16 , price_total= 0.28}
}
How do I sort the table after [price_total] ?
Now I want to make a Schedule, lets call them, Charge_ev_in_min
How do I edit a schedule from script? I need to do the following:
Set time and date for start
Set value (0-600)
Disable scheduler
Enable scheduler
delete schedule
BR
Tue
Posts: 1764
Threads: 6
Joined: Jul 2015
Reputation:
117
14.02.2022, 07:10
(This post was last modified: 14.02.2022, 07:11 by Erwin van der Zwart.)
For the sorting use this:
Code: table.sort(tbl, function(a, b) return a['price_total'] < b['price_total'] end)
For the update use this (old) sample:
Posts: 19
Threads: 4
Joined: Jun 2020
Reputation:
0
(14.02.2022, 07:10)Erwin van der Zwart Wrote: For the sorting use this:
Code: table.sort(tbl, function(a, b) return a['price_total'] < b['price_total'] end)
For the update use this (old) sample:
Thx for that, It seams like when I first storage the table using:
Code: storage.set('NordPool_prices', pris_array_alle)
And after that
Code: tbl_price = storage.get('NordPool_prices') -- Get prize from Nordpool
table.sort(tbl_price, function(a, b) return a['price_total'] > b['price_total'] end)
log(tbl_price)
It cannot sort it? Bellow is how the log looks, is there somthing with the table .. table here?
Code: Event for Elprisen er lige nu (7/1/2) 14.02.2022 14:44:51
* table:
[0]
* table:
["price_raw"]
* number: 0.7961544
["date"]
* number: 14
["hour"]
* number: 14
["month"]
* number: 2
["price_total"]
* number: 1.3961544
[1]
* table:
["price_raw"]
* number: 0.8211528
["date"]
* number: 14
["hour"]
* number: 15
["month"]
* number: 2
["price_total"]
* number: 1.4211528
[2]
* table:
["price_raw"]
* number: 0.8022552
["date"]
* number: 14
["hour"]
* number: 16
["month"]
* number: 2
["price_total"]
* number: 1.4022552
[13]
* table:
["price_raw"]
* number: 0.7928064
["date"]
* number: 15
["hour"]
* number: 3
["month"]
* number: 2
["price_total"]
* number: 1.3928064
[31]
* table:
["price_raw"]
* number: 0.6808344
["date"]
* number: 15
["hour"]
* number: 21
["month"]
* number: 2
["price_total"]
* number: 1.2808344
[11]
* table:
["price_raw"]
* number: 0.7898304
["date"]
* number: 15
["hour"]
* number: 1
["month"]
* number: 2
["price_total"]
* number: 1.3898304
[27]
* table:
["price_raw"]
* number: 0.8695128
["date"]
* number: 15
["hour"]
* number: 17
["month"]
* number: 2
["price_total"]
* number: 1.6495128
[9]
* table:
["price_raw"]
* number: 0.7766616
["date"]
* number: 14
["hour"]
* number: 23
["month"]
* number: 2
["price_total"]
* number: 1.3766616
[23]
* table:
["price_raw"]
* number: 0.8261376
["date"]
* number: 15
["hour"]
* number: 13
["month"]
* number: 2
["price_total"]
* number: 1.4261376
[19]
* table:
["price_raw"]
* number: 0.88164
["date"]
* number: 15
["hour"]
* number: 9
["month"]
* number: 2
["price_total"]
* number: 1.48164
[32]
* table:
["price_raw"]
* number: 0.7582104
["date"]
* number: 15
["hour"]
* number: 22
["month"]
* number: 2
["price_total"]
* number: 1.3582104
[28]
* table:
["price_raw"]
* number: 0.855228
["date"]
* number: 15
["hour"]
* number: 18
["month"]
* number: 2
["price_total"]
* number: 1.635228
[24]
* table:
["price_raw"]
* number: 0.8245008
["date"]
* number: 15
["hour"]
* number: 14
["month"]
* number: 2
["price_total"]
* number: 1.4245008
[33]
* table:
["price_raw"]
* number: 0.6540504
["date"]
* number: 15
["hour"]
* number: 23
["month"]
* number: 2
["price_total"]
* number: 1.2540504
[20]
* table:
["price_raw"]
* number: 0.8489784
["date"]
* number: 15
["hour"]
* number: 10
["month"]
* number: 2
["price_total"]
* number: 1.4489784
[16]
* table:
["price_raw"]
* number: 0.8242776
["date"]
* number: 15
["hour"]
* number: 6
["month"]
* number: 2
["price_total"]
* number: 1.4242776
[14]
* table:
["price_raw"]
* number: 0.7944432
["date"]
* number: 15
["hour"]
* number: 4
["month"]
* number: 2
["price_total"]
* number: 1.3944432
[12]
* table:
["price_raw"]
* number: 0.7861104
["date"]
* number: 15
["hour"]
* number: 2
["month"]
* number: 2
["price_total"]
* number: 1.3861104
[10]
* table:
["price_raw"]
* number: 0.7887888
["date"]
* number: 15
["hour"]
* number: 0
["month"]
* number: 2
["price_total"]
* number: 1.3887888
[8]
* table:
["price_raw"]
* number: 0.8000976
["date"]
* number: 14
["hour"]
* number: 22
["month"]
* number: 2
["price_total"]
* number: 1.4000976
[7]
* table:
["price_raw"]
* number: 0.8084304
["date"]
* number: 14
["hour"]
* number: 21
["month"]
* number: 2
["price_total"]
* number: 1.4084304
[6]
* table:
["price_raw"]
* number: 0.8171352
["date"]
* number: 14
["hour"]
* number: 20
["month"]
* number: 2
["price_total"]
* number: 1.4171352
[5]
* table:
["price_raw"]
* number: 0.7989816
["date"]
* number: 14
["hour"]
* number: 19
["month"]
* number: 2
["price_total"]
* number: 1.5789816
[4]
* table:
["price_raw"]
* number: 0.8688432
["date"]
* number: 14
["hour"]
* number: 18
["month"]
* number: 2
["price_total"]
* number: 1.6488432
[30]
* table:
["price_raw"]
* number: 0.7835064
["date"]
* number: 15
["hour"]
* number: 20
["month"]
* number: 2
["price_total"]
* number: 1.3835064
[29]
* table:
["price_raw"]
* number: 0.8146056
["date"]
* number: 15
["hour"]
* number: 19
["month"]
* number: 2
["price_total"]
* number: 1.5946056
[22]
* table:
["price_raw"]
* number: 0.8294112
["date"]
* number: 15
["hour"]
* number: 12
["month"]
* number: 2
["price_total"]
* number: 1.4294112
[26]
* table:
["price_raw"]
* number: 0.8003952
["date"]
* number: 15
["hour"]
* number: 16
["month"]
* number: 2
["price_total"]
* number: 1.4003952
[25]
* table:
["price_raw"]
* number: 0.821748
["date"]
* number: 15
["hour"]
* number: 15
["month"]
* number: 2
["price_total"]
* number: 1.421748
[3]
* table:
["price_raw"]
* number: 0.8733072
["date"]
* number: 14
["hour"]
* number: 17
["month"]
* number: 2
["price_total"]
* number: 1.6533072
[21]
* table:
["price_raw"]
* number: 0.8371488
["date"]
* number: 15
["hour"]
* number: 11
["month"]
* number: 2
["price_total"]
* number: 1.4371488
[18]
* table:
["price_raw"]
* number: 0.8847648
["date"]
* number: 15
["hour"]
* number: 8
["month"]
* number: 2
["price_total"]
* number: 1.4847648
[15]
* table:
["price_raw"]
* number: 0.8076864
["date"]
* number: 15
["hour"]
* number: 5
["month"]
* number: 2
["price_total"]
* number: 1.4076864
[17]
* table:
["price_raw"]
* number: 0.873828
["date"]
* number: 15
["hour"]
* number: 7
["month"]
* number: 2
["price_total"]
* number: 1.473828
This is how I make the table in the beginning
Code: NordPool_pricepriser[NordPool_price_i] = {month = tonumber(os.date('%m' , os.time() + (3600*NordPool_price_i))), date = tonumber(os.date('%d' , os.time() + (3600*NordPool_price_i))), hour = tonumber(os.date('%H' , os.time() + (3600*NordPool_price_i))),price_raw = NordPool_price_pris, price_total = NordPool_price_pris + total_add_price}-- store price and time for price
if NordPool_pricepriser[NordPool_price_i].hour == 17 or NordPool_pricepriser[NordPool_price_i].hour == 18 or NordPool_pricepriser[NordPool_price_i].hour == 19 then
NordPool_pricepriser[NordPool_price_i].price_total = NordPool_pricepriser[NordPool_price_i].price_total + Konstant_ekstra_tariff_17_20
end
and that routine is called and then stoareg like this
Code: pris_array_alle = NordPool_price() -- Get prices = {month, day, hour, price_raw, price_total}
storage.set('NordPool_prices', pris_array_alle)
Posts: 7782
Threads: 42
Joined: Jun 2015
Reputation:
448
Since you have [0] index in the table it is not treated as an array (index 1..n) so it cannot be sorted. You can convert it to array like this:
Code: tbl_price = storage.get('NordPool_prices') -- Get prize from Nordpool
arr_price = {}
for _, row in pairs(tbl_price) do
arr_price[ #arr_price + 1 ] = row
end
table.sort(arr_price, function(a, b) return a['price_total'] > b['price_total'] end)
log(arr_price)
Posts: 19
Threads: 4
Joined: Jun 2020
Reputation:
0
(14.02.2022, 14:02)admin Wrote: Since you have [0] index in the table it is not treated as an array (index 1..n) so it cannot be sorted. You can convert it to array like this:
Code: tbl_price = storage.get('NordPool_prices') -- Get prize from Nordpool
arr_price = {}
for _, row in pairs(tbl_price) do
arr_price[ #arr_price + 1 ] = row
end
table.sort(arr_price, function(a, b) return a['price_total'] > b['price_total'] end)
log(arr_price)
YOU ARE A LIFESAVER :-)!!
|