Script to display next F1 Race

User avatar
Brutus
Posts: 326
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 18:08

elmortero wrote:
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 .
Dzvents is new for me so I looked at the following wiki page for timer options. Damm... planty of options there!

https://www.domoticz.com/wiki/DzVents:_ ... gger_rules
1x Intel NUC D54250WYK (Windows 7 x64)
1x Aeon Labs USB Z-Stick S2
1x P1 Smart Meter USB
19x Fibaro Modules

melgi
Posts: 18
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 18:34

Brutus wrote: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
Brutus, I'd like to use your script. I have a virtual sensor called "F1" and two text switches called "F1 Race" and "F1 Quali". What do I need to change in your script to have it work for me too?

Verstuurd vanaf mijn SM-G955F met Tapatalk


User avatar
Brutus
Posts: 326
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 18:40

@melgi

I don't know why u need a Virtual Sensor called "F1"?

But for now you need the change this:

Code: Select all

	local sensor =  domoticz.devices('F1 Race')
	local qboard =  domoticz.devices('F1 Kwalificatie')
To this:

Code: Select all

	local sensor =  domoticz.devices('F1 Race')
	local qboard =  domoticz.devices('F1 Quali')
1x Intel NUC D54250WYK (Windows 7 x64)
1x Aeon Labs USB Z-Stick S2
1x P1 Smart Meter USB
19x Fibaro Modules

melgi
Posts: 18
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 18:59

Brutus, thanx for your reply. Will try that. In my post I made a mistake. The "F1" is actually the Dummy I created in the Hardware section. On this hardware I created the said virtual text sensors.

Verstuurd vanaf mijn SM-G955F met Tapatalk


Furiousz23
Posts: 29
Joined: Saturday 30 December 2017 21:01
Target OS: Raspberry Pi
Domoticz version: 4.10199
Location: Nederland
Contact:

Re: Script to display next F1 Race

Post by Furiousz23 » Saturday 15 September 2018 20:05

Great script! Now i don`t have to open the Ziggo app anymore to check when the race is starting :mrgreen: . Now i have to figure out how to get a Telegram notification through Domoticz one hour before the start of the race.

User avatar
Brutus
Posts: 326
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 » Sunday 16 September 2018 12:01

Furiousz23 wrote:
Saturday 15 September 2018 20:05
Great script! Now i don`t have to open the Ziggo app anymore to check when the race is starting :mrgreen: . Now i have to figure out how to get a Telegram notification through Domoticz one hour before the start of the race.
That would be cool indeed.
1x Intel NUC D54250WYK (Windows 7 x64)
1x Aeon Labs USB Z-Stick S2
1x P1 Smart Meter USB
19x Fibaro Modules

melgi
Posts: 18
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 » Monday 17 September 2018 21:11

I still can't get this to work :-(.

THIS SCRIPT DOESN'T WORK FOR ME:

return {
on = {
timer = { 'every 2 minutes on sat, sun, mon, tu, wed, thu, fri' }
--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_next_race') -- name of my virtual text sensor
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
}


THIS SCRIPT WORKS FOR ME:

return {
on = {
timer = { 'every 2 minutes on sat, sun, mon, tu, wed, thu, fri' }, --{ 'at *:22', 'every 15 minutes on sat, sun, mon, tu, wed, thu, fri' },
httpResponses = { 'F1-Top3' },
devices = {'F1'}
},

execute = function(domoticz, triggerItem)
local board = domoticz.devices('F1 race') --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
}

What am I doing wrong? I'm just starting to use dzVents and trying really hard to understand this. Sadly I can't see any log from running this script. Maybe it contains the errors that show up while running this script. If only I could read/see them, I could try to figure out what is going wrong here.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest