Template for retrieving JSON array values

I am trying to pull nhl data into home assistant using their api and REST sensor. I am able to pull the data but do not know how to assign the values to different template sensors.

I have a sensor setup to just grab the away team name.

nhl_away_team:
    value_template: '{{ states.sensor.nhl_game.attributes["dates"]["games"]["teams"]["away"]["team"]["name"] }}'

The JSON comes into the Rest Sensor nhl_game like this:

"dates" : [ {
    "date" : "2019-12-01",
    "totalItems" : 1,
    "totalEvents" : 0,
    "totalGames" : 1,
    "totalMatches" : 0,
    "games" : [ {
      "gamePk" : 2019020416,
      "link" : "/api/v1/game/2019020416/feed/live",
      "gameType" : "R",
      "season" : "20192020",
      "gameDate" : "2019-12-01T20:00:00Z",
      "status" : {
        "abstractGameState" : "Final",
        "codedGameState" : "7",
        "detailedState" : "Final",
        "statusCode" : "7",
        "startTimeTBD" : false
      },
      "teams" : {
        "away" : {
          "leagueRecord" : {
            "wins" : 15,
            "losses" : 10,
            "ot" : 3,
            "type" : "league"
          },
          "score" : 2,
          "team" : {
            "id" : 25,
            "name" : "Dallas Stars",
            "link" : "/api/v1/teams/25"
          }
        },
        "home" : {
          "leagueRecord" : {
            "wins" : 12,
            "losses" : 11,
            "ot" : 4,
            "type" : "league"
          },
          "score" : 3,
          "team" : {
            "id" : 30,
            "name" : "Minnesota Wild",
            "link" : "/api/v1/teams/30"
          }
        }
      },

I think that dates, and games are actually arrays of different objects but I do not know how to show this in value templating and could not find examples. My knowledge of JSON is pretty limited. Is this possible with templating?

can you post all of your configuration? I want to verify that you actually have a json object as your dates attribute.

If it is a json object then the correct template would be:

{% set date = 0 %} # first date, 0 = 1st, 1 = 2nd, 2 = 3rd
{% set game = 0 %} # first game, 0 = 1st, 1 = 2nd, 2 = 3rd
{{ states.sensor.nhl_game.attributes.dates[date].games[game].teams.away.team.name }}

or

{% set date = 0 %} # first date, 0 = 1st, 1 = 2nd, 2 = 3rd
{% set game = 0 %} # first game, 0 = 1st, 1 = 2nd, 2 = 3rd
{{ states.sensor.nhl_game.attributes['dates'][date]['games'][game]['teams']['away']['team']['name'] }}

or without the variables

{{ states.sensor.nhl_game.attributes['dates'][0]['games'][0]['teams']['away']['team']['name'] }}

Thank you!

This works. I just did not know how to show that dates and games were arrays.

Here is my REST sensor code:

  - platform: rest
    name: nhl_game
    resource: https://statsapi.web.nhl.com/api/v1/schedule?teamId=25
    json_attributes:
      - dates
    value_template: 'OK'

Is it possible to change the scan_interval depending on a value? It would not let me use templates on scan_interval.

    scan_interval: >- 
      {% if states.sensor.nhl_game.attributes["teams"][0]["nextGameSchedule"]["dates"][0]["games"][0]["status"]["abstractGameState"].state == 'LIVE' %}
        5
      {% else %}
        7200
      {% endif %}

Nope, only fields called out in the documents as ‘templates’ can be templated. The bulk majority of fields in configurations cannot be templated.