How to extract JSON value from sensor state attributes

Hey guys,

I’m using a REST Sensor with the json_attributes attribute to retrieve a high level JSON field devices which in itself is another JSON object. This object is then stored in the sensor state attributes. How do I parse this JSON object to retrieve a particular field from it?

What I want to retrieve is the gateway status plus the taplinker status.

State attributes:

  "devices": [
      "name": "Gateway",
      "location": "redacted",
      "gatewayId": "redacted",
      "status": "Connected",
      "version": "A0301311810201851I_C0300051708152305",
      "taplinker": [
          "taplinkerName": "Lawn",
          "taplinkerId": "redacted",
          "status": "Connected",
          "location": "Backyard",
          "version": "T0300021702051436",
          "signal": "61%",
          "batteryStatus": "100%",
          "workMode": "I",
          "plan": {
            "interval": 4,
            "Y": 2019,
            "X": 1,
            "Z": 2,
            "ecoOff": 2,
            "ecoOn": 3,
            "eco": true,
            "slot": [
                "H": 5,
                "M": 0,
                "D": 15
          "watering": null
  "friendly_name": "LinkTap Get All Devices"

Gateway status:

Taplinker status:

Thanks for the reply. I knew how to drill down to the element I wanted but not how to extract it from within the sensor state attributes. I figured it out… a lot simpler than I thought:

{{ state_attr('sensor.linktap_get_all_devices', 'devices')[0]["status"] }}
{{ state_attr('sensor.linktap_get_all_devices', 'devices')[0]["taplinker"][0]["status"] }}


You fooled me when you asked:

Thank you for posting this answer to the problem!

Took quite a bit of googling to find this answer!

Hi there,

It took me a long time to find this thread for a similar issue! However I’m wondering if there’s a way to put more of this within the state_attr function, since that won’t error out if the attribute doesn’t exist.

My use case is with the SureHA (SurePet flap/feeders) integration, for an attribute “feeding” which might not exist for every pet, e.g. the below causes an error because pet_bella has no feeding data:

{{ state_attr("device_tracker.pet_daisy", "status")['feeding']['change'] }}
{{ state_attr("device_tracker.pet_bella", "status")['feeding']['change'] }}

Actually I can work around this in my case as I know pet_Bella won’t have feeidng data, and I’m sure there’s something I could wrap around this to make it error-resistant, but was hopign to do something graceful like have state_attr handle that for me.

I have a similar issue extracting data from a JSON response. My REST call returns this in the sensor,

  "data": {
    "me": {
      "homes": [
          "electricVehicles": [
              "lastSeen": "2023-11-01T01:38:26.000+00:00",
              "battery": {
                "percent": 80

I want to extract the lastseen and the battery percent.
I looked at the example above and tried:

      friendly_name: "Polestar 2 State of Charge"
      value_template: "{{ state_attr('sensor.polestar','')[0]['electricVehicles'][0]['battery.percent']) | int }}"
      unit_of_measurement: "%"
      device_class: battery

But this is not accepted

Use JSON Path Finder to dig JSON values. You have an error in your string