How to create a sensor for river level getting data from API vigicrue (France)

Tags: #<Tag:0x00007f3265072fa8>

Hi,
I’m trying to define a sensor to be updated every hour to get the river level close to my house. The source is an API providing a JSON output (https://hubeau.eaufrance.fr/api/v1/hydrometrie/observations_tr?code_entite=V453001002&size=1&pretty&grandeur_hydro=H&fields=date_obs,resultat_obs,continuite_obs_hydro).
I’m trying it by simply create a sensor:

- platform: rest
  name: River Deep
  scan_interval: 1200
  resource: https://hubeau.eaufrance.fr/api/v1/hydrometrie/observations_tr?code_entite=V453001002&size=1&pretty&grandeur_hydro=H&fields=date_obs,resultat_obs,continuite_obs_hydro
  value_template: '{{ value_json.resultat_obs }}'
  unit_of_measurement: "m"

I’m not an expert on JSON! And this code is not returning anything.
Thanks in advance for your help

Welcome. I moved your post as this is a configuration question, not development.

Try this:

  value_template: "{{ value_json['data'][0]['resultat_obs'] }}"

I found it by pasting the json at the link you provided into the left side of this tool then drilling down to the required item on the right hand side:

https://jsonpathfinder.com/

Good job on formatting your pasted code correctly in your first post. I’ve lost count of the times I’ve had to ask a fist time poster to do this.

Hi Tom,

Thanks a lot for the correction and the tip.
you are a boss!
working fine now.

1 Like

Hi,

here is the code for both height (m) and flow (m3/s) referred as H and Q in the API

sensor:
  - platform: rest
    name: Loire Hauteur Orleans
    scan_interval: 1200
    resource: https://hubeau.eaufrance.fr/api/v1/hydrometrie/observations_tr?code_entite=K435001020&size=1&pretty&grandeur_hydro=H&fields=date_obs,resultat_obs,continuite_obs_hydro
    value_template: "{{ ((value_json['data'][0]['resultat_obs']) | int / 1000 | round(3) ) }}" # mm en m
    unit_of_measurement: "m"

  - platform: rest
    name: Loire Débit Orleans
    scan_interval: 1200
    resource: https://hubeau.eaufrance.fr/api/v1/hydrometrie/observations_tr?code_entite=K435001020&size=1&pretty&grandeur_hydro=Q&fields=date_obs,resultat_obs,continuite_obs_hydro
    value_template: "{{ (((value_json['data'][0]['resultat_obs']) | int / 1000 ) | round ) }}" # l en m3/s
    unit_of_measurement: "m3/s"

remember you can search the station ID on vigicrues.gouv.fr

Don’t know if it’s easily possible to parse both answers simultaneously from this query:
API request H and Q, list 2

PS: my 1st post here :wave:

1 Like