Extracting Values from JSON API Call

I want to create a pie chart from the UK Climate Intensity API.
This is the API Entry.
https://api.carbonintensity.org.uk/regional/regionid/12

The values I want are under GenerationMix, with each fuel type being a percentage.

Using this code I can get the list of values:

  - platform: rest
    name: Carbon intensity
    resource_template: >-
      {% set regionid = 12 %}
      https://api.carbonintensity.org.uk/regional/regionid/{{ regionid }}
    value_template: "{{ value_json.data[0].data[0].generationmix.fuel }}"
    json_attributes_path: "$.data[0].data[0]"
    json_attributes:
      - "generationmix" 

Which returns something like this:

generationmix: 
- fuel: biomass
  perc: 0
- fuel: coal
  perc: 0
- fuel: imports
  perc: 9
- fuel: gas
  perc: 53.5
- fuel: nuclear
  perc: 3.1
- fuel: other
  perc: 0
- fuel: hydro
  perc: 0
- fuel: solar
  perc: 33.6
- fuel: wind
  perc: 0.7

What I want to do is get each value in to its own sensor value, which I can then use to create a Pie Chart using ApexCharts.
Can anyone help?

/cc @troon :smiley:

Using the RESTful integration, I’ve done the first two for you and the rest should be obvious. Untested, but ought to work:

rest:
  - resource_template: >-
      {% set regionid = 12 %}
      https://api.carbonintensity.org.uk/regional/regionid/{{ regionid }}
    scan_interval: 3600

    sensor:
      - name: "Biomass mix"
        value_template: >-
          {% set genlist = value_json.data[0].data[0].generationmix %}
          {% for genitem in genlist %}
             {% if genitem['fuel'] == 'biomass' %}
               {{ genitem['perc'] }}
             {% endif %}
          {% endfor %}
        unit_of_measurement: '%'

      - name: "Coal mix"
        value_template: >-
          {% set genlist = value_json.data[0].data[0].generationmix %}
          {% for genitem in genlist %}
             {% if genitem['fuel'] == 'coal' %}
               {{ genitem['perc'] }}
             {% endif %}
          {% endfor %}
        unit_of_measurement: '%'

@koying was already on it :rofl:

little less verbose

{{ (value_json.data[0].data[0].generationmix | selectattr('fuel', 'eq', 'biomass') | first).perc | default }}
1 Like

Hello,
It does indeed work. I had to setup a rest.yaml page as I don’t use rest in that way anywhere else, but other than that, it was pretty easy to get going.

In my code, I changed the names so that the sensors are all grouped together within HA.
So this in a file called rest.yaml and an entry in the configuration.yaml of

rest: !include includes/rest.yaml

Full code:


- resource_template: >-
      {% set regionid = 12 %}
      https://api.carbonintensity.org.uk/regional/regionid/{{ regionid }}
    scan_interval: 3600

    sensor:
      - name: "Generation Mix Biomass"
        value_template: >-
          {% set genlist = value_json.data[0].data[0].generationmix %}
          {% for genitem in genlist %}
             {% if genitem['fuel'] == 'biomass' %}
               {{ genitem['perc'] }}
             {% endif %}
          {% endfor %}
        unit_of_measurement: '%'

      - name: "Generation Mix Coal"
        value_template: >-
          {% set genlist = value_json.data[0].data[0].generationmix %}
          {% for genitem in genlist %}
             {% if genitem['fuel'] == 'coal' %}
               {{ genitem['perc'] }}
             {% endif %}
          {% endfor %}
        unit_of_measurement: '%'
        
      - name: "Generation Mix Imports"
        value_template: >-
          {% set genlist = value_json.data[0].data[0].generationmix %}
          {% for genitem in genlist %}
             {% if genitem['fuel'] == 'imports' %}
               {{ genitem['perc'] }}
             {% endif %}
          {% endfor %}
        unit_of_measurement: '%'
        
      - name: "Generation Mix Gas"
        value_template: >-
          {% set genlist = value_json.data[0].data[0].generationmix %}
          {% for genitem in genlist %}
             {% if genitem['fuel'] == 'gas' %}
               {{ genitem['perc'] }}
             {% endif %}
          {% endfor %}
        unit_of_measurement: '%'
        
      - name: "Generation Mix Nuclear"
        value_template: >-
          {% set genlist = value_json.data[0].data[0].generationmix %}
          {% for genitem in genlist %}
             {% if genitem['fuel'] == 'nuclear' %}
               {{ genitem['perc'] }}
             {% endif %}
          {% endfor %}
        unit_of_measurement: '%'
        
      - name: "Generation Mix Other"
        value_template: >-
          {% set genlist = value_json.data[0].data[0].generationmix %}
          {% for genitem in genlist %}
             {% if genitem['fuel'] == 'other' %}
               {{ genitem['perc'] }}
             {% endif %}
          {% endfor %}
        unit_of_measurement: '%'        
        
      - name: "Generation Mix Hydro"
        value_template: >-
          {% set genlist = value_json.data[0].data[0].generationmix %}
          {% for genitem in genlist %}
             {% if genitem['fuel'] == 'hydro' %}
               {{ genitem['perc'] }}
             {% endif %}
          {% endfor %}
        unit_of_measurement: '%'        
        
      - name: "Generation Mix Solar"
        value_template: >-
          {% set genlist = value_json.data[0].data[0].generationmix %}
          {% for genitem in genlist %}
             {% if genitem['fuel'] == 'solar' %}
               {{ genitem['perc'] }}
             {% endif %}
          {% endfor %}
        unit_of_measurement: '%'        
        
      - name: "Generation Mix Wind"
        value_template: >-
          {% set genlist = value_json.data[0].data[0].generationmix %}
          {% for genitem in genlist %}
             {% if genitem['fuel'] == 'wind' %}
               {{ genitem['perc'] }}
             {% endif %}
          {% endfor %}
        unit_of_measurement: '%'

The pie chart code is this:

type: 'custom:apexcharts-card'
chart_type: pie
header:
  title: Generation Mix
  show: true
series:
  - entity: sensor.generation_mix_biomass
    name: Biomass
  - entity: sensor.generation_mix_coal
    name: Coal
  - entity: sensor.generation_mix_gas
    name: Gas
  - entity: sensor.generation_mix_hydro
    name: Hydro
  - entity: sensor.generation_mix_imports
    name: Imports
  - entity: sensor.generation_mix_nuclear
    name: Nuclear
  - entity: sensor.generation_mix_other
    name: Other
  - entity: sensor.generation_mix_solar
    name: Solar
  - entity: sensor.generation_mix_wind
    name: Wind

The resulting pie chart is thus

Pie Chart

Pretty happy with that, thanks @troon for the pointer.

1 Like