Question about Rest API

I want the current consommation of Québec, the data is avalable to https://www.hydroquebec.com/data/documents-donnees/donnees-ouvertes/json/demande.json
and it look like that:

{
  "dateStart" : "2023-01-10T00:00:00",
  "dateEnd" : "2023-01-12T00:00:00",
  "recentHour" : "2023-01-11T10:45:00",
  "indexDonneePlusRecent" : 139,
  "nbDateAvecData" : 140,
  "details" : [ {
    "date" : "2023-01-10T00:00:00",
    "valeurs" : {
      "demandeTotal" : 25842.0
    }
  }, {
    "date" : "2023-01-10T00:15:00",
    "valeurs" : {
      "demandeTotal" : 25625.0
    }
  }, {
    "date" : "2023-01-10T00:30:00",
    "valeurs" : {
      "demandeTotal" : 25509.0
    }
  }, {
    "date" : "2023-01-10T00:45:00",
    "valeurs" : {
      "demandeTotal" : 25431.0
    }
  }, {
    "date" : "2023-01-10T01:00:00",
    "valeurs" : {
      "demandeTotal" : 25448.0
    }

My script is:

- platform: rest
    name: Hydro-Québec Demande Totale
    resource: https://www.hydroquebec.com/data/documents-donnees/donnees-ouvertes/json/demande.json
    value_template: '{{ value_json.details[indexDonneePlusRecent].valeurs.demandeTotal }}'
    unit_of_measurement: 'MW'
    scan_interval: 900

But it’s doesn’t work, I think “indexDonneePlusRecent” didn’t return the number. Someone can help me?

When I use the link above it throws an error,
Try to add logging for the rest component (in confyaml > logger section add homeassistant.components.rest : debug) and see if it actually loads the data

indexDonneePlusRecent is a field in value_json, but you’re not using that, you’re referencing an undefined variable named indexDonneePlusRecent. You need to index the array using the field:

    value_template:  '{{ value_json.details[value_json.indexDonneePlusRecent].valeurs.demandeTotal }}'

You can experiment with this template code in the template editor by creating a variable with the expected JSON data :

{% set value_json = {
  "dateStart" : "2023-01-10T00:00:00",
  "dateEnd" : "2023-01-12T00:00:00",
  "recentHour" : "2023-01-11T10:45:00",
  "indexDonneePlusRecent" : 1,
  "nbDateAvecData" : 140,
  "details" : [ {
    "date" : "2023-01-10T00:00:00",
    "valeurs" : {
      "demandeTotal" : 25842.0
    }
  }, {
    "date" : "2023-01-10T00:15:00",
    "valeurs" : {
      "demandeTotal" : 25625.0
    }
  }, {
    "date" : "2023-01-10T00:30:00",
    "valeurs" : {
      "demandeTotal" : 25509.0
    }
  }, {
    "date" : "2023-01-10T00:45:00",
    "valeurs" : {
      "demandeTotal" : 25431.0
    }
  }, {
    "date" : "2023-01-10T01:00:00",
    "valeurs" : {
      "demandeTotal" : 25448.0
    }
  } ]
} 
%}

{{ value_json.details[value_json.indexDonneePlusRecent].valeurs.demandeTotal }}
1 Like

Thanks! It’s good to know. :slight_smile: