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

Looks like it could be the same issue I’m having. Are you seeing anything in the logs about setting up the REST component?

This is the only reference to REST in my logs. And I’m not even sure what it is referring to.

No module named ‘homeassistant.components.rest.camera’

There are still a lot of these errors because I have not figured out the boolean yet:

2021-04-02 11:51:12 WARNING (MainThread) [homeassistant.components.automation] Error evaluating condition in ‘Cardinals - Game On - With Lights’:
In ‘condition’ (item 10 of 10):
In ‘state’:
In ‘state’ condition: unknown entity input_boolean.cardinals

It’s hard to tell what your issue is from the details provided. If it happens to be the same problem as mine, you can try the workaround here:

I’ve got this running, and on my dashboard (Go Sox!) but it doesn’t seem to update unless I manually run the “Update Red Sox game info” script or “Boston - In Progress” automation. I am seeing this error in my logs which started after adding the sensors. Any ideas?

Logger: homeassistant.config
Source: config.py:424
First occurred: 1:49:30 PM (2 occurrences)
Last logged: 1:49:31 PM

Invalid config for [automation]: must be a value between 0 and 59 for dictionary value @ data['seconds']. Got None. (See /config/configuration.yaml, line 19).

I’m wondering if it is because the Cardinals aren’t playing today, so it isn’t in the API, therefore won’t return anything.

I guess I’ll wait until tomorrow and see what happens.

That error seems pretty specific. Take a look at the trigger for your automation in yaml mode (according to that error, it should be on line 19), and make sure you have a valid number.

Alternatively, if you post your code, I can take a look.

That would do it. If there are no games on a given day, the sensors will have no value. In that case, an empty Lovelace card is expected.

Line 19 in configuration.yaml is “group: !include groups.yaml” so I don’t think that’s it (that file is currently empty)

The code I used is the one you used in your original post without any modifications at all.

Take a look at the triggers for the “pre-game” and “unavailable” automation. I think those are the only two in that file that have a “seconds” trigger. Maybe something was missed or dropped during copy / paste.

That might have done it. Those two weren’t loading correctly into the UI because they were /60 and it wants 1-59. I changed it to /59 and that loaded those two automations and appears to be working. I’ll know for sure with tomorrow’s game.

Makes sense. I think there was a change to that functionality in Home Assistant with one of the releases since I posted this.

Sorry for the confusion.

Ok, so the game started but it’s kept at 0-0 even though the score is now 2-1. I manually ran the automation “Boston - Game On” and it updated the score.

I am seeing this error in the logs:

Logger: homeassistant.components.automation
Source: components/automation/__init__.py:636
Integration: Automation (documentation, issues)
First occurred: 10:12:00 AM (80 occurrences)
Last logged: 11:31:00 AM

Error evaluating condition in 'Boston - Game On': In 'condition' (item 10 of 10): In 'state': In 'state' condition: unknown entity input_boolean.red_sox

I’ve created a helper that’s an on off toggle input to see if that helps.

Edit: That solved it. Working now and updating.

Thought, any way to grab the inning?

Glad to hear you got it working.

It doesn’t look like inning is returned via the API endpoint, but there is a “live feed” endpoint that may have it. You can find the URL for that within the message response you are already getting back from the endpoint you have.

I’m still fairly new to HA and usually the one asking questions, so feels good to hopefully maybe help out for once!

I got the inning to work, using the method BigBlackBear referenced. It’s just set to pull the game info of the current game and look up the half inning and inning # variables, and join them with a space so it reads as “Bottom 8”, for example.

- platform: rest
  name: atlanta_inning_status
  resource_template: >-
    {% if states('sensor.atlanta_game_status') == 'unknown' %}
      http://statsapi.mlb.com/api/v1/schedule?sportId=1&teamId=144
    {% else %}
      {{ "http://statsapi.mlb.com" ~ states.sensor.atlanta_game_status.attributes["dates"][0]["games"][0]["link"] }}
    {% endif %}
  value_template: >-
    {% if states('sensor.atlanta_game_status') == 'unknown' %}
      No_Game
    {% elif value_json.liveData.linescore.inningState == "End" %}
      {{ "End " ~ value_json.liveData.linescore.currentInning }}
    {% elif states('sensor.atlanta_game_status') == 'Game Over' %}
      Final
    {% else %}
      {{ value_json.liveData.linescore.inningHalf ~ " " ~ value_json.liveData.linescore.currentInning }}
    {% endif %}
  json_attributes:
      - liveData

