Get JSON value with Rest (nested / not logic)

I want to monitor some stock in Home Assistant but I’m not getting it working. So I want from the JSON below the value from inStock

So i figured out with https://jsonpathfinder.com that the path should be x.availableLots[“0001-01-01T00:00:00.0000000Z”].9000000013.inStock

This is the JSON

{
  "availableLots": {
    "0001-01-01T00:00:00.0000000Z": {
      "9000000013": {
        "inStock": "False",
        "deliveryType": "Ship",
        "onlineOrderAvailable": "False",
        "render": "True",
        "isUnknownDate": "False",
        "showDateOverride": "False",
        "deliverByDates": {
          "0001": "2021-11-23T12:00:00.0000000Z",
          "0004": "2021-11-25T12:00:00.0000000Z"
        },
        "hasArbitraryLimitPolicy": "False"
      }
    }
  }
}

So I tried multiple combinations to get this into a sensor with the rest sensor

    json_attributes:
      - inStock
      - deliveryType
    json_attributes_path: '$.availableLots["0001-01-01T00:00:00.0000000Z"].["9000000013"]'

    json_attributes:
      - inStock
      - deliveryType
    json_attributes_path: '$.availableLots["0001-01-01T00:00:00.0000000Z"].9000000013'

    value_template: '{{ value_json.availableLots["0001-01-01T00:00:00.0000000Z"].9000000013.inStock }}'

value_template: '{{ value_json.availableLots["0001-01-01T00:00:00.0000000Z"].9000000013.inStock }}'

Who can help me getting the inStock into a rest or template sensor?

Don’t combine [] and ., try this:

    json_attributes:
      - inStock
      - deliveryType
    json_attributes_path: '$.availableLots["0001-01-01T00:00:00.0000000Z"]["9000000013"]'

Documentation: here and here

That’s not working unfortunately, sensor not showing up when changing to your code with the following in the logs

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 382, in async_add_entities
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 607, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 715, in add_to_platform_finish
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 486, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 596, in _async_write_ha_state
    self.hass.states.async_set(
  File "/usr/src/homeassistant/homeassistant/core.py", line 1222, in async_set
    state = State(
  File "/usr/src/homeassistant/homeassistant/core.py", line 912, in __init__
    raise InvalidStateError(
homeassistant.exceptions.InvalidStateError: Invalid state encountered for entity ID: sensor.testrest. State max length is 255 characters.

So exceeding the 255 characters I guess is bugging it ?

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

Hm, yeah, seems like it
Do you need anything else extracted / something in it’s state?
If not, add

    value_template: OK

If this still won’t work, please paste the entire sensor config (of course, you can remove credentials/URLs and/or use secrets), so we can take a closer look at it.

With the code below I get the OK value

  - platform: rest
    name: testrest
    resource: !secret stock1url
    json_attributes:
      - inStock
      - deliveryType
    json_attributes_path: '$.availableLots["0001-01-01T00:00:00.0000000Z"]["9000000013"]'
    value_template: OK

but without the value template ok I get the errors mentioned below

  - platform: rest
    name: testrest
    resource: !secret stock1url
    json_attributes:
      - inStock
      - deliveryType
    json_attributes_path: '$.availableLots["0001-01-01T00:00:00.0000000Z"]["9000000013"]'

Ah sorry, try this:

  - platform: rest
    name: testrest
    resource: !secret stock1url
    json_attributes:
      - inStock
      - deliveryType
    value_template: 'value_json["0001-01-01T00:00:00.0000000Z"]["9000000013"]'

then he exactly shows what is in the value_json :stuck_out_tongue:

Oh sorry, I’m tired :laughing:

  - platform: rest
    name: testrest
    resource: !secret stock1url
    json_attributes:
      - inStock
      - deliveryType
    value_template: '{{ value_json["0001-01-01T00:00:00.0000000Z"]["9000000013"] }}'

The same struggle you are now having with me is the struggle I had this entire afternoon. I’m not getting any information in the sensor, I just want to know by value or true/false that there is stock

Maybe it’s easier to use sensor below and use a template sensor to get data from this sensor but that I also get not working because of the deep nested data

Okay, I’m not tired, I’m stupid.
This should finally do it, sorry for the wasted time :frowning:

  - platform: rest
    name: testrest
    resource: !secret stock1url
    json_attributes:
      - inStock
      - deliveryType
    value_template: '{{ value_json["availableLots"]["0001-01-01T00:00:00.0000000Z"]["9000000013"] }}'

Not wasting my time, happy that someone is willing to help me :slight_smile: But unfortunately it’s not working, now the sensor is not coming up with the error of 255 characters again so it looks like it’s not looking to only inStock or shipping

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 382, in async_add_entities
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 607, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 715, in add_to_platform_finish
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 486, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 596, in _async_write_ha_state
    self.hass.states.async_set(
  File "/usr/src/homeassistant/homeassistant/core.py", line 1222, in async_set
    state = State(
  File "/usr/src/homeassistant/homeassistant/core.py", line 912, in __init__
    raise InvalidStateError(
homeassistant.exceptions.InvalidStateError: Invalid state encountered for entity ID: sensor.testrest. State max length is 255 characters.

I’ve set up a webserver and am continuing testing, I’ll get back to you right away.

Alright, after banging my head against the wall for a good hour, I think I’ve found the problem, it’s that theres a . in the key (:00.0000000Z).
This issue might be related.
The thing is, the extraction works for the value_template, but not for the json_attributes_path, I’m assuming it interprets the . as part of the path, not the name.

Please file an issue since this behaviour is unexpected.

For a quick fix, just create two sensors that extract via value_template.
If you want to, you can create a template sensor to have them combined again.
Since you’d be using two sensors on the same endpoint, use rest integration, not the sensor integration.

Example:

# note that this is using the rest integration, don't put the following
# in your sensor.yaml / under sensor:
rest:
  - resource: !secret stock1url
    sensor:
    - name: testrest_inStock
      value_template: '{{value_json["availableLots"]["0001-01-01T00:00:00.0000000Z"]["9000000013"]["inStock"]}}'
    - name: testrest_deliveryType
      value_template: '{{value_json["availableLots"]["0001-01-01T00:00:00.0000000Z"]["9000000013"]["deliveryType"]}}'

# note that this is is using the template integration, don't put the following
# in your sensor.yaml / under sensor:
template:
  - sensor:
      - name: "myTestRest"
        state: "OK"
        attributes:
          inStock: "{{states('sensor.testrest_inStock')}}"
          deliveryType: "{{states('sensor.testrest_deliveryType')}}"
1 Like

You’re awesome! That worked! Thanks for the support and tinkering with me hopefully someone else will also have something on our discussion here :slight_smile:

P.S. Hopefully you didn’t bang your head too much haha :slight_smile:

1 Like