Create sensor from xml data using rest platform

hello all,

I’ll have an xml which come from this website : https://donnees.roulez-eco.fr/opendata/instantane.

here an short example of the data (the order of the array can change):

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<pdv_liste>
  <pdv id="1000001" latitude="4620114" longitude="519791" cp="01000" pop="R">
    <adresse>596 AVENUE DE TREVOUX</adresse>
    <ville>SAINT-DENIS-LèS-BOURG</ville>
    <services>
      <service>Station de gonflage</service>
      <service>Vente de gaz domestique (Butane, Propane)</service>
      <service>DAB (Distributeur automatique de billets)</service>
    </services>
    <prix nom="Gazole" id="1" maj="2022-03-11T09:28:19" valeur="2151"/>
    <prix nom="SP95" id="2" maj="2022-03-11T09:28:19" valeur="2010"/>
    <prix nom="SP98" id="6" maj="2022-03-11T09:28:20" valeur="2127"/>
    <rupture id="3" nom="E85" debut="2017-09-16T09:50:23" fin=""/>
    <rupture id="4" nom="GPLc" debut="2017-09-16T09:50:23" fin=""/>
    <rupture id="5" nom="E10" debut="2018-12-13T09:49:49" fin=""/>
  </pdv>
  <pdv id="1000002" latitude="4621842" longitude="522767" cp="01000" pop="R">
    <adresse>16 Avenue de Marboz</adresse>
    <ville>BOURG-EN-BRESSE</ville>
    <services>
      <service>Vente de gaz domestique (Butane, Propane)</service>
      <service>DAB (Distributeur automatique de billets)</service>
    </services>
    <prix nom="Gazole" id="1" maj="2022-03-11T11:31:23" valeur="2290"/>
    <prix nom="SP95" id="2" maj="2022-03-11T11:31:24" valeur="2139"/>
    <prix nom="SP98" id="6" maj="2022-03-11T11:31:24" valeur="2010"/>
    <rupture id="3" nom="E85" debut="2017-09-07T11:05:59" fin=""/>
    <rupture id="4" nom="GPLc" debut="2017-09-07T11:05:59" fin=""/>
    <rupture id="5" nom="E10" debut="2017-09-07T11:05:59" fin=""/>
  </pdv>
</pdv_liste>

Now, I tried to create a sensor to retrieve the price of SP95 for station id 10000002.
I use a rest sensor to return this information but i could not set the correct value-template phrase.

can someone assist me ?

thanks a lot

Try this:

value_template: '{{ value_json[1]["prix"][1]["@valeur"] }}'

Thanks : this will work only if this ID is always at [1] but this is not the case (same for sp95).

I think I need to use “selectattr” but I don’t know how

Hi, seeing what you are trying to accomplish… there is an integration with the french state API…
May be easier (or you looked at it and want more :slight_smile: )
This one you can also easily change to your liking.
max5962/prixCarburant-home-assistant: Récupération des prix des stations en France (github.com)

Yes I have it but the integration update is done once per day and often price are not updated (the price change every day but not with this integration)

Other users have noticed the same thing.

The update sequence in the script states hourly (3600s) … I must admit that I am not driving to/by gas stations to see if it changed and when :slight_smile: I just checked this and darn…it does look quite different on prices. So my guess is that the basis of this implementation is incorrect (below). The one thing I noticed though is that the liste-instantanée does not show the station name…which I like. I will have a look at the source myself and see if this is easier to fix than rewriting the whole rest

ryann72/essence (github.com)

Yes, we should have the correct price.
Here, an example :slight_smile:

Station ID: '94170002'
Gasoil: '1.989'
Last Update Gasoil: '2022-03-15T13:33:00'
E95: None
Last Update E95: ''
E98: '2.034'
Last Update E98: '2022-03-15T13:33:00'
E10: '1.918'
Last Update E10: '2022-03-15T13:33:00'
E85: None
Last Update E85: ''
GPLc: None
Last Update GPLc: ''
Station Address: 264 Avenue du Général de Gaulle 94170 Le Perreux-sur-Marne
Station name: ESSO LE PERREUX
Last update: '2022-03-17'
unit_of_measurement: €
icon: mdi:currency-eur
friendly_name: PrixCarburant_94170002

The sensor is updated march 17th but for the price we have March 15th.

I just checked the source mentioned above and this is the culprit as it is pointing to the JOUR and not INSTANTANE. I am now trying to find out how to change this as it seems not that simple as just change the link…keep you posted

Update, got it fixed by incorporating the py extracting the data and modifying that to get the ‘instantane’

  • link changed
  • filename changed
  • value calc changed

Ideally this should be fixed in the source but since that extract is rather different, it would have to be added to that py script as a secondary option to not disturb other users/usage… So I forked it to my own github repo…let me know if you are willing to use it and in case of issues

Thanks I will try it right away and let you know

It works.

I will continue to monitor it.

It works but does not update on the scan_interval (3600s)…the source prixCarburant.py assumes a daily update and I missed that yesterday… I will continue to work on it

So… it took a while and the main reason is that each sensor can kick off the load of data. In the previous setup this means that it would download data from gouv.fr. per (!) sensor, download, unzip, treat, remove…I have 20 sensors…so thsi x20…a bit much.

I have now changed the lot

  • prixCarburantClient: along gouv.fr, details instantanés are refreshed every 10 mins: built-in check to verify filestamp and not download if <10mins
  • prixCarburantClient: with above the file(s) cannot be deleted anylonger, I moved folder/file into custom_components
  • sensor: update no longer checks on date as the data can be updated intra-day and intra-sensor e.g. Gasoil price could change in the morning and E86 i3 hours later…too many checks to perform.

With this, I see updates entering during the day now instead of only after full refresh / daily

I’ll check

Thanks… it was really a bit of an odd piece.
Off the shelf it did not work as the client only runs daily. As soon as this was removed, each individual sensor kicked off the same process. I thought about it for a few hours and came up with this one, certainly not ideal but I did not want to re-invent the wheel for the client. My personal preference would be to rewrite it to mqtt but I found that too much hassle for this…effort vs. value :slight_smile:
What I personally like is that I now have all elements in one folder, so anyone can tweak/alter it easily to their own liking without having to search for the client in the background

Yes I agree.
Thanks for your work, I really appreciate and really far far away of my competence. :pray:

No probs… I learnt on the fly (sort of my new ‘hobby’)… if issues, please leave them in the github ‘issues’ area (in French is also OK). Next steps for me is to include the distance (albeit simple) in the sensor which may help to determine if a detour actually makes sense. …will be vagueish as the current distance calc is just a translation of as-the-crow-flies…nothing fancy like google :slight_smile:

Yes, that will be very helpful