REST sensor needs to get latest element of list

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.



I checked this issue: it’s caused by non fully matching postalcode with the given town: 8380 is town Dudzele, while Brugge was set. 8380 is a sub-municipality of Brugge.
I now made a new release (R11.2.5) that would also match town names with the sub-municipality to cover this issue.

It works perfect now. Stored the values of the notification in a input_text and Used this in a script to start the navigation directly when the script is toggled from the android auto app. Maybe this could help someone in the future, so here is the yaml.

For the input text (1for displaying info, 1 for navigating):

alias: Test Carbu
description: Notify when the lowest fuel price is retrieved and update input_text.
triggers:
  - event_type: carbu_com_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

The script to trigger from the dashboard:

alias: Fuel price Area 5km 
sequence:
  - action: carbu_com.get_lowest_fuel_price
    data:
      fuel_type: super95
      country: BE
      postalcode: "{{ state_attr('sensor.gsm_benji_geocoded_location', 'postal_code') }}"
      town: "{{ state_attr('sensor.gsm_benji_geocoded_location', 'locality') }}"
      max_distance: 5
  - delay:
      hours: 0
      minutes: 0
      seconds: 15
      milliseconds: 0
  - data:
      message: command_activity
      data:
        intent_package_name: com.google.android.apps.maps
        intent_action: android.intent.action.VIEW
        intent_uri: >-
          google.navigation:q={{ states('input_text.carbu_coord') |
          replace('Address: ', '') }}&mode=d
    action: notify.mobile_app_gsm_benji
description: 

Thank you for the help and for the great integration.