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.

Reading Data from awattar API
#2
Hi,

Try this:
Code:
-- Load needed libs
require("ssl.https")
require('json')

-- Fetch data from API
data, code = ssl.https.request('https://api.awattar.de/v1/marketdata')

-- Check if data is received and decode JSON to LUA table
if data then
  tabledata = json.pdecode(data)
else
  -- No data recieved, exit script
  return
end

-- Check if decoded data is available
if tabledata then
  if type(tabledata.data) == 'table' then
    cheapest = tabledata.data[1].marketprice
    cheapestindex = 1
    -- Iterate tabledata to find the cheapest hour
    for key, value in pairs(tabledata.data) do
      -- find cheapest hour
      if value.marketprice < cheapest then
        cheapest = value.marketprice
        cheapestindex = key
      end
      -- Create & Add timestamps in human readable format
      tabledata.data[key].start_timestamp_hr = os.date('%A %d %B %H:%M',math.floor(tabledata.data[cheapestindex].start_timestamp/1000 + 0.5))
      tabledata.data[key].end_timestamp_hr = os.date('%A %d %B %H:%M',math.floor(tabledata.data[cheapestindex].end_timestamp/1000 + 0.5))
    end
   
    -- Log received table
    log(tabledata.data)
   
    -- Cheapest hour
    log(tabledata.data[cheapestindex])
   
    -- Write hour to KNX object as byte value
    grp.checkwrite('1/1/1', os.date('*t', math.floor(tabledata.data[cheapestindex].start_timestamp/1000 + 0.5)).hour)
   
  end
end
BR,

Erwin
Reply


Messages In This Thread
RE: Reading Data from awattar API - by Erwin van der Zwart - 27.04.2020, 22:48

Forum Jump: