Script to display next F1 Race

Post Reply
elmortero
Posts: 201
Joined: Sunday 29 November 2015 21:46
Target OS: Raspberry Pi
Domoticz version: 3.9639
Location: Spain
Contact:

Script to display next F1 Race

Post by elmortero » Sunday 17 June 2018 11:56

This script gets the next F1 race from https://ergast.com/mrd/ and writes it to a text sensor.
Ergast offers more info on F1 - I need a race in progress to see if it provides live position information.

Code: Select all

return {
	on = {
		timer = { 'at 10:00' },
		httpResponses = { 'nextF1' } -- 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')
		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) then			--when a valid json is received do this
				 local name = response.json.MRData.RaceTable.Races[1].raceName
				 local fechapura = (response.json.MRData.RaceTable.Races[1].date)   --get the date as provided(YYYY-MM-DD)
				    --Next line converts it to DD-MM-YYYY, you can delete it if you are OK with provided date format
				  local YY = string.sub(fechapura, 1,4); local MM = string.sub(fechapura, 6,7); local DD = string.sub(fechapura, 9,10); local fecha = (DD..'-'..MM..'-'..YY)
				 local horapura = (response.json.MRData.RaceTable.Races[1].time) --get time as provided (GMT without offset for DST)
				  --convert provided time to time in your timezone 
				  local HH = (string.sub(horapura, 1,2) + offset); local MM = string.sub(horapura, 4,5); local hora = (HH..':'..MM)
				 local race = (name..'\n'..fecha..' 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
			
				else
						print('**nextF1 failed to fetch info')
				end													--end of actions after valid json received
		end
	end
}
Attachments
f1.JPG
f1.JPG (15.29 KiB) Viewed 451 times

SweetPants
Posts: 2101
Joined: Friday 12 July 2013 21:24
Target OS: Linux
Domoticz version: V4.9701
Location: The Netherlands
Contact:

Re: Script to display next F1 Race

Post by SweetPants » Tuesday 19 June 2018 10:33

Cool, i like this one as a F1 fan. Will give it a try this evening
Edit: Already implemented, works great. Many thanks for it
Ubuntu 16.04.3 LTS, Intel NUC, MySensors 2.3.0-alpha, Pro Mini 3.3/8M RFM69(HW), ESP8266 (SDK2.2.0), Sonoff, RFLink, RFXcom, MQTT, NodeRed, Domoticz Beta (source)

Thelion
Posts: 38
Joined: Saturday 08 October 2016 12:15
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Script to display next F1 Race

Post by Thelion » Thursday 21 June 2018 11:53

Nice.

Works like a charm. 8-)

Maybe implement free practice and qualifying sessions too.

And it sure would be nice to list the top 3 drivers during the GP itself until a day later or so.

elmortero
Posts: 201
Joined: Sunday 29 November 2015 21:46
Target OS: Raspberry Pi
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Script to display next F1 Race

Post by elmortero » Thursday 21 June 2018 12:17

Thelion wrote:
Thursday 21 June 2018 11:53
Nice.

Works like a charm. 8-)

Maybe implement free practice and qualifying sessions too.

And it sure would be nice to list the top 3 drivers during the GP itself until a day later or so.
If you mean the season ranking Top 3:
T3.JPG
T3.JPG (16.66 KiB) Viewed 324 times

Code: Select all

return {
	on = {
		timer = { 'at 0:58', 'every 15 minutes on sat, sun, mon' },
		httpResponses = { 'F1-Top3' },
		devices = {'F1'}
	},
	
	execute = function(domoticz, triggerItem)
    local board = domoticz.devices('F1_Top3')    --a virtual text sensor
    local prevboard = tostring(board.text)          --get current value of text sensor
	
		if (triggerItem.isTimer) then
			domoticz.openURL({
				url = 'https://ergast.com/api/f1/current/driverStandings.json',
				method = 'GET',
				callback = 'F1-Top3'
			})
			
		elseif (triggerItem.isHTTPResponse) then

	local response = triggerItem
			if (response.ok and response.isJSON) then
			local pInfo = ''
			local bInfo = ''
			tl = 3
			tc = 1
			repeat
				local code = tostring(response.json.MRData.StandingsTable.StandingsLists[1].DriverStandings[tc].Driver.code)
				local points = tostring(response.json.MRData.StandingsTable.StandingsLists[1].DriverStandings[tc].points) 
				local const = tostring(response.json.MRData.StandingsTable.StandingsLists[1].DriverStandings[tc].Constructors[1].name)
				local pos = tostring(response.json.MRData.StandingsTable.StandingsLists[1].DriverStandings[tc].position)
				 if string.len(points) < 3 then points = (' '..points) end
				local Full = (pos..'. '..code..'   '..points..'   '..const..'\n')
				tc = tc + 1
				pInfo = (pInfo..Full)
			until tc == tl + 1
				print(pInfo)
					if bInfo ~= prevboard then

						board.updateText(pInfo)
					end			
			else
					print('** F1-Top3 failed to fetch info')
		end
		end
	end
}
For the Qualify, I don't think Ergast provides that info. Have a look for yourself at https://ergast.com/mrd/ maybe you find something useful there.

elmortero
Posts: 201
Joined: Sunday 29 November 2015 21:46
Target OS: Raspberry Pi
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Script to display next F1 Race

Post by elmortero » Saturday 23 June 2018 14:56

Thelion wrote:
Thursday 21 June 2018 11:53
Maybe implement free practice and qualifying sessions too.
Just in time for the qualify of today (starts in about 25 minutes). Gets from the next race event the round and gets the link for the qualify of that event.
Shows the Top3 of the qualify and updates text sensor 'F1_Quali3'

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')
	local qboard =  domoticz.devices('F1_Qualy3')
	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)						
					local Qresults = ('1. '..P1driver..'('..P1Const..') - '..P1Best..'\n2. '..P2driver..'('..P2Const..') - '..P2Best..'\n3. '..P3driver..'('..P3Const..') - '..P3Best)
					local QresultsPrint = ('\n'..Qresults)
					print(Qresults)		

    				if Qresults ~= prevQboard then
    				 qboard.updateText(Qresults)
    				 end
				else
				     qboard.updateText('Qualify not finished yet')
				--end
				end

					
				else
						print('**nextF1 failed to fetch info')
				end													--end of actions after valid json received
		end
	end
}
I am sure it is not the cleanest code - I could loop through the results - but as a rough draft should be ok

User avatar
Phantom
Posts: 82
Joined: Saturday 31 December 2016 15:47
Target OS: Raspberry Pi
Domoticz version: 3.8922
Location: The Netherlands
Contact:

Re: Script to display next F1 Race

Post by Phantom » Wednesday 27 June 2018 23:18

Thanks, those scripts are great :D

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest