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
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
We have rewritten script (old script will not work properly), because of a change
in time intervals (previously electricity price was per hour, now per 15 minutes).
hourly_prices = {}
for h = 0, 23 do
hourly_prices[h] = { sum = 0, count = 0 }
end
if not data or not data.data or not data.data[country] then
log('No data for country: ' .. country)
return
end
for _, record in ipairs(data.data[country]) do
local timestamp = record.timestamp
local price = record.price
if timestamp >= day_start_ts and timestamp <= day_end_ts then
if price < min_price then
min_price = price
min_price_timestamp = timestamp
end
if price > max_price then
max_price = price
max_price_timestamp = timestamp
end
local record_time = os.date('*t', timestamp)
local hour = record_time.hour
if hourly_prices[hour] then
hourly_prices[hour].sum = hourly_prices[hour].sum + price
hourly_prices[hour].count = hourly_prices[hour].count + 1
end
end
if current_ts >= timestamp and current_ts < (timestamp + 15 * 60) then
current_price = price
end
end
--log('--- Current Status ---')
if current_price then
--log('Current 15-min price: ' .. current_price .. ' EUR/MWh')
grp.write(current_price_addr, current_price) -- float
else
log('Could not determine current price')
end
for hour, data in pairs(hourly_prices) do
if data.count > 0 then
local avg_price = data.sum / data.count
--log(string.format("Hour %02d:00 - Avg Price: %.2f", hour, avg_price))
if avg_price < min_avg_price then
min_avg_price = avg_price
cheapest_hour = hour
end
if avg_price > max_avg_price then
max_avg_price = avg_price
costliest_hour = hour
end
end
end
We have rewritten script (old script will not work properly), because of a change
in time intervals (previously electricity price was per hour, now per 15 minutes).
hourly_prices = {}
for h = 0, 23 do
hourly_prices[h] = { sum = 0, count = 0 }
end
if not data or not data.data or not data.data[country] then
log('No data for country: ' .. country)
return
end
for _, record in ipairs(data.data[country]) do
local timestamp = record.timestamp
local price = record.price
if timestamp >= day_start_ts and timestamp <= day_end_ts then
if price < min_price then
min_price = price
min_price_timestamp = timestamp
end
if price > max_price then
max_price = price
max_price_timestamp = timestamp
end
local record_time = os.date('*t', timestamp)
local hour = record_time.hour
if hourly_prices[hour] then
hourly_prices[hour].sum = hourly_prices[hour].sum + price
hourly_prices[hour].count = hourly_prices[hour].count + 1
end
end
if current_ts >= timestamp and current_ts < (timestamp + 15 * 60) then
current_price = price
end
end
--log('--- Current Status ---')
if current_price then
--log('Current 15-min price: ' .. current_price .. ' EUR/MWh')
grp.write(current_price_addr, current_price) -- float
else
log('Could not determine current price')
end
for hour, data in pairs(hourly_prices) do
if data.count > 0 then
local avg_price = data.sum / data.count
--log(string.format("Hour %02d:00 - Avg Price: %.2f", hour, avg_price))
if avg_price < min_avg_price then
min_avg_price = avg_price
cheapest_hour = hour
end
if avg_price > max_avg_price then
max_avg_price = avg_price
costliest_hour = hour
end
end
end
We have prepared script for electricity prices for Denmark, Norway, Sweden and Germany
Code:
price_area = 'DK1' -- west Denmark DK1 grid region
--price_area = 'DK2' -- east Denmark DK2 grid region
--price_area = 'DE' -- Germany
--price_area = 'NO2' -- southern and southwestern Norway
--price_area = 'SE3' -- north Sweden
--price_area = 'SE4' -- south Sweden
current_price_addr = '40/1/18' -- 14. 4 byte floating point
cheapest_hour_addr = '40/1/16' -- 10. 3 byte time / day
cheapest_hour_price_addr = '40/1/17' -- 14. 4 byte floating point
costliest_hour_addr = '40/1/19' -- 10. 3 byte time / day
costliest_hour_price_addr = '40/1/20' -- 14. 4 byte floating point
for _, record in ipairs(data.records) do
ts = record.TimeDK:gsub('%-', ''):gsub('T', ''):gsub(':', '')
hour = tonumber(ts:sub(1, 10))
price = record.DayAheadPriceEUR
if hour >= curr_hour then
store = prices[ hour ] or { sum = 0, count = 0 }
store.sum = store.sum + price
store.count = store.count + 1
prices[ hour ] = store
end
if ts:sub(1, 12) == curr_ts then
curr_price = price
end
end
hourly = {}
for hour, store in pairs(prices) do
price = store.sum / store.count
hourly[ #hourly + 1 ] = {
hour = hour,
price = price
}
end
table.sort(hourly, function(a, b)
return a.price > b.price
end)
function update(store, hour_addr, price_addr)
local hour = tostring(store.hour)
local date = hour:sub(1, 8)
local day = date == curr_date and curr_day or next_day
grp.checkupdate(hour_addr, { day = day, hour = hour:sub(9, 10) })
grp.checkupdate(price_addr, store.price)
end