REST Sensor how to extract the value from date/value pairs

Hi there, I’ve been using a REST API call to get some solar PV power consumption data from my inverter into home assistant. I’ve got the easy values imported such as pv_to_date and pv_today into the sensors, by referencing value_json.stats.kpis.pv_to_date and value_json.stats.kpis.pv_today, as these are easily identifiable at the end of the data feed. However I’m struggling to get the consumed energy for today into a sensor. As you can see below, the data structure has an inverter serial number, followed by each day of month and a corresponding value. So I’d like to get the consumed value matching today’s date (i.e. ‘stats’.‘graphs’.‘consumed_energy’.‘2731042228110002’.. into a sensor for using in the HA energy tab.
I’ve been trying a few things but I’m not knowledgeable enough in coding yet… .

  - name: "consumed_today"
    value_template: >
      {% set today = now().strftime('%Y-%m-%d') %}
      {% set items = value_json | selectattr('stats'.'graphs'.'consumed_energy'.'2731042228110002','eq',today) | list | first %}

Can anyone show me how to get appropriate value, and can it be retrieved at the same time as I gather the other sensor data?

{
  "stats": {
    "graphs": {
      "pv_energy": {
        "2731042228110002": {
          "2023-12-01": 0,
          "2023-12-02": 0,
          "2023-12-03": 0,
          "2023-12-04": 0,
          "2023-12-05": 0,
          "2023-12-06": 0,
          "2023-12-07": 0,
          "2023-12-08": 0,
          "2023-12-09": 0,
          "2023-12-10": 0,
          "2023-12-11": 1,
          "2023-12-12": 2,
          "2023-12-13": 5,
          "2023-12-14": 1,
          "2023-12-15": 0,
          "2023-12-16": 0,
          "2023-12-17": 0,
          "2023-12-18": 0,
          "2023-12-19": 0,
          "2023-12-20": 0,
          "2023-12-21": 0,
          "2023-12-22": 0,
          "2023-12-23": 0,
          "2023-12-24": 0,
          "2023-12-25": 0,
          "2023-12-26": 0,
          "2023-12-27": 0,
          "2023-12-28": 0,
          "2023-12-29": 0,
          "2023-12-30": 0,
          "2023-12-31": 0
        }
      },
      "consumed_energy": {
        "2731042228110002": {
          "2023-12-01": 0,
          "2023-12-02": 0,
          "2023-12-03": 0,
          "2023-12-04": 0,
          "2023-12-05": 0,
          "2023-12-06": 0,
          "2023-12-07": 0,
          "2023-12-08": 0,
          "2023-12-09": 0,
          "2023-12-10": 1,
          "2023-12-11": 3,
          "2023-12-12": 19,
          "2023-12-13": 33,
          "2023-12-14": 14,
          "2023-12-15": 0,
          "2023-12-16": 0,
          "2023-12-17": 0,
          "2023-12-18": 0,
          "2023-12-19": 0,
          "2023-12-20": 0,
          "2023-12-21": 0,
          "2023-12-22": 0,
          "2023-12-23": 0,
          "2023-12-24": 0,
          "2023-12-25": 0,
          "2023-12-26": 0,
          "2023-12-27": 0,
          "2023-12-28": 0,
          "2023-12-29": 0,
          "2023-12-30": 0,
          "2023-12-31": 0
        }
      },
      "no_comms": [
        
      ]
    },
    "kpis": {
      "pv_today": 1,
      "pv_month": 6,
      "pv_to_date": 6
    }
  }
}

Thanks in advance.

{% set today = today_at().date() | string %}
{{ value_json.stats.graphs.consumed_energy.items() | select('0', 'eq', today) | map(attribute='1') | first | default }}

Hey Petro, thanks very much for the quick response, that set me on the correct path. I needed to add in the static inverter path into the tree, and now my current logic looks like this …

  - name: "grid_consumed_today"
    value_template: >-
          {% set today = today_at().date() | string %}
          {% set energy_data = value_json.stats.graphs.consumed_energy["2731042228110002"].items() %}
          {% set grid_consumed_today = energy_data[today] if today in energy_data.keys() else 0 %}
          {{ grid_consumed_today }}
    unit_of_measurement: "kWh"
    device_class: energy
    state_class: total_increasing

but for some reason the grid_consumed_today still showing ‘unknown’. I’ve already tested out the steps using the template editor, and also tried creating a test sensor in sensors.yaml with the actual data from the API, and that test sensor worked correctly and extracted the value matching the date. Why it’s not working in the live state I don’t know… yet. I’m going to keep trying …

sorry, I missed a level.

{% set today = today_at().date() | string %}
{{ value_json.stats.graphs.consumed_energy['2731042228110002'].items() | selectattr('0', 'eq', today) | map(attribute='1') | first | default }}

It will be unknown until the result is pulled from the endpoint.

Petro I can’t get your line to work… trying it in the template results in “TemplateRuntimeError: No test named ‘0’.” on this line
{{ value_json.stats.graphs.consumed_energy[‘2731042228110002’].items() | select(‘0’, ‘eq’, today) | map(attribute=‘1’) | first | default }}

I know this way works in the template, spreading the logic over 2 lines…

{% set consumed_energy_data = value_json.stats.graphs.consumed_energy[“2731042228110002”] %}
{% set grid_consumed_today = consumed_energy_data[today] if today in consumed_energy_data.keys() else 0 %}

but still no joy when I put the logic into the config.yaml. All the basic stats pv_today, pv_now are fine, just this one wont populate. I’ve even stripped back to just doing this one and it fails to populate. I’ve debug set on the rest sensor but nothing showing in the logs. Wierd…

sorry, select should be selectattr