Restful sensor value_template question

I’m working on a restful sensor to pull outage data from my energy provider, but I’m getting stuck on the value template. I know what the error means… it doesn’t like the “?”, but I can’t figure out format a valid JSON search in a value template to get the correct value. The output of the JSON attributes from the JSON attributes path is correct.

What an I missing to get the value template to have the same data as the JSON attribute? Is this a syntax issue?

other things I’ve tried in the in the value_template that didn’t work:

{{states.sensor.austin_energy_78725_affected.val()}}
{{states.sensor.austin_energy_78725_affected.attributes.val}}
{{state_attr(‘sensor.austin_energy_78725_affected’,‘val’)}}
{{value_json.file_data.areas.cust_a.val}}

  - scan_interval: XXXXX
    resource_template: "https://kubra.io/{{state_attr('sensor.austin_energy_url_generation_data','interval_generation_data')}}/public/reports/7e91b271-8db9-4931-a14c-67845dfcd496_report.json"
    sensor:
      - name: Austin Energy 78725 Affected
        unique_id: austin_energy_78725_affected
        icon: mdi:transmission-tower
        unit_of_measurement: "number"
        value_template: >
          {{ value_json.file_data.areas[?(@.name == '78725')].cust_a.val}}
        json_attributes_path: "$.file_data.areas[?(@.name == '78725')].cust_a"
        json_attributes:
          - "val"

Error:

Invalid config for ‘rest’ at configuration.yaml, line 84: invalid template (TemplateSyntaxError: unexpected char ‘?’ at 30) for dictionary value ‘rest->3->sensor->0->value_template’, got “{{ value_json.file_data.areas[?(@.name == ‘78725’)].cust_a.val}}\n”

Sample output to be parsed

{
  "version": "V2",
  "file_title": "report_zip.json",
  "file_data": {
    "areas": [
      {
        "key": "zip",
        "name": "78725",
        "cust_a": {
          "val": 0
        },
        "cust_s": 4355,
        "percent_cust_a": {
          "val": 0
        },
        "etr": null,
        "etr_confidence": null,
        "gotoMap": {
          "bbox": [
            -97.67872599990034,
            30.206063998743392,
            -97.54131699976469,
            30.285654299022923
          ]
        },
        "areaId": "Boun|78725|zip",
        "n_out": 0
      },
      {
        "key": "zip",
        "name": "78759",
        "cust_a": {
          "val": 0
        },
        "cust_s": 24689,
        "percent_cust_a": {
          "val": 0
        },
        "etr": null,
        "etr_confidence": null,
        "gotoMap": {
          "bbox": [
            -97.79529059993826,
            30.361292998426297,
            -97.70255300000878,
            30.43453299964365
          ]
        },
        "areaId": "Boun|78759|zip",
        "n_out": 0
      }
    ]
  }
}

Templates in HA use Jinja, so you need to use Jinja filters and functions, not a JSON query.

  - resource_template: "https://kubra.io/{{state_attr('sensor.austin_energy_url_generation_data','interval_generation_data')}}/public/reports/7e91b271-8db9-4931-a14c-67845dfcd496_report.json"
    sensor:
      - name: Austin Energy 78725 Affected
        unique_id: austin_energy_78725_affected
        icon: mdi:transmission-tower
        unit_of_measurement: "number"
        value_template: >
          {{ ((value_json.file_data.areas | selectattr('name', 'eq', '78725')|first).cust_a.val) }}
1 Like

Thank you, that is exactly what I was missing. On my simpler restful sensors the json_attributes_path and value_template were basically the same and I was not putting it together that there were actually not the same query language.