How to process large json over 255

Im trying to pull some info from this link.

http://www.nfl.com/liveupdate/scores/scores.json

im using a sensor like this- platform: rest

  name: NFL
  json_attributes:
    - home
    - abbr
    - away
  resource: 'http://www.nfl.com/liveupdate/scores/scores.json'

(im just testing if I can get it to work, im new at this)

if you go to that url you can see its more than 255 charecters. I see this error in ha

2018-09-06 17:18:19 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/app/homeassistant/helpers/entity.py", line 313, in async_update_ha_state
    self.entity_id, state, attr, self.force_update, self._context)
  File "/usr/src/app/homeassistant/core.py", line 853, in async_set
    context)
  File "/usr/src/app/homeassistant/core.py", line 623, in __init__
    "State max length is 255 characters.").format(entity_id))
homeassistant.exceptions.InvalidStateError: Invalid state encountered for entity id: sensor.nfl. State max length is 255 characters.

If anyone could help that would be really nice

First you need to specify something smaller for the sensor’s state with value_template.

But even if you do that, this output has a level above home, abbr, etc. And the keys to the higher levels are basically dates, which of course change. So I don’t think you’re going to be able to do this with a simple REST Sensor.

I would suggest taking a look at jq. Just Google search for it. It’s a JSON parser that you can use in conjunction with curl and a Command Line Sensor. E.g., to get the scores for a game where Denver is at home, you could do this:

curl -s http://www.nfl.com/liveupdate/scores/scores.json | jq 'to_entries|.[]|select(.value.home.abbr == "DEN")|.value.home,.value.away'

The output:

{
  "score": {
    "1": null,
    "2": null,
    "3": null,
    "4": null,
    "5": null,
    "T": null
  },
  "abbr": "DEN",
  "to": null
}
{
  "score": {
    "1": null,
    "2": null,
    "3": null,
    "4": null,
    "5": null,
    "T": null
  },
  "abbr": "SEA",
  "to": null
}

Of course that’s not a complete solution, but maybe it’s a start in the right direction. Or you could write some python code that could do a request and parse the output into something you could use in HA.

2 Likes

Awesome. That’s what I needed. Now I got two paths to go look into. Thank you for taking the time to help!

Thank you, I have this problem in a completely unrelated sensor I was trying to develop.