20.12.2022, 07:39
Hi!
Here is a script, that gives six objects 1.current_time; 2.current_price; 3.cheapest_hour; 4.cheapest_price; 5.costly_hour; 6.costly_price
Here is a script, that gives six objects 1.current_time; 2.current_price; 3.cheapest_hour; 4.cheapest_price; 5.costly_hour; 6.costly_price
Code:
https = require('ssl.https')
json = require('json')
ltn12 = require('ltn12')
country = 'lv' -- Latvia
--country = 'fi' -- Finland
--country = 'lt' -- Lithuania
--country = 'ee' -- Estonia
obj_current_time = grp.create({ datatype = dt.time, address = '35/5/11', name = 'current_time'})
obj_current_price = grp.create({ datatype = dt.float32, address = '35/5/12', name = 'current_price'})
obj_cheapest_hour = grp.create({ datatype = dt.time, address = '35/5/13', name = 'cheapest_hour'})
obj_cheapest_hour_price = grp.create({ datatype = dt.float32, address = '35/5/14', name = 'cheapest_price'})
obj_costly_hour = grp.create({ datatype = dt.time, address = '35/5/15', name = 'costly_hour'})
obj_costly_hour_price = grp.create({ datatype = dt.float32, address = '35/5/16', name = 'costly_price'})
--[[]]
current_date = os.date('*t')
previous_date = os.date('*t', os.time()-24*60*60)
--log(previous_date)
if previous_date.month < 10 then
previous_date.month = '0' .. previous_date.month
end
if previous_date.day < 10 then
previous_date.day = '0' .. previous_date.day
end
previous_date_str = previous_date.year .. '-' .. previous_date.month .. '-' .. previous_date.day
--log(previous_date_str)
next_date = os.date('*t', os.time() + 24*60*60)
--log(next_date)
if next_date.month < 10 then
next_date.month = '0' .. next_date.month
end
if next_date.day < 10 then
next_date.day = '0' .. next_date.day
end
next_date_str = next_date.year .. '-' .. next_date.month .. '-' .. next_date.day
--log(next_date_str)
url = 'https://dashboard.elering.ee/api/nps/price?start='.. previous_date_str ..'T00%3A00%3A00.999Z&end=' .. next_date_str ..'T00%3A00%3A00.999Z'
--log(url)
mac = 0
io.readfile('/sys/class/net/eth0/address'):gsub('%x%x', function(v)
mac = mac * 256 + tonumber(v, 16)
end)
response = {}
res, code = https.request({
url = url,
protocol = 'tlsv12',
headers = {
['user-agent'] = 'LM ' .. mac
},
sink = ltn12.sink.table(response)
})
if res and code == 200 then
data = json.pdecode(table.concat(response))
else
log('request error', res, code)
end
--log(data)
--log(data['data'][country])
min_price = 99999
max_price = -1
min_price_hour = -1
max_price_hour = -1
for i = 23, 47, 1 do -- 23 is 00:00 for GMT+0200 (Eastern European Standard Time)
if data['data'][country][i] then
price = data['data'][country][i]['price']
if price < min_price then
min_price = price -- EUR/MegaWatt*hour
min_price_hour = i - 23 -- get hour for GMT+0200 (Eastern European Standard Time)
end
if price > max_price then
max_price = price -- EUR/MegaWatt*hour
max_price_hour = i - 23 -- get hour for GMT+0200 (Eastern European Standard Time)
end
end
end
--log(min_price_hour)
--log(min_price)
--log(max_price_hour)
--log(max_price)
curr_day_time = {}
if current_date['wday'] > 1 then
current_date['wday'] = current_date['wday'] - 1 -- 1 is Monday, 2 is Tuesday
else
current_date['wday'] = 7 -- 7 is Sunday
end
curr_day_time['day'] = current_date['wday']
curr_day_time['hour'] = current_date['hour']
curr_day_time['minute'] = current_date['min']
grp.write(obj_current_time, curr_day_time) -- 3 byte time day
current_price = data['data'][country][ current_date['hour'] + 23 ]['price'] -- EUR/MegaWatt*hour
--[[
timestamp = data['data'][country][ current_date['hour'] + 23 ]['timestamp']
log(timestamp)
temp = os.date("*t", timestamp)
log(temp)
--]]
grp.write(obj_current_price, current_price) -- float
cheapest_day_time = {}
cheapest_day_time['day'] = current_date['wday']
cheapest_day_time['hour'] = min_price_hour
grp.write(obj_cheapest_hour, cheapest_day_time) -- 3 byte time day
grp.write(obj_cheapest_hour_price, min_price) -- float
costly_day_time = {}
costly_day_time['day'] = current_date['wday']
costly_day_time['hour'] = max_price_hour
grp.write(obj_costly_hour, costly_day_time) -- 3 byte time day
grp.write(obj_costly_hour_price, max_price) -- float
--]]