JSON Error from Rest Sensor

Can someone help with understanding why my Rest sensor is returning the following error in my log? “JSON result was not a dictionary or list with 0th element a dictionary”.

  - platform: rest
    resource: http://content.caiso.com/awe/noticeflexRSS.xml
    scan_interval: 300 #5 minutes
    name: Flex_Alert_Newest
    value_template: "OK"
    json_attributes_path: "$.rss.channel.item.[0]"
    json_attributes:
      - title
      - link
      - description

Well, there are a few issues.

  1. It’s not JSON, it’s XML, it cannot be parsed as JSON, because it isn’t JSON.
  2. If it was JSON, it would be $.rss.channel[0].item[0]

Restfull automatically convert xml to json, so that’s covered :wink:

1 Like

I’m still hoping someone has an answer for this… the XML file is otherwise used and processed by Home Assistant just fine… I just have a bunch of errors in my log from every scan of the XML file.

Post the errors…

This happens because you are trying to reference an array of JSON objects.

After converting the current XML to JSON we get:

{
	"rss": {
		"channel": {
			"title": "CAISO Flex Alert Notice Log",
			"link": "http://www.caiso.com/awe/ noticelogflex.html",
			"description": "Flex Alert Notice Log",
			"language": "en-us",
			"item": {
				"title": "No Flex notices issued in last 90 days",
				"link": "http://www.caiso.com/informed/Pages/Notifications/Flex-Alerts.aspx",
				"description": ""
			}
		},
		"_version": "0.91"
	}
}

Notice how item is a single JSON object. When there haven’t been any caiso flex alerts for a while, the xml will be formatted in this error producing style. Once flex alerts start rolling in, something like the following is produced:

{
	"rss": {
		"channel": {
			"title": "CAISO Flex Alert Notice Log",
			"link": "http://www.caiso.com/awe/ noticelogflex.html",
			"description": "Flex Alert Notice Log",
			"language": "en-us",
			"item": [{
					"title": "First alert",
					"link": "http://www.caiso.com/informed/Pages/Notifications/Flex-Alerts.aspx",
					"description": "first random description"
				},
				{
					"title": "Second alert",
					"link": "http://www.caiso.com/informed/Pages/Notifications/Flex-Alerts.aspx",
					"description": "second specific description"
				}
			]
		},
		"_version": "0.91"
	}
}

Notice how item now contains an array of JSON objects? This is what json_attributes_path is trying to reference.

I’ve got the same sensor in my setup (gotta love rolling blackouts). Unfortunately, I haven’t figured out a way to avoid the error. It does seem to only occur when the XML source initially switches into the glitchy format. When the flex alerts populate, so will the sensor attributes. It’s not ideal, but you’ll (probably) have to wait until the alerts start happening to finetune your sensor.

You could try to build a template that first check if items is a single entry or an array. If the first, use this and if array use element 0?

That would work for the value of the sensor, but not its attributes.

You could create a rest sensor that saves (part of) the json in an attribute and then create additional template sensors to parse that. I use this for my Corona dashboard:

An additional benefit is that you only need to call the api once to create multiple sensors.