Script to display next F1 Race
Posted: 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.
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
}