Script to display next F1 Race

elmortero
Posts: 233
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 1041 times

SweetPants
Posts: 2250
Joined: Friday 12 July 2013 21:24
Target OS: Linux
Domoticz version: V4.9999
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 (from source)

Thelion
Posts: 46
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: 233
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 914 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: 233
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

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

Re: Script to display next F1 Race

Post by SweetPants » Sunday 09 September 2018 11:20

Between 03:00 and 04:00, the script stopped working. The URL returns "Unable to select database"
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 (from source)

User avatar
Derik
Posts: 2150
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Script to display next F1 Race

Post by Derik » Sunday 09 September 2018 12:32

Great scripts ....
Only is see 3 scripts?
En 2 dummy?

Please some smal wiki?
Need 2 dummy only where do i need to set the name of this Dummy in the script??
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups

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

Re: Script to display next F1 Race

Post by SweetPants » Monday 10 September 2018 14:16

SweetPants wrote:
Sunday 09 September 2018 11:20
Between 03:00 and 04:00, the script stopped working. The URL returns "Unable to select database"
Back on-line again :mrgreen: :mrgreen: :mrgreen:
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 (from source)

User avatar
Derik
Posts: 2150
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Script to display next F1 Race

Post by Derik » Monday 10 September 2018 21:03

works here to!!!!


Perhaps possible to show place 4 and 5 also??
Perhaps we can see MAX :-)
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups

elmortero
Posts: 233
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 » Tuesday 11 September 2018 0:03

Derik wrote:
Sunday 09 September 2018 12:32
Great scripts ....
Only is see 3 scripts?
En 2 dummy?

Please some smal wiki?
Need 2 dummy only where do i need to set the name of this Dummy in the script??
Name of the virtual Text sensor is set in
"local sensor = domoticz.devices('F1')" for the first script
In the second on (F1-top3) : local board = domoticz.devices('F1_Top3') --a virtual text sensor
For the 3rd one (qualify) : local qboard = domoticz.devices('F1_Qualy3')

elmortero
Posts: 233
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 » Tuesday 11 September 2018 0:13

Derik wrote:
Monday 10 September 2018 21:03
Perhaps possible to show place 4 and 5 also??
Perhaps we can see MAX :-)
Improved version :-)

Code: Select all