I also created a sensor to pull in the game start time, if that may be of use. Note: I’ve calculated the time to show as Central time, so you may need to adjust the math for the value_template to match your time zone (CST was - 5).

atlanta_game_time:
      value_template: >-
        {% if states('sensor.atlanta_game_status') == 'unknown' %}
          No_Game
        {% else %}
          {% set time = states.sensor.atlanta_game_status.attributes["dates"][0]["games"][0]["gameDate"] %}
          {% if (time[11:13]|int - 5) > 12 %}
            {{ (time[11:13]|int - 5 - 12)|string ~ time[13:16] }}
          {% else %}
            {{ (time[11:13]|int - 5)|string ~ time[13:16] }}
          {% endif %}
        {% endif %}
4 Likes

I’m getting these errors since a couple of weeks :

Logger: homeassistant.components.template.sensor
Source: helpers/config_validation.py:739 
Integration: Template (documentation, issues) 
First occurred: 11:31:12 AM (2 occurrences) 
Last logged: 11:31:12 AM

The 'entity_id' option near /config/sensors/mlb2.yaml:3 is deprecated, please remove it from your configuration
The 'entity_id' option near /config/sensors/mlb2.yaml:23 is deprecated, please remove it from your configuration

Logger: homeassistant.helpers.template
Source: helpers/template.py:1366 
First occurred: 11:31:45 AM (4 occurrences) 
Last logged: 11:31:45 AM

Template variable warning: 'dict object' has no attribute 'score' when rendering '{% if states('sensor.toronto_game_status') == 'unknown' %} No_Game {% else %} {{ states.sensor.toronto_game_status.attributes["dates"][0]["games"][0]["teams"]["away"]["score"] }} {% endif %}'
Template variable warning: 'dict object' has no attribute 'score' when rendering '{% if states('sensor.toronto_game_status') == 'unknown' %} No_Game {% else %} {{ states.sensor.toronto_game_status.attributes.dates[0]["games"][0]["teams"]["home"]["score"] }} {% endif %}'

Anyone get this too? I’m too newbie to fix the code :confused:

I think I got doubleheaders working!
I don’t know anything about json so I don’t know if I’m using the correct terms, but all I ended up doing was just making new sensors and changed the index to games[1] instead of games[0] in the rest sensor.

Then I just created additional sensors for the 2nd game and changed games[0] to games[1] in the new sensors and added new automations/ triggers to just with another entity name for the second game double header. So now I have the scores from both games showing up.

I did this after last night’s Yankees doubleheader (btw, not sorry for sweeping the Red Sox :rofl: yesterday) so I couldn’t see it live, but I do have the scores from game 2 as well!

Any way someone can walk me through where to copy and paste this awesome api:( I have no programming background but am hungry to learn!

No programming background should be needed. I tried my best to spell out the steps in detail. The code for the REST sensors (Ubuntu Pastebin) should be pasted in your configuration.yaml file. The script and automations (Ubuntu Pastebin) should be pasted into your scripts.yaml and automations.yaml files respectively. And to show the details in your dashboard, you can paste the code from my original post into a new manual card.

I hope that gives you what you were looking for.

@BigBlackBear well done, this is awesome!

@stx21 thanks for the inning status!

Does anyone have a pretty lovelace card they could share?

The code I use for my lovelace card is in the original post, but I wouldn’t necessarily call it “pretty”. Beyond that, someone has put together a nice one here:

The list of venues is complete, though. Once we solve that problem and I find time to incorporate some other changes I’ve been meaning to, I’ll make a new post with the latest and greatest.