Sensor Rest Platform json without key

Hey Guys,
I’m trying to create a sensor with the rest platform to call an API (grocy)

This API returns a JSON array (at the moment with to record set a 0 & 1

In the value_template I can extract picking either [1] or [0], I’m trying to do the same in the json_attributes for then later to create sensors with the platform template to make only one API call.

Json:

[
   {
      "chore_id":"1",
      "last_tracked_time":null,
      "next_estimated_execution_time":"2021-01-02 19:50:33",
      "track_date_only":"1",
      "next_execution_assigned_to_user_id":null,
      "chore_name":"Clean Bathroom"
   },
   {
      "chore_id":"2",
      "last_tracked_time":null,
      "next_estimated_execution_time":"2021-01-03 19:50:33",
      "track_date_only":"0",
      "next_execution_assigned_to_user_id":null,
      "chore_name":"Clean Ensuite"
   }
]

Code:

- platform: rest
  name: grocy_chores_sensors
  resource: http://IP_ADDR/api/chores
  method: GET
  value_template: '{{ value_json[1].chore_id }}'
  headers:
    Accept: application/json
    Content-Type: application/json
    GROCY-API-KEY: XXX
  json_attributes_path: "$"
  json_attributes:
    - 0.chore_id
    - 0.chore_name
    - 0.next_estimated_execution_time
    - 0.last_tracked_time
    - 1.chore_id
    - 1.chore_name
    - 1.next_estimated_execution_time
    - 1.last_tracked_time

Where am I going wrong ?
Thanks for the help !

What errors? Any logs? Please clarify.

Hi Kendell!

This is my error from the logs: JSON result was not a dictionary or list with 0th element a dictionary

Please post all of the error, not just one line, and hit the image button.

Hi Kendell,

This is the error message:

Logger: homeassistant.components.rest.sensor
Source: components/rest/sensor.py:251
Integration: rest (documentation, issues)
First occurred: 15:32:46 (2 occurrences)
Last logged: 15:33:18

JSON result was not a dictionary or list with 0th element a dictionary

Try using Jinja instead of that syntax.


with the way you have it set up, i’d just have 2 separate sensors. You’re not reaching out to the net so there’s no reason to keep it in 1 sensor.

- platform: rest
  name: grocy_chores_sensors
  resource: http://IP_ADDR/api/chores
  method: GET
  value_template: '{{ value_json[0].chore_id }}'
  headers:
    Accept: application/json
    Content-Type: application/json
    GROCY-API-KEY: XXX
  json_attributes_path: "$.0"
  json_attributes:
    - chore_id
    - chore_name
    - next_estimated_execution_time
    - last_tracked_time
- platform: rest
  name: grocy_chores_sensors
  resource: http://IP_ADDR/api/chores
  method: GET
  value_template: '{{ value_json[1].chore_id }}'
  headers:
    Accept: application/json
    Content-Type: application/json
    GROCY-API-KEY: XXX
  json_attributes_path: "$.1"
  json_attributes:
    - chore_id
    - chore_name
    - next_estimated_execution_time
    - last_tracked_time

I don’t think you can mix json_attributes and value_template.

Yes you can. I do it all the time. Here’s a working config in my own system

  - platform: rest
    resource: "https://services1.arcgis.com/0MSEUqKaxRlEPj5g/arcgis/rest/services/ncov_cases/FeatureServer/1/query?f=json&where=(Confirmed%20%3E%200)%20AND%20(Country_Region%3D%27US%27)&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&orderByFields=Confirmed%20desc%2CCountry_Region%20asc%2CProvince_State%20asc&outSR=102100&resultOffset=0&resultRecordCount=250&cacheHint=true"
    name: Corona Virus Rest
    value_template: >
      {%- set last_updated = value_json.features | map(attribute='attributes.Last_Update') | list | max / 1000 %}
      {{ last_updated | timestamp_custom('%Y-%m-%dT%H:%M:%S.%f+00:00', False) }}
    json_attributes:
      - features
1 Like

I believe @natureboss89’s issue is that he’s using 0.xyz as the json_attributes attribute name. If he just moved the 0 and the 1 into the json_attributes_path selector, it should work.

I understand, but if it had a key I could have done it right ?

If it was keyed you wouldn’t have the issue. Is there a reason why you don’t want 2 rest sensors?

Hi Petro! I like to push the limits of my knowledge to learn, good to know if something can be done or not. I have multiple sensors as suggested !

Hi ,
I think I have a possilbe solution, I shared the same in an other post but this one I think is also about the exact same case and was not resolved…
I faced same issue by getting all google calendar of current day which come as a json array and store them in a sensor (by default only next callendar event is stored in the calendar attribute…)

I did fix it by using command line sensor to get the json array in a json with one direct attribute.
This way it is possible to have in json_attributes one attribute which contain all the array and can be used for later porcessing by any sensor or automation.
In value_template I store the number of entries in the array

Here how I did it :

- platform: command_line
  name: calendar_events
  scan_interval: 1500
  command: >
     echo "{\"events\":" $(
     curl 
     -s 
     -H 'Authorization: Bearer XXX'
     'http://192.168.0.50:8123/api/calendars/calendar.mycalendar_gmail_com?start={{ now().strftime('%Y-%m-%dT%H:%M:%SZ') }}&end={{ (now() + timedelta(days=1)).strftime('%Y-%m-%dT%H:%M:%SZ') }}'
     ) "}" 
  value_template: > 
     {{ value_json.events | length }}
  json_attributes:
     - events

hope this helps, if not sorry for bringing back an old post to life…

1 Like