return {
	on = {
		timer = { 'at *:22', '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 = 5
			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
					if pInfo ~= prevboard then
						board.updateText(pInfo)
					end			
			else
					print('** F1-Top3 failed to fetch info')
		end
		end
	end
}
In this example the 5 first in the ranking are fetched: Line 25 (tl = 5) change the number to match your needs.

User avatar
Brutus
Posts: 322
Joined: Friday 26 September 2014 9:33
Target OS: Windows
Domoticz version:
Location: Heerlen, The Netherlands
Contact:

Re: Script to display next F1 Race

Post by Brutus » Tuesday 11 September 2018 10:31

Thnx for these scripts. This is the first time I use dzvents.

When I copied the first script and changed the time a couple of minutes forward to fors an update, the Virtual Text Sensor "F1" does not receive an update. 12 hours later still no update. What could be wrong?

I use the latest Stable Domoticz version on a Windows computer.

Thnx for the help.
1x Intel NUC D54250WYK (Windows 7 x64)
1x Aeon Labs USB Z-Stick S2
1x P1 Smart Meter USB
19x Fibaro Modules

elmortero
Posts: 233
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 » Tuesday 11 September 2018 13:25

Brutus wrote:
Tuesday 11 September 2018 10:31
When I copied the first script and changed the time a couple of minutes forward to fors an update, the Virtual Text Sensor "F1" does not receive an update. 12 hours later still no update. What could be wrong?
Hi, In order to find that out, please check your Domoticz log for errors and comment them here.

User avatar
Brutus
Posts: 322
Joined: Friday 26 September 2014 9:33
Target OS: Windows
Domoticz version:
Location: Heerlen, The Netherlands
Contact:

Re: Script to display next F1 Race

Post by Brutus » Tuesday 11 September 2018 14:00

elmortero wrote:
Tuesday 11 September 2018 13:25
Brutus wrote:
Tuesday 11 September 2018 10:31
When I copied the first script and changed the time a couple of minutes forward to fors an update, the Virtual Text Sensor "F1" does not receive an update. 12 hours later still no update. What could be wrong?
Hi, In order to find that out, please check your Domoticz log for errors and comment them here.
Noting happens even in de Log file when the time is met.

EDIT: Within the options Of domoticz you can turn dzVents On of Off. I had it turned off :)
1x Intel NUC D54250WYK (Windows 7 x64)
1x Aeon Labs USB Z-Stick S2
1x P1 Smart Meter USB
19x Fibaro Modules

melgi
Posts: 13
Joined: Tuesday 15 August 2017 15:50
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Script to display next F1 Race

Post by melgi » Wednesday 12 September 2018 13:28

Thanx for the clear explanation! I thought I'd try this and created my first virtual sensors and creating my first dzVents events using your code worked like a charm!

User avatar
Brutus
Posts: 322
Joined: Friday 26 September 2014 9:33
Target OS: Windows
Domoticz version:
Location: Heerlen, The Netherlands
Contact:

Re: Script to display next F1 Race

Post by Brutus » Saturday 15 September 2018 17:03

I don't understand the logic when these script run:

First script runs ones a day for checking the upcomming Race. Thats good

Code: Select all

return {
	on = {
		timer = { 'at 10:00' },
		httpResponses = { 'nextF1' } -- matches callback string below
The second checks the qualifying every 20 min every day while the qualifying only is at Saturday?

Code: Select all

return {
	on = {
		timer = { 'every 20 minutes' },
		httpResponses = { 'nextF1', 'qualyF1' } -- matches callback string below


The third script checks the Driver standings, every 15 minutes on saterday, sunday or monday. At Saturday there can't be any new driver standings because the race is at Sunday.

Code: Select all

return {
	on = {
		timer = { 'at *:22', 'every 15 minutes on sat, sun, mon' },
		httpResponses = { 'F1-Top3' },
		devices = {'F1 Race'}
Last thing when will the site https://ergast.com/api/f1/2018/15/qualifying update? The qualifying is already done for a couple of hours and still no data...

EDIT: Qualifying data is now online after about 2 hours. I think this is normal?
1x Intel NUC D54250WYK (Windows 7 x64)
1x Aeon Labs USB Z-Stick S2
1x P1 Smart Meter USB
19x Fibaro Modules

elmortero
Posts: 233
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 15 September 2018 17:30

Brutus wrote:
Saturday 15 September 2018 17:03
I don't understand the logic when these script run:

The second checks the qualifying every 20 min every day while the qualifying only is at Saturday?

The third script checks the Driver standings, every 15 minutes on saterday, sunday or monday. At Saturday there can't be any new driver standings because the race is at Sunday.

Last thing when will the site https://ergast.com/api/f1/2018/15/qualifying update? The qualifying is already done for a couple of hours and still no data...

EDIT: Qualifying data is now online after about 2 hours. I think this is normal?
Hi,
You can of course change all the timers to your taste
About the third one: most likely I was doing tests on a Saturday while writing the script and did not want to wait until Sunday (so got the results from the previous results)

As for the update of the results (of qualify and race), that depends on the maintainer of Ergast. I believe I read somewhere that he tries to make sure to update within 4 hours.
If you want real-time results - and are willing to pay - you could subscribe to the F1.com API and adapt these scripts to get their data .

melgi
Posts: 13
Joined: Tuesday 15 August 2017 15:50
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Script to display next F1 Race

Post by melgi » Saturday 15 September 2018 17:31

Today's qualifying result don't show to me neither.

Verstuurd vanaf mijn SM-G955F met Tapatalk


User avatar
Brutus
Posts: 322
Joined: Friday 26 September 2014 9:33
Target OS: Windows
Domoticz version:
Location: Heerlen, The Netherlands
Contact:

Re: Script to display next F1 Race

Post by Brutus » Saturday 15 September 2018 17:42

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
1x Intel NUC D54250WYK (Windows 7 x64)
1x Aeon Labs USB Z-Stick S2
1x P1 Smart Meter USB
19x Fibaro Modules

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest