LogicMachine Forum
Script with Dutch power prices - Printable Version

+- LogicMachine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Script with Dutch power prices (/showthread.php?tid=4068)



Script with Dutch power prices - jeroen@stevens.biz - 29.05.2022

Hi. I want to read out the current hourly power prices (Dutch EPEX spot market). With this info I want to switch on heavy power users (car charging, airco etc) and switch off my solar panels when the market prices are below -/-0,06729 per kWh (since then the energy taxes don't compensate the negative market price, taxes are 0,03679 per kWh + 0,0305 per kWh). I found a website where I can read out these prices, but how can I let a script return the hourly values in a way I can make decisions with it?

With this simple script (where I've put in the time range of 1 day), I get the following output;

--/--

local https = require('ssl.https')
local ltn12 = require('ltn12')
local json = require('json')
local cBody = 'TariffReturn'
local cReq = {}

result1 = https.request({
  url = 'https://mijn.easyenergy.com/nl/api/tariff/getapxtariffs?startTimestamp=2022-05-29T22%3A00%3A00.000Z&endTimestamp=2022-05-30T22%3A00%3A00.000Z&grouping=',
  method = 'GET',
  headers = {
    ['content-length'] = #cBody,
    ['content-type'] = 'application/json'
  },
  source = ltn12.source.string(cBody),
  sink = ltn12.sink.table(cReq)
})

if (cReq) then
  mydata =  json.pdecode(table.concat(cReq))
  log(mydata)

end


--/--

In the output I refer to this part:

--/
....
[4]
  * table:
  ["Timestamp"]
    * string: 2022-05-30T01:00:00+00:00
  ["TariffReturn"]
    * number: 0.1845
  ["TariffUsage"]
    * number: 0.223245
  ["SupplierId"]
    * number: 0


/--
where '  ["TariffReturn"]
    * number: 0.1845
the price per kWh is for every kWh consumed (or delivered) between 2022-05-30, 04.00 - 05.00u

The notation is a little bit confusion, since the table states it is at 01.00.

I check the output of easyenergy with one of the other EPEX spot market parties: https://prijzen.powerhouse.net/forecast/

Any help is welcome.

Jeroen


RE: Script with Dutch power prices - Daniel - 30.05.2022

log(mydata[4].TariffReturn)


RE: Script with Dutch power prices - admin - 30.05.2022

Try this, it will automatically use current date time and will provide prices for the current and the next hour.

Code:
json = require('json') http = require('socket.http') escape = require('socket.url').escape now = os.date('!%Y-%m-%dT%H:00:00+00:00') today = os.date('!%Y-%m-%d') tomorrow = os.date('!%Y-%m-%d', os.time() + 86400) ts = escape(today .. 'T00:00:00+00:00') te = escape(tomorrow .. 'T00:00:00+00:00') url = 'https://mijn.easyenergy.com/nl/api/tariff/getapxtariffs?startTimestamp=' .. ts .. '&endTimestamp=' .. te res, err = http.request({   url = url,   headers = {     ['content-type'] = 'application/json'   } }) if res then   data = json.pdecode(res)   if type(data) == 'table' then     for i, item in ipairs(data) do       if item.Timestamp == now then         tariff_return_now = item.TariffReturn         if data[ i + 1 ] then           tariff_return_next = data[ i + 1 ].TariffReturn         end         break       end     end     log(tariff_return_now, tariff_return_next)   else     log('invalid data', res)   end else   log('request failed', err) end



RE: Script with Dutch power prices - jeroen@stevens.biz - 30.05.2022

Thanks Admin, this works like a charm. I only had to make a little adjustment, since the price of 'now' is the price for the 'next hour'. Some for the price of 'next hour', that is the price of 2 hours later.
I guess this script shall be helpfull to many more people in the future since the power prices are so changing every hour.

Greets
Jeroen


RE: Script with Dutch power prices - admin - 31.05.2022

Check that you have correct timezone set in Date/Time settings. Reported time is in UTC so incorrect timezone can explain 1 hour shift.


RE: Script with Dutch power prices - JASK - 15.03.2023

Hello,
I'm trying to create a script to retrieve tomorrow's electricity prices with api: https://www.hvakosterstrommen.no/strompris-api.

I try this:
Code:
local http = require('ssl.https') local json = require('json') local date = os.date('*t', os.time() + 86400) -- get tomorrow's date local year = tostring(date.year) local month = string.format('%02d', date.month) local day = string.format('%02d', date.day) local area = 'NO1' -- specify area local url = string.format('https://www.hvakosterstrommen.no/api/v1/prices/%s/%s-%s_%s.json', year, month, day, area) print('Fetching data from URL:', url) local body, code, headers, status = http.request(url) print('HTTP response code:', code) if code == 200 then     print('Data retrieved successfully')     print('Response body:', body)     local data = json.decode(body)     local group_address = '32/1/%d' -- starting group address     for i, item in ipairs(data) do         local hour = tonumber(string.sub(item.time_start, 12, 13))         local price = item.NOK_per_kWh         print('Hour:', hour)         print('Price:', price)         if price ~= nil then             local value = string.format('%014s', string.format('%.4f', price * 10000)):gsub(' ', '0')             print('Value:', value)             grp.write(string.format(group_address, hour), value, '14ByteAsc')         end     end else     print('Error fetching data: ' .. status) end

I'm having a bit of trouble getting the data out.


RE: Script with Dutch power prices - admin - 16.03.2023

'14ByteAsc' is not a valid data type use dt.string instead (without quotes). You also need to setup your script to run at a certain time when tomorrow prices become available. Right now it returns 404 because the data is not available yet.