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

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

1 Like

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.

1 Like

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:

2 Likes

Hi Pierrot,

This is my first post and my first faltering steps in the wonderful world of HA.
I would very much like to use this idea, but I’m struggling to replicate it.

Did you create this in a Sensor element in the Lovelace dashboard? When I create a sensor in the dashboard it picks up the following elements:

type: sensor
entity: sensor.0x00158d000638dd6c_hchp_cost

And then displays a value of 5.157800 EUR no matter what station ID I put into the YAML code.

What I would love to do is plot the values in a line graph, or even in a gauge, with thresholds for low, medium and high levels of the river. And trigger a notify automation when I’m getting close to red!

Sorry if this is a real newbie question. I’ve been reading the manual, promise!

Jerry

Bonjour @pierrot et @JCLB je vous remercie pour ces infos, mais je vous avoue débuter avec HA et ne pas trop savoir où placer ces différentes infos, un capteur virtuel? dans le config.yaml?

Ca a fonctionné en plaçant le contenu dans le config.yaml

Bonsoir @ebretteville ,
Vous utilisez aussi vigicrue ?
Je viens du monde domoticz, et je cherche à récupérer les mesures vigicrues aussi, cependant je n’y suis pas encore parvenu.

Vous ajoutez juste une portion de code yaml ?
Merci

Bonjour,
N’hésite pas à aller voir cette discussion sur le forum français : Vigicrue avec Rest - Entraide Home Assistant - Home Assistant Communauté Francophone (hacf.fr)