09.02.2022, 09:20 (This post was last modified: 09.02.2022, 12:51 by Tue.)
Hello there
I'm newbee to scripting to a API, I need you help to get me startet. I can from bellow link get the eletric pricing for the next 24 hours (is updated everyday at 14:00 for the next day)
And I do know that I need to make some ssl.https.request(url) there I change from current time (date = os.date('*t')) and make a new string with date.time and so on. But anybody that can help me in the right directions?
09.02.2022, 12:30 (This post was last modified: 09.02.2022, 12:52 by Tue.)
(09.02.2022, 09:20)Tue Wrote: Hello there
I'm newbee to scripting to a API, I need you help to get me startet. I can from bellow link get the eletric pricing for the next 24 hours (is updated everyday at 14:00 for the next day)
And I do know that I need to make some ssl.https.request(url) there I change from current time (date = os.date('*t')) and make a new string with date.time and so on. But anybody that can help me in the right directions?
--[[************************************************************************************************Created: 2022-02-09Get eletric pricing from Norpol and set car charge when good :-)************************************************************************************************]]--require('json')
https = require'ssl.https'localdate = os.date('*t')
localdata = ' 'localurl = ' 'fori=1, 24dodate.hour = date.hour + 1ifdate.hour > 23thendate.day = date.day + 1date.hour = 0end-- Missing if month changes or year (to come later)data = date.year .. '-' .. date.month .. '-' .. date.day .. 'T' .. date.hour .. ':00:00'-- Make the date/clock string to requesturl = 'https://api.energidataservice.dk/datastore_search?resource_id=elspotprices&filters=%7B%22HourDK%22:%22' .. data .. '%22,%22PriceArea%22:%22DK1%22%7D'-- Here is the requst string :-)status = ssl.https.request(url) -- Here comes reply from serverlog(status) -- How do I decode it and get it into a time tableend--[[This is returned format..:{"help": "https://api.energidataservice.dk/help_show?name=datastore_search","success": true,"result": {"include_total": true,"resource_id": "c86859d2-942e-4029-aec1-32d56f1a2e5d","fields": [{"type": "int","id": "_id"},{"type": "timestamptz","id": "HourUTC"},{"type": "timestamp","id": "HourDK"},{"type": "text","id": "PriceArea"},{"type": "float8","id": "SpotPriceDKK"},{"type": "float8","id": "SpotPriceEUR"}],"records_format": "objects","records": [{"_id": 2023070,"HourUTC": "2022-02-09T09:00:00+00:00","HourDK": "2022-02-09T10:00:00","PriceArea": "DK1","SpotPriceDKK": 885.32, <-***************************** This is the value what I need ***********************"SpotPriceEUR": 118.94}],"_links": {"start": "/datastore_search?filters=%7B%22HourDK%22%3A%222022-02-09T10%3A00%3A00%22%2C%22PriceArea%22%3A%22DK1%22%7D&resource_id=elspotprices","next": "/datastore_search?offset=100&filters=%7B%22HourDK%22%3A%222022-02-09T10%3A00%3A00%22%2C%22PriceArea%22%3A%22DK1%22%7D&resource_id=elspotprices"},"filters": {"HourDK": "2022-02-09T10:00:00","PriceArea": "DK1"},"total": 1}} ]]--
--[[************************************************************************************************Created: 2022-02-09Get eletric pricing from Norpol the next 24 hours and set car charge when good :-)It works now, needs to sort out lowest rates and so on************************************************************************************************]]--require('json')
https = require'ssl.https'localdata = ' 'localurl = ' 'localpriser = {klokken, pris}
localend_i = 11-- if hour is <= 13 it must stop at 23 hours (no new prices yet)-- if hour is > 13 there are prices until followering midnight (more than 24 hoursiftonumber(os.date('%H', os.time())) <= 12thenend_i = 23 - tonumber(os.date('%H', os.time()))
elseend_i = (23 - tonumber(os.date('%H', os.time()))) + 24end-- Get prices for the next hours, defined in "end_i"-- Prices are from Norpol energy trader-- They are from west region Denmarkfori=0, end_idodata = os.date('%Y-%m-%dT%H:00:00', (os.time() + (3600*i)))
url = 'https://api.energidataservice.dk/datastore_search?resource_id=elspotprices&filters=%7B%22HourDK%22:%22' .. data .. '%22,%22PriceArea%22:%22DK1%22%7D'-- Here is the requst string :-)status = ssl.https.request(url) -- Here comes reply from serverdata = json.decode(status) -- Decode the Json fileprice = data.result.records[1].SpotPriceDKK / 1000-- Find price in DKK ører for bellow time time = data.result.records[1].HourDK-- Time of above price expected-- Extra cost from 17 to 20 hour at Konstant net (as we uses)if (os.date('%H' , (os.time() + (3600*i)))) == '17'or (os.date('%H' , (os.time() + (3600*i)))) == '18'or (os.date('%H' , (os.time() + (3600*i)))) == '19'thenprice = price + konstant_ekstra_tariff_17_20endpriser[i] = {klokken = tonumber(os.date('%H' , os.time() + (3600*i))),pris = price}-- store price and time for priceendlog(priser) -- Here are recorded data-- find when prices are lowest in the forcastlocalpos = 0localtester = 20fori=0, end_idoiftester > priser[i].pristhenpos = itester = priser[i].prisendendlog (priser[pos]) -- This is the most cheap hour
now there comes a new challanges, I want to find state of EV car, It is now posible for me to get the POST that makes me get the Bearer Authorization, but cannot get it out of the string?, and from there how do I make ep GET command with it?
--[[************************************************************************************************Created: 2022-02-10Get state of charge from car************************************************************************************************]]--require('socket.http')
functionencodepost(t)
localres = {}
localesc = require('socket.url').escapefork, vinpairs(t) dores[ #res + 1 ] = esc(k) .. '=' .. esc(v)
endreturntable.concat(res, '&')
endurl = 'https://api-eu.TRONITY.io/oauth/authentication'payload = encodepost({
client_id = "WRITE IT HERE",
client_secret = "WRITE IT HERE",
grant_type = "app"
})
res, err, headers, status = socket.http.request(url, payload)
--log(res, err, headers, status)iferr == 201then-- Get Bearer Authorizationlog(res) -- Here is the Bearer Authorization in how do I get the access_token out? And how do I do a json GET with that token?end
THIS IS MY res string, where I need to get access_token value/string out from:
10.02.2022, 16:40 (This post was last modified: 10.02.2022, 16:53 by Erwin van der Zwart.)
Code:
123456789101112131415
require('json')
iferr == 201then-- Get Bearer Authorization --log(res) -- Here is the Bearer Authorization in how do I get the access_token out? And how do I do a json GET with that token?data = json.pdecode(res)
tokentype = data.token_typeaccesstoken = data.access_tokenexpiresin = data.expires_inid = data.idlog(tokentype)
log(accesstoken)
log(expiresin)
log(id)
end
Here is an sample of integration i wrote with authentication token renewal that uses the expire stamp of the token, with this sample you should be able to create what you want to do