Not a real football fan myself but with the World Cup I make a little exception.
For that reason - and because I am always looking for data sources to play with the asynchronous http requests - I wrote this script.
The data are fetched from http://worldcup.sfg.io/ and they offer other data sets. I was only interested in the status of the current game.
changed: now using the /today endpoint. This way at the end of the game the winner is displayed
In the code below I check if one of the teams is a team I want notifications about. If you don't want to use that remove line 30 to 32 and 47 to 49
Code: Select all
return {
on = {
timer = { 'every 2 minutes' },
httpResponses = { 'scoreboard2' } -- matches callback string below
},
execute = function(domoticz, triggerItem)
local board = domoticz.devices('Scoreboard2') --a virtual text sensor
local prevboard = tostring(board.text) --get current value of text sensor
if (triggerItem.isTimer) then
domoticz.openURL({
url = 'http://worldcup.sfg.io/matches/today',
method = 'GET',
callback = 'scoreboard2'
})
elseif (triggerItem.isHTTPResponse) then
local response = triggerItem
if (response.ok and response.isJSON) then
tl = #response.json
tc = 1
repeat
local status = tostring(response.json[tc].status)
local home = tostring(response.json[tc].home_team.country)
local away = tostring(response.json[tc].away_team.country)
local hgoal = tostring(response.json[tc].home_team.goals)
local agoal = tostring(response.json[tc].away_team.goals)
if away == 'Spain' or away == 'Belgium' or home == 'Spain' or home == 'Belgium' then
sender = true
end
show = (home..' '..hgoal..' - '..agoal..' '..away)
if status == 'in progress' then tc = tl + 1 end
if status == 'completed' then
local score = (hgoal..' - '..agoal)
local winner = tostring(response.json[tc].winner)
show = ('Match won by '..winner..' with '..score)
tc = tc + 1
end
if status == 'future' then tc = tl + 1 end
until tc > tl
if show ~= prevboard then
print ('result '..show)
board.updateText(show)
if sender then
--put your notification method here (mine is telegram)
end
end
else
print('**scoreboard2 failed to fetch info')
end
end
end
}