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.

External eletric pricing on API
#4
Hello there

Thax alot, now that works :-)
Code:
--[[************************************************************************************************
Created: 2022-02-09
Get 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'
local data = ' '
local url = ' '
local priser = {klokken, pris}
local end_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 hours
if tonumber(os.date('%H', os.time())) <= 12 then
  end_i = 23 - tonumber(os.date('%H', os.time()))
else
  end_i = (23 - tonumber(os.date('%H', os.time()))) + 24
end

-- Get prices for the next hours, defined in "end_i"
-- Prices are from Norpol energy trader
-- They are from west region Denmark
for i=0, end_i do
  data = 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 server
  data = json.decode(status) -- Decode the Json file
    price = 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' then
    price = price + konstant_ekstra_tariff_17_20
  end

    priser[i] = {klokken = tonumber(os.date('%H' , os.time() + (3600*i))),pris = price}-- store price and time for price
end
log(priser) -- Here are recorded data

-- find when prices are lowest in the forcast
local pos = 0
local tester = 20
for i=0, end_i do
  if tester > priser[i].pris then
    pos = i
    tester = priser[i].pris
  end 
end
log (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?

Code:
--[[************************************************************************************************
Created: 2022-02-10
Get state of charge from car
************************************************************************************************]]--
require('socket.http')
function encodepost(t)
  local res = {}
  local esc = require('socket.url').escape

  for k, v in pairs(t) do
    res[ #res + 1 ] = esc(k) .. '=' .. esc(v)
  end

  return table.concat(res, '&')
end

url = '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)

if err == 201 then -- 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?
end

THIS IS MY res string, where I need to get access_token value/string out from:

string: {"id":"cfed0e4d6351407dae165944745d48f","access_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJjZmVkMGU0ZC02MzUxLTQwN2QtYWUxNi0yNTk0NDc0NWQ0OGYiLCJhcHBJZCI6Ijg3NzhjMTM4LTQ0ODgtNGU2Ni1iMmZhLTY3NGUyYmQ2NGYyMiIsInZlaGljbGVJZCI6bnVsbCwic2NvcGVzIjpbInJlYWRfYmF0dGVyeSIsInJlYWRfY2FyZHMiLCJyZWFkX2NoYXJnZXBvaW50cyIsInJlYWRfaWRsZXMiLCJyZWFkX2xvY2F0aW9uIiwicmVhZF9vZG9tZXRlciIsInJlYWeHAiOjE2NDQ1OTU0ODQsImF1ZCI6Imh0dHBzOi8vcGxhdGZvcm0udHJvbml0eS5pbyIsImlzcyI6InBsYXRmb3JtLnRyb25pdHkuaW8ifQ.AWboomF9whW6udiSzWsWyWvlhu9ZpmdEN9IKrbFOHIM","expires_in":3600,"token_type":"bearer"}

And this is my URL I need to GET with above bearer Authorization

url = 'https://api-eu.TRONITY.io/v1/vehicles/{CAR ID HERE}/bulk'
Reply


Messages In This Thread
External eletric pricing on API - by Tue - 09.02.2022, 09:20
RE: External eletric pricing on API - by Tue - 09.02.2022, 12:30
RE: External eletric pricing on API - by Tue - 10.02.2022, 16:29
RE: External eletric pricing on API - by Tue - 11.02.2022, 15:01

Forum Jump: