27.04.2020, 22:48
(This post was last modified: 27.04.2020, 22:49 by Erwin van der Zwart.)
Hi,
Try this:
BR,
Erwin
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
Erwin