How to configure a dependent RESTful sensor?

I’m working on an integration with MLB using their RESTful API. In my configuration.yaml, I created a new sensor using one of the API endpoints to obtain my team’s schedule:

  - platform: rest
    resource: http://statsapi.mlb.com/api/v1/schedule?sportId=1&teamId=121
    name: mets_game_status
    scan_interval: 
      hours: 24
    value_template: '{{ value_json.dates[0].games[0].status.detailedState }}'
    json_attributes:
      - dates

This works great and I have a sensor named mets_game_status. One of the json attributes that this API returns is a Game ID (called gamePk) which is needed to then call a second API to get the score and other details of the game in progress.

Here’s what I have in my config file for that one:

  - platform: rest
    resource_template: http://statsapi.mlb.com/api/v1/game/{{ states.sensor.mets_game_status.attributes["dates"][0]["games"][0]["gamePk"] | int }}/linescore
    name: mets_game_linescore
    scan_interval: 30
#      hours: 24
    value_template: '{{ value_json.currentInning }}'
    json_attributes:
      - currentInning
      - currentInningOrdinal
      - inningState
      - inningHalf
      - isTopInning
      - innings
      - balls
      - strikes
      - outs

This second sensor called mets_game_linescore isn’t being created, and I’m assuming it’s because there is a dependency on getting the gamePk value from the first sensor. Ultimately I’m trying to use this for some light automations and in my dashboard.

Here’s the error I get in the log:

2022-04-04 23:11:47 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up rest platform for sensor
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 407, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1814, in _render_with_context
return template.render(**kwargs)
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 1291, in render
self.environment.handle_exception()
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 925, in handle_exception
raise rewrite_traceback_stack(source=source)
File "<template>", line 1, in top-level template code
File "/usr/local/lib/python3.9/site-packages/jinja2/sandbox.py", line 303, in getitem
return obj[argument]
jinja2.exceptions.UndefinedError: 'None' has no attribute 'attributes'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 249, in _async_setup_platform
await asyncio.shield(task)
File "/usr/src/homeassistant/homeassistant/components/rest/sensor.py", line 65, in async_setup_platform
rest = create_rest_data_from_config(hass, conf)
File "/usr/src/homeassistant/homeassistant/components/rest/__init__.py", line 168, in create_rest_data_from_config
resource = resource_template.async_render(parse_result=False)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 409, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: 'None' has no attribute 'attributes'

Any suggestions on how I can get the second sensor (mets_game_linescore) to maybe wait until the first sensor (mets_game_status) is available first? Or if I hard-coded an initial value for the gamePk value in the second sensor, is there a way to dynamically update it later on through an automation or script?

Ever figure this out?

No, I was never able to figure this out.

1 Like

If you reload REST after it is running I think it it created. I have the same issue now with some sensors.getting data for Pokémon. I am going to test reload after start.