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

You can force an automation to run. Or you can run the update script directly.

Unfortunately no luck. The sensor is still not showing up after 10:05.

I do now show this in the log

Logger: homeassistant.helpers.entity_component
Source: helpers/entity_component.py:48
First occurred: 10:05:00 AM (3 occurrences)
Last logged: 10:05:00 AM

Forced update failed. Entity sensor.cardinals_game_status not found.
Forced update failed. Entity sensor.cardinals_away_score not found.
Forced update failed. Entity sensor.cardinals_home_score not found.

I do appreciate your continued response.

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.