REST sensor needs to get latest element of list

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}]]

So I’ve defined this rest sensor:

  - platform: rest
    name: Mazoutprijzen
    resource: https://www.mazoutvoordeel.be/nl/ajax/graphic-prices/
    value_template: "{{value[-1:].y}}"
    json_attributes:
      - label
      - y

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 :wink: )
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:

  - platform: rest
    name: Mazoutprijzen
    resource: https://www.mazoutvoordeel.be/nl/ajax/graphic-prices/
    value_template: "{{value_json[-1:][0].y}}"
    scan_interval: 3600
    device_class: monetary
    unit_of_measurement: "€/l"
    json_attributes_path: "$[-1:]"
    json_attributes:
      - label
      - y
    state_class: measurement
2 Likes

It is the aura of the forum… it does tend to let you think differently by trying to think how to describe what you really need…good find!

1 Like

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?


multiscrape:
  - name: mazoutprijs
    resource: https://mazout.com/belgie/mazoutprijs
    scan_interval: 3600
    sensor:
      - name: mazoutprijs_morgen_minder_2000L
        select: "#price_table_3945434934 > rc-http > div > div > price-table > table > tbody > tr:nth-child(1) > td:nth-child(4)"

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.

1 Like

FYI, first release of my custom integration for carbu.com is available, including diesel, super 95, fuel oil prices, predictions and best prices at local stations in 5km and 10km region. See full instructions at GitHub - myTselection/Carbu_com: HACS integration to Carbu.com site to compare and save on your fuel oil, diesel and Super purchases

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.

I’m trying something similar to get the last value of the following json:

[
	{
		"station_name": "name",
		"rows": "192",
		"data": [
			[
				"2024-01-27T23:30:00.000+01:00",
				253
			],
			...
			[
				"2024-01-29T23:15:00.000+01:00",
				226
			]
		]
	}
]

I’m currently using the following sensor:

sensor:
    - platform: rest
      resource: link_to_json

rest:
    - resource: link_to_json
      scan_interval: 900
      sensor:
        - name: sensor_name
          unique_id: my_id
          value_template: "{{ value_json.0.data.191.1 }}"
#          value_template: "{{ value_json.0.data[-1:].1 }}"
#          value_template: "{{ value_json.0.data.[-1:].1 }}"
          device_class: distance
          unit_of_measurement: "cm"
          state_class: measurement

However, when I use the [-1:] syntax to grab the last data-element, the sensor seems not to work (unknown value).

Anyone has an idea what goes wrong here?

Having looked more into it, I actually found a way that also solves the issue state max length is 255 characters using the approach described by @123:

sensor:
    - platform: rest
      resource: link_to_json
      scan_interval: 900
      name: sensor_name
      json_attributes_path: "$.[0]"
      value_template: '1'
      json_attributes:
        - "station_name"
        - "rows"
        - "data"

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.

In Developer tools > Templates you can try some jinja2 syntaxes:

{% set value_json = [{ "station_name": "name", "rows": "192", "data": [ [ "2024-01-27T23:30:00.000+01:00",253],["2024-01-29T23:15:00.000+01:00",226]]}] %}
{{value_json}}
value_template: "{{ value_json.0.data }}"
value_template: "{{ value_json.0.data.1.0 }}"
value_template: "{{ value_json.0.data.1.1 }}"
value_template: "{{ value_json.0.data[-1:].0.0 }}"
value_template: "{{ value_json.0.data[-1:].0.1 }}"

Result:

[{'station_name': 'name', 'rows': '192', 'data': [['2024-01-27T23:30:00.000+01:00', 253], ['2024-01-29T23:15:00.000+01:00', 226]]}]
value_template: "[['2024-01-27T23:30:00.000+01:00', 253], ['2024-01-29T23:15:00.000+01:00', 226]]"
value_template: "2024-01-29T23:15:00.000+01:00"
value_template: "226"
value_template: "2024-01-29T23:15:00.000+01:00"
value_template: "226"

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:

{% set lst = (value_json.0.data | last) %}
value_template: "{{ lst.0 }}"
value_template: "{{ lst.1 }}"

EDIT: I just realised, and tested, that the list jinja filter was not even needed before the last one (hence removed from above code)
This also works:


value_template: "{{ (value_json.0.data | last).0 }}"
value_template: "{{ (value_json.0.data | last).1 }}"
1 Like