REST sensor template help

I’ve been trying for a while, but I’m not very good with jinja2 templates, and I’m not sure how to pull the data I want from this json.

edited for brevity

{
	"events": [{
		"name": "Green Bay Packers at New Orleans Saints",
		"shortName": "GB @ NO",
		"competitions": [{
			"competitors": [{
					"homeAway": "home",
					"team": {
						"name": "Saints",
						"abbreviation": "NO",
						"displayName": "New Orleans Saints"
					},
					"score": "0"
				},
				{
					"homeAway": "away",
					"team": {
						"name": "Packers",
						"abbreviation": "GB",
						"displayName": "Green Bay Packers"
					},
					"score": "0"
				}
			],
			"status": {
				"clock": 0.0,
				"displayClock": "0:00",
				"period": 0,
				"type": {
					"state": "pre",
					"completed": false
				}
			}
		}]
	}]
}

I’d like to have a sensor for GB score, and I’m having some trouble in a couple of areas. The first issue is just matching abbreviation == GB. In the dev tools template editor, I’m trying the following

{% for competitions in value_json.events %}
{% if  competitors.team.abbreviation == 'GB' %}
"It's GB"
{% endif %}
{% endfor %}

But that results in an error UndefinedError: 'competitors' is undefined

Secondly, even if I was able to match the team abbreviation, I can’t think of a good way to pull the score value since it resides outside of the team tag.

For reference, this is using the ESPN NFL API. I used to use the NFL API with the following template, and I’m trying to recreate it using the ESPN API.

      {% for game in value_json.gms %}
        {% if game.h == 'GB' or game.v == 'GB' %}
          {% if game.q != 'F' and game.q != 'P' and game.q != 'FO' and game.q != 'S' %}
            {% if game.h == 'GB' %}
              {{ game.hs }}
            {% else %}
              {{ game.vs }}
            {% endif %}
          {% else %}
            0
          {% endif %}
        {% endif %}
      {% endfor %}

Any help is hugely appreciated. Thanks in advance.

You can try this to find the correct path-
https://jsonpathfinder.com/

After you paste it, click the value that you want to retrieve. The path will be displayed at the right hand side.

I see how they’re nested, but not sure about the formatting for jinja2 (specifically competitors.team.abbreviation). I’m not seeing how this path
x.events[0].competitions[0].competitors[1].team.abbreviation is helpful. The order that these values show up in the json will be changing each week.

This works, as a test

{% for event in value_json["events"] %}
  {% for competition in event["competitions"] %}
    {% for competitor in competition["competitors"] %}
      {% if  competitor.team.abbreviation == 'GB' %}
        "It's GB"
      {% endif %}
    {% endfor %}
  {% endfor %}
{% endfor %}

but it’s not clear to me why an event could have multiple “competitions”…
If you always have a single one, you can simplify to

{% for event in value_json["events"] %}
    {% for competitor in event["competitions"][0]["competitors"] %}
      {% if competitor.team.abbreviation == 'GB' %}
        "It's GB"
      {% endif %}
    {% endfor %}
{% endfor %}

You can also do

{% for event in value_json["events"] %}
  {% if 'GB' in event.shortName %}
    "It's GB"
  {% endif %}
{% endfor %}
1 Like

Thank you so much! That was extremely helpful. This works great to report the score.

{% for event in value_json["events"] %}
  {% for competition in event["competitions"] %}
    {% for competitor in competition["competitors"] %}
      {% if  competitor.team.abbreviation == 'GB' %}
        {{ competitor.score }}
      {% endif %}
    {% endfor %}
  {% endfor %}
{% endfor %}

And your last example should work great to pull status.type.state info

edit: full sensor for others wanting an NFL score sensor for their team

  - platform: rest
    resource: http://site.api.espn.com/apis/site/v2/sports/football/nfl/scoreboard
    name: Packers Score
    value_template: >
      {% for event in value_json["events"] %}
        {% if 'GB' in event.shortName %}
          {% for competition in event["competitions"] %}
            {% if competition.status.type.state != 'pre' and  competition.status.type.state != 'post' %}
              {% for competitor in competition["competitors"] %}
                {% if  competitor.team.abbreviation == 'GB' %}
                  {{ competitor.score }}
                {% endif %}
              {% endfor %}
            {% else %}
              0
            {% endif %}
          {% endfor %}
        {% endif %}
      {% endfor %}
    scan_interval: 5