I have extended the qualifying results to a top 6 . Max doesn't always qualify in the top 3 like today
Code: Select all
return {
on = {
timer = { 'every 20 minutes' },
httpResponses = { 'nextF1', 'qualyF1' } -- matches callback string below
},
data = {
nextF1 = { initial = nil }
},
execute = function(domoticz, triggerItem)
if situation == 'CET' then -- check for DST
offset = 1 -- offset for timezone Madrid Winter time
else offset = 2 -- offset for timezone Madrid Summer time, change these values to match your timezone
end
local board = domoticz.data.nextF1
local sensor = domoticz.devices('F1 Race')
local qboard = domoticz.devices('F1 Kwalificatie')
local prevQboard = qboard.text
if (triggerItem.isTimer) then
domoticz.openURL({
url = 'https://ergast.com/api/f1/current/next.json',
method = 'GET',
callback = 'nextF1'
})
elseif (triggerItem.isHTTPResponse) then
local response = triggerItem
if (response.ok and response.isJSON) and (triggerItem.callback == 'nextF1') then --when a valid json is received do this
local name = response.json.MRData.RaceTable.Races[1].raceName
local country = response.json.MRData.RaceTable.Races[1].Circuit.Location.country
local season = response.json.MRData.RaceTable.season
local round = response.json.MRData.RaceTable.round
local qualURL = ('https://ergast.com/api/f1/'..season..'/'..round..'/qualifying.json')
print('Find qualify at '..qualURL)
local fechapura = (response.json.MRData.RaceTable.Races[1].date) --get the date as provided(YYYY-MM-DD)
_,_,y,m,d = string.find(fechapura, "(%d+)%-(%d+)%-(%d+)")
local fecha = (d..'/'..m..'/'..y)
local horapura = (response.json.MRData.RaceTable.Races[1].time) --get time as provided (GMT without offset for DST)
_,_,h,mi = string.find(horapura, "(%d+):(%d+)")
local hora = ((h + offset)..':'..mi)
local race = (name..'\n'..fecha..'Starting at '..hora)
if board ~= race then --Only update text sensor if current info is different from the stored info
sensor.updateText(race)
domoticz.data.nextF1 = race --write the new info to global variable
end
--now get qualify
print('** Fetching info from qualify')
--local qualURL = 'https://ergast.com/api/f1/2018/7/qualifying.json'
domoticz.openURL({
url = qualURL,
method = 'GET',
callback = 'qualyF1'
})
elseif (response.ok and response.isJSON) and (triggerItem.callback == 'qualyF1') then
local Races = #response.json.MRData.RaceTable.Races
print('**Number of records for Qualify = '..Races)
if Races > 0 then
print('** Qualify results are availabale')
--get data for P1
local P1driver = response.json.MRData.RaceTable.Races[1].QualifyingResults[1].Driver.familyName
local P1Const = response.json.MRData.RaceTable.Races[1].QualifyingResults[1].Constructor.name
local P1Q1 = response.json.MRData.RaceTable.Races[1].QualifyingResults[1].Q1
_,_,m,s = string.find(P1Q1, "(%d+):(%d+.+)")
local P1Q1sec = ((tonumber(m) * 60) + tonumber(s))
local P1Best = P1Q1
local P1Q2 = response.json.MRData.RaceTable.Races[1].QualifyingResults[1].Q2
_,_,m,s = string.find(P1Q2, "(%d+):(%d+.+)")
local P1Q2sec = ((tonumber(m) * 60) + tonumber(s))
if P1Q2sec < P1Q1sec then P1Best = P1Q2 end
local P1Q3 = response.json.MRData.RaceTable.Races[1].QualifyingResults[1].Q3
_,_,m,s = string.find(P1Q3, "(%d+):(%d+.+)")
local P1Q3sec = ((tonumber(m) * 60) + tonumber(s))
if P1Q3sec < P1Q1sec and P1Q3sec < P1Q2sec then P1Best = P1Q3 end
print('P1Best = '..P1Best)
--now get data for P2
local P2driver = response.json.MRData.RaceTable.Races[1].QualifyingResults[2].Driver.familyName
local P2Const = response.json.MRData.RaceTable.Races[1].QualifyingResults[2].Constructor.name
local P2Q1 = response.json.MRData.RaceTable.Races[1].QualifyingResults[2].Q1
_,_,m,s = string.find(P2Q1, "(%d+):(%d+.+)")
local P2Q1sec = ((tonumber(m) * 60) + tonumber(s))
local P2Best = P2Q1
local P2Q2 = response.json.MRData.RaceTable.Races[1].QualifyingResults[2].Q2
_,_,m,s = string.find(P2Q2, "(%d+):(%d+.+)")
local P2Q2sec = ((tonumber(m) * 60) + tonumber(s))
if P2Q2sec < P2Q1sec then P2Best = P2Q2 end
local P2Q3 = response.json.MRData.RaceTable.Races[1].QualifyingResults[2].Q3
_,_,m,s = string.find(P2Q3, "(%d+):(%d+.+)")
local P2Q3sec = ((tonumber(m) * 60) + tonumber(s))
if P2Q3sec < P2Q1sec and P2Q3sec < P2Q2sec then P2Best = P2Q3 end
print('P2Best = '..P2Best)
-- get data for P3
local P3driver = response.json.MRData.RaceTable.Races[1].QualifyingResults[3].Driver.familyName
local P3Const = response.json.MRData.RaceTable.Races[1].QualifyingResults[3].Constructor.name
local P3Q1 = response.json.MRData.RaceTable.Races[1].QualifyingResults[3].Q1
_,_,m,s = string.find(P3Q1, "(%d+):(%d+.+)")
local P3Q1sec = ((tonumber(m) * 60) + tonumber(s))
local P3Best = P3Q1
local P3Q2 = response.json.MRData.RaceTable.Races[1].QualifyingResults[3].Q2
_,_,m,s = string.find(P3Q2, "(%d+):(%d+.+)")
local P3Q2sec = ((tonumber(m) * 60) + tonumber(s))
if P3Q2sec < P3Q1sec then P3Best = P3Q2 end
local P3Q3 = response.json.MRData.RaceTable.Races[1].QualifyingResults[3].Q3
_,_,m,s = string.find(P3Q3, "(%d+):(%d+.+)")
local P3Q3sec = ((tonumber(m) * 60) + tonumber(s))
if P3Q3sec < P3Q1sec and P3Q3sec < P3Q2sec then P3Best = P3Q3 end
print('P3Best = '..P3Best)
--now get data for P4
local P4driver = response.json.MRData.RaceTable.Races[1].QualifyingResults[4].Driver.familyName
local P4Const = response.json.MRData.RaceTable.Races[1].QualifyingResults[4].Constructor.name
local P4Q1 = response.json.MRData.RaceTable.Races[1].QualifyingResults[4].Q1
_,_,m,s = string.find(P4Q1, "(%d+):(%d+.+)")
local P4Q1sec = ((tonumber(m) * 60) + tonumber(s))
local P4Best = P4Q1
local P4Q2 = response.json.MRData.RaceTable.Races[1].QualifyingResults[4].Q2
_,_,m,s = string.find(P4Q2, "(%d+):(%d+.+)")
local P4Q2sec = ((tonumber(m) * 60) + tonumber(s))
if P4Q2sec < P4Q1sec then P4Best = P4Q2 end
local P4Q3 = response.json.MRData.RaceTable.Races[1].QualifyingResults[4].Q3
_,_,m,s = string.find(P4Q3, "(%d+):(%d+.+)")
local P4Q3sec = ((tonumber(m) * 60) + tonumber(s))
if P4Q3sec < P4Q1sec and P4Q3sec < P4Q2sec then P4Best = P4Q3 end
print('P4Best = '..P4Best)
--now get data for P5
local P5driver = response.json.MRData.RaceTable.Races[1].QualifyingResults[5].Driver.familyName
local P5Const = response.json.MRData.RaceTable.Races[1].QualifyingResults[5].Constructor.name
local P5Q1 = response.json.MRData.RaceTable.Races[1].QualifyingResults[5].Q1
_,_,m,s = string.find(P5Q1, "(%d+):(%d+.+)")
local P5Q1sec = ((tonumber(m) * 60) + tonumber(s))
local P5Best = P5Q1
local P5Q2 = response.json.MRData.RaceTable.Races[1].QualifyingResults[5].Q2
_,_,m,s = string.find(P5Q2, "(%d+):(%d+.+)")
local P5Q2sec = ((tonumber(m) * 60) + tonumber(s))
if P5Q2sec < P4Q1sec then P5Best = P4Q2 end
local P5Q3 = response.json.MRData.RaceTable.Races[1].QualifyingResults[5].Q3
_,_,m,s = string.find(P5Q3, "(%d+):(%d+.+)")
local P5Q3sec = ((tonumber(m) * 60) + tonumber(s))
if P5Q3sec < P5Q1sec and P5Q3sec < P5Q2sec then P5Best = P5Q3 end
print('P5Best = '..P5Best)
--now get data for P6
local P6driver = response.json.MRData.RaceTable.Races[1].QualifyingResults[6].Driver.familyName
local P6Const = response.json.MRData.RaceTable.Races[1].QualifyingResults[6].Constructor.name
local P6Q1 = response.json.MRData.RaceTable.Races[1].QualifyingResults[6].Q1
_,_,m,s = string.find(P6Q1, "(%d+):(%d+.+)")
local P6Q1sec = ((tonumber(m) * 60) + tonumber(s))
local P6Best = P4Q1
local P6Q2 = response.json.MRData.RaceTable.Races[1].QualifyingResults[6].Q2
_,_,m,s = string.find(P6Q2, "(%d+):(%d+.+)")
local P6Q2sec = ((tonumber(m) * 60) + tonumber(s))
if P6Q2sec < P6Q1sec then P6Best = P6Q2 end
local P6Q3 = response.json.MRData.RaceTable.Races[1].QualifyingResults[6].Q3
_,_,m,s = string.find(P6Q3, "(%d+):(%d+.+)")
local P6Q3sec = ((tonumber(m) * 60) + tonumber(s))
if P6Q3sec < P6Q1sec and P6Q3sec < P6Q2sec then P6Best = P6Q3 end
print('P6Best = '..P6Best)
local Qresults = ('1. '..P1driver..'('..P1Const..') - '..P1Best..'\n2. '..P2driver..'('..P2Const..') - '..P2Best..'\n3. '..P3driver..'('..P3Const..') - '..P3Best..'\n4. '..P4driver..'('..P4Const..') - '..P4Best..'\n5. '..P5driver..'('..P5Const..') - '..P5Best..'\n6. '..P6driver..'('..P6Const..') - '..P6Best)
local QresultsPrint = ('\n'..Qresults)
print(Qresults)
if Qresults ~= prevQboard then
qboard.updateText(Qresults)
end
--end
end