I’d like to get latest mazoutvoordeel.be prices in HA. They keep a json list with daily prices for Belgian oil/fuel.
The JSON result hosted by mazoutvoordeel looks like this: [{"label":"02\/01\/18","y":0.6065},{"label":"04\/01\/18","y":0.5824},{"label":"05\/01\/18","y":0.5805},{"label":"08\/01\/18","y":0.586},{"label":"09\/01\/18","y":0.5885},{"label":"10\/01\/18","y":0.5936},{"label":"13\/01\/18","y":0.5841},{"label":"15\/01\/18","y":0.5837},{"label":"16\/01\/18","y":0.5849},...,{"label":"23\/03\/22","y":1.2907}]]
With above value_template"{{value[-1:].y}}" I tried to fetch the latest y value of the json array. After some testing with jsonpath.com I expected this to work, but in HA I only get an empty value. With the json_attributes label and y, I only get the first (oldest) label and y value retrieved in the attributes of the HA entity.
Anyone some ideas how the last element of json array could be fetched? Or any other approach that could be used?
I’d like to use this in order to get some alerts when it would be a good time to buy some oil. And allow to visualize some graph’s on mazout pricing trends.
nevermind, I’ve found it myself (how come only after searching and testing a lot without success, suddenly after posting it in forum I tend to find the answer myself each time )
by using value_template: "{{value_json[-1:][0].y}}" I was able to fetch the latest entry of the array and get the desired y value element out of it.
Just for reference, if it would be useful to anyone, please find below my (currently) final working config:
Thanks. This is working. Unfortunately, it only shows today’s price.
On https://mazout.com/belgie/mazoutprijs they show the price of the next day, which is even more useful to decide if it’s a good moment to buy.
I tried to capture the data from this site via webscraping, but it does not work.
Anyone can check?
interesting, I’m looking into it and I might create a custom integration for it to load mazout & diesel prices from carbu.com. I’ll see how far I get in coming weeks, will keep you posted.
Hello,
It was a while I was looking for something like this.
Did not know about carbu_com.
I have installed and set my home post code.
A lot of people commute from home to work (or other places). Would there be a way to allow the setup to ask for post code for home and work. In this way one could create a card with petrol cost done near home and near work and always have at hand which is the most convenient.
I am attempting to set this up but do not have all the skill like you!
Hi Carlo,
I’m actually doing the same. You can add multiple locations with the carbu_com integration, so postal code of home and work can be added. You just need to repeat the same ‘integration’ setup of each location.
And then use a template sensor to extract the individual fields of the json. Plus: I can use the templates-section in the developer tools section to easily test the code.
Dear @myT, I have a question about your very useful carbu.com integration: how to write a valid regex expression in the configuration dialog to exclude a specific oil provider?
PS: reading this thread, fyi this is how I do to have the last element of a json: