How to get your favourite MLB team's scores in Home Assistant

So, I recently took to Twitter to share the latest re-design of my Lovelace dashboard:

Among the responses I received, there was a significant amount of interest in finding out how I was able to pull in and display the live MLB scores, so I agreed to write up a little tutorial. Hopefully this is something that a few of you out there can benefit from.

Before I begin, though, I should acknowledge the real brains behind this solution. When I first got the RGB bulbs for my basement, I was looking to build the smart home equivalent to the Budweiser goal light and stumbled upon the following reddit post:


So, full credit goes to the the the fine folks in that thread. I merely copied what they did and extended / personalized the solution for my own purposes. That said, I’m happy to pass along the details of what I’ve done.

At it’s heart, this solution relies on a series of REST sensors that call a public API at predefined intervals. Here is the code for them:
https://paste.ubuntu.com/p/SrHNfHzMS8/
.
As you can see from the code, I’m a Red Sox fan. If you are not, that’s okay… Here are the steps to adapt this solution to your favourite team:

  1. Visit the following URL and find the three-digit ID for your team of choice:
    http://statsapi.mlb.com/api/v1/schedule?sportId=1

  2. Replace the teamID at the end of line 3.

  3. Rename the sensors to your liking by finding and replacing any references to “boston” or “red sox” in my code to the equivalents for your team.

With the sensors in place, we now need to make sure they are kept up-to-date. At the same time, it’s also fun to kick off a little celebration when your team scores a run. Here is the script and automations that I use:
https://paste.ubuntu.com/p/WkCYQYdpBH/

Again, simply find and replace any references to my team with your own. Also, a couple of my automations rely on an input boolean that acts as a flag to have the scores update more frequently when I’m actually watching the game. If you copy my code directly, you’ll want to create one of your own in your configuration.

Lastly, here is the code I use to display the scores on my dashboard:

  columns: 3
  show_name: false 
  type: glance
filter:
  exclude:
    - state: No_Game
    - state: unknown
  include:
    - entity_id: sensor.boston_home_score
    - entity_id: sensor.boston_away_score
    - entity_id: sensor.boston_game_status
show_empty: false
type: 'custom:auto-entities'

My full implementation actually tracks a couple of teams, so I use the auto-entities card to ensure that only teams that are playing on a given day show up on the dashboard.

So, that’s it. But before I go, I should mention that I haven’t figured out a good way to handle double-headers. With this solution, only the first game of a double-header will update, while the second one will be ignored. If any of you find an elegant way to address this, please do let me know.

7 Likes

This is a really cool project. I’ve implemented it today without any issues!

1 Like

Awesome! That’s great to hear!

Man, thank you so much for posting this. I have looking for a solution forever. I figured it was a closed api, well not free at least.Cant wait to set this up and piss of the family with the flashing lights.

No problem. I hope you enjoy.

I managed to not “piss off” anyone by making sure I also built in tracking and celebrations for my wife’s team too.

What timezone are you in? Im guessing est? I am but it doesnt seem to update at 10am. I call the service and it works so im thinking its just too early?

Also there must be a way to use an attribute to deal with double headers. I see there is a split squad option and a night and day. I wonder if somehow that can be used.

Lastly. In lovelace above status I have the “eye” icon, how can I get rid of that? I tried in the glance card but it would just remove it all and I dont see an option to remove the icon in customize.

What timezone are you in? Im guessing est? I am but it doesnt seem to update at 10am. I call the service and it works so im thinking its just too early?

I’m in the Atlantic time zone, but the code I posted was for the Eastern time zone. I’m pretty sure it updates every day at 10 AM Eastern, and I check 5 minutes after that. Realistically, though, all the automations call the same script, so they do essentially the same thing. If you call it later in the day, you’ll still get the schedule (or the current status of the game). If you were to call it too early, you should just get the results from the day before.

Also there must be a way to use an attribute to deal with double headers. I see there is a split squad option and a night and day. I wonder if somehow that can be used.

There is a "doubleHeader" attribute that tells whether or not the game is a double header - this is not the issue. The issue is that the attributes for the second game are buried in a second message body, further down. So, I suspect a second set of template sensors would be required or, perhaps, a conditional in the existing sensor templates. Again, if you figure out how to extract this, I would be really interested in hearing the solution.

Lastly. In lovelace above status I have the “eye” icon, how can I get rid of that? I tried in the glance card but it would just remove it all and I dont see an option to remove the icon in customize.

You can just set it to null with the following:
icon: ' '

Is that in the card or customize? Im using pure yaml for lovelace.

It’s not in the card. I don’t remember whether I did it via the Customise UI or YAML, but either should work.

1 Like

Sorry to bring up an old post, does this still work for you? seems like maybe the API got locked down?

I follow the Red Sox and Blue Jays. Sadly both trends have been out of contention for a while :sob:, but it worked fine right up until the end.

I’m not seeing any data returned right now, but maybe that’s because it’s just the World Series left and it’s not scheduled yet (??).

Seems like it’s working again… Are you still having issues?

Working now! Thank you!!

Thanks OP, it’s very good!

I created automations and if I’m not mistaking, the only one that gets turn on and off is the game on one.

Shouldn’t the other automation be turned on and off too to prevent useless time pattern verification every 60 seconds?

I must be doing something wrong cause my Pre-Game automation is always running…?

The only one that should get turned on is the “game on” automation. That is intentional. All the other automations have conditions that make sure they only trigger when the game in question is in a particular status. For the “game on” automation, however, there are a number of statuses that a game can switch to before it’s truly “over”. For that reason, the “game on” automation stays off until the game is “In Progress”, then it stays on until the game is over. This helps make sure that any status changes during the game don’t prevent it from updating.

I hope that clears things up.

I copied your REST sensor config exactly, yet nothing shows up when I create the auto-entities card. It doesn’t error out, it’s just blank. I have restarted the server, and the sensors still don’t show up in my entities configuration. The automations all show up, but not the sensors.

Any help would be greatly appreciated!

Are you seeing any errors in your logs? I know I’ve been having problems with the REST component for some time and have had to resort to a workaround to keep my sensors working.

The only error I am seeing is the input_boolean error because I haven’t bothered setting any of that up yet. I was hoping to get the dashboard showing game details setup first, then work on the automations to match my system.

So, your sensors aren’t showing up, but you’re not getting any errors either?

Are you just looking for the sensors in Lovelace? Or have you checked in Developer Tools?

I am looking in Configuration/Entities. But I also just realized that it might be the automation hasn’t run yet since it is not 10:05:00 (I’m in CDT). I’ll give it another hour and check back to see if the sensors show up.