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

Hi! First of all thank you for the Carbu integration!
I am trying to get the postalcode from my device tracker and use this as an imput to get the data from Carbu.
So I try to add a new setup and enter the template code to extract the postalcode attribute from my device tracker, so far so good. I’m able to go to the next form, where I am asked for the town. But I’m not able to type or select anything. What am I missing? I have set up 2 different location based on a fixed postalcode and those 2 are working perfect.

Can you point me in the wright directions. Thnx!

Dear @BenjiMarlies ,
Are you aware the carbu.com integration also provides services which can be used to integrate dynamic locations: carbu_com.get_lowest_fuel_price_coor
This will get the lowest fuel price around a provided coordinate (lat/lon). (It will require the geoapify.com API key to convert it back into postal code)

With regards to the specific issue you encounter: could you share the country and or postal code that would get this issue? as each country has specific behavior in the setup config flow…

Hi!

Thank you for the answer. I am aware that there is a service call and I tried different ways but with no luck.

In the screenshot you can find the data I use.
When I check the response in Template it returns a value for each of the attributes.

Hope this helps narrowing it down a bit. When I use the integration with a fixed postal code it all runs perfect.
Thanks again!

this was actually cause by a bug, it has been fixed now in R11.2.3

Works perfect now. :pray:

When I use the event carbu_com.get_lowest_fuel_price_coor the automation works perfect.

When I try to use the same automation with the event: get_lowest_fuel_price
The automation doesn’t return anything.

This is the automation I use:

alias: Test Carbu
description: Notify when the lowest fuel price is retrieved and update input_text.
triggers:
  - event_type: "Carbu.com: get_lowest_fuel_price"
    trigger: event
conditions: []
actions:
  - target:
      entity_id: input_text.carbu_coord
    data:
      value: |
        Address: {{ trigger.event.data.address }}
    action: input_text.set_value
  - target:
      entity_id: input_text.carbu_info
    data:
      value: |
        Supplier: {{ trigger.event.data.supplier }} 
        Price: {{trigger.event.data.price }}€
        Distance: {{ trigger.event.data.distance }}km
        Address: {{ trigger.event.data.address }}
    action: input_text.set_value
  - data:
      message: >-
        Supplier: {{ trigger.event.data.supplier }}  Price:
        {{trigger.event.data.price }}€ Distance: {{ trigger.event.data.distance
        }}km Address: {{ trigger.event.data.address }}
    action: notify.persistent_notification
mode: single

Am I missing something?

Grtz

event_type should be:

event_type: carbu_com_lowest_fuel_price

full example, see: https://github.com/myTselection/Carbu_com > “Example automation triggered by event”

I have tried every example possible on your github page. I don’t even need diesel but I tried.
The event just doesn’t fire when I put in my geocoded attributes. When I fire it with a preset Locality and Postcode, it returns data.

The geocoded attributes are correct as you can see in the template editor and everything is up to date.