Fuel/Gas Prices integration

The python script is receiving the output from the fuel_prices.find_fuels action. The template isn’t rendered until the end of the python script.

I’ve added an option to restrict the number of results.

Try this:

Remember to set your radius, fuel type, tracking device.
You can set the number of results by changing max_results =

Set it to 5, and see what happens.

scripts.yaml:

find_fuel:
  sequence:
    - action: input_text.set_value
      target:
        entity_id: input_text.fuel_prices_data
      data:
        value: reloading
    - data:
        type: E10
        location:
          latitude: |
            {{ state_attr('device_tracker.my_device', 'latitude') | float }}
          longitude: |
            {{ state_attr('device_tracker.my_device', 'longitude') | float }}
          radius: |
            {{ 10 * 1609.34 }}
      response_variable: data
      action: fuel_prices.find_fuels
    - if:
        - condition: template
          value_template: "{{ (data.fuels | count) > 0 }}"
      then:
        - variables:
            response: |
              {{ { 'value': data.fuels } }}
        - service: python_script.set_fuel_prices
          data:
            max_results: 0 # Set to 0 for all results
            fuels: "{{ data.fuels }}"
        - stop: ''
          response_variable: response

set_fuel_prices.py

fuels = data.get('fuels', [])

fuel_strings = [f"{fuel['distance']:.1f} mi - {fuel['brand']}: {fuel['cost']:.2f}" for fuel in fuels]
fuel_postcodes = [f"{fuel['postal_code']}" for fuel in fuels]
max_length = 255
max_results = data.get('max_results')

restricted_fuel_strings = []
final_fuel_postcodes = []
length_so_far = 0

i = 0

for fuel_string in fuel_strings:
    additional_length = len(fuel_string) + (2 if restricted_fuel_strings else 0)
    if length_so_far + additional_length <= max_length:
        restricted_fuel_strings.append(fuel_string)
        final_fuel_postcodes.append(fuel_postcodes[i])
        length_so_far += additional_length
        if max_results != 0 and i == (max_results - 1):
            break
        else:
            i += 1
    else:
        break

fuel_data_str = ", ".join(restricted_fuel_strings)
fuel_postcode_str = ",".join(final_fuel_postcodes)

hass.services.call('input_text', 'set_value', {
    'entity_id': 'input_text.fuel_prices_data',
    'value': fuel_data_str
})

hass.services.call('input_text', 'set_value', {
    'entity_id': 'input_text.fuel_postcodes',
    'value': fuel_postcode_str
})

Appreciate your perseverence with this. Made the changes and now get the following error:

Action python_script.set_fuel_prices not found.

I have confirmed that the syntax that you provided is in set_fuel_prices.py under the pyscript folder

pyscript or python_script? These folders are for different integrations

1 Like

Folder should be called python_scripts.

Hi guys, I saw this integration last week and immediately installed it. I use the directlease (Dutch) provider to give me the locations and prices 2 miles from my house. I noticed however that the prices don’t get updated automatically. Even a reload of the integration isn’t working. The prices only get updated after a HA reboot. Does anyone have a solution for this issue?

For DirectLease, see here:

Basically, the poll interval has been set so low that they have probably blocked your connection, currently no way around this.

DirectLease will be temporarily removed in the next version because of this. However I have been trying to work with them to get something up and running. Not heard back now for a week or so.

1 Like

Thanks for your effort.

Sorry but still having issues with this.

Can run each of the 3 fuel associated actions within developer actions screen but I do need to reduce the length manually of the longtitude/latitude by 1 numeric and then put that numeric back in for it to work. Error is

This action requires field location, please enter a valid value for location

Then the following actions run individually without issue

Fuel Prices: Find fuel stations from location
Fuel Prices: Find fuel prices from location
Fuel Prices: force_update

When I try to run the final report I get the following error

Action script.find_fuel not found.

The syntax that makes up find_fuel is correctly set up in scripts.yaml.

Appreciate any further assistance with this. Many thanks.

Can you post your find_fuels script here?
Sounds like there’s an error in there somewhere…

Fuel syntax is below as requested;

find_fuel:
  sequence:
    - action: input_text.set_value
      target:
        entity_id: input_text.fuel_prices_data
      data:
        value: reloading
    - data:
        type: U91
        location:
          latitude: |
            {{ state_attr('device_tracker.life360_zzz_zzzzzzz, 'latitude') | float }}
          longitude: |
            {{ state_attr('device_tracker.life360_zzz_zzzzzzz', 'longitude') | float }}
          radius: |
            {{ 5 * 1609.34 }}
      response_variable: data
      action: fuel_prices.find_fuels
    - if:
        - condition: template
          value_template: "{{ (data.fuels | count) > 0 }}"
      then:
        - variables:
            response: |
              {{ { 'value': data.fuels } }}
        - service: python_script.set_fuel_prices
          data:
            max_results: 0 # Set to 0 for all results
            fuels: "{{ data.fuels }}"
        - stop: ''
          response_variable: response
          

You’re missing the closing single quotation on the latitude entity.

should be:

{{ state_attr('device_tracker.life360_zzz_zzzzzzz', 'latitude') | float }}

Thanks for finding the missing apostrophe. I now am back getting the error

Failed to perform the action script/find_fuel. Template output exceeded maximum size of 262144 characters

Have rechecked that the syntax held in file set_fuel_prices.py matches what you provided in post 181. Sorry that I have to keep coming back to you on this.

Do you have the python script in the correct directory now?
Should be in python_scripts. You’ll need to reload python scripts from developer tools.

You should be able to find the python script in Developer Tools → Actions.
Script is called set_fuel_prices.

If that is showing up in the list, then try setting the max_results to 1 in the find_fuels script, reload scripts, and see if that returns the same error.

The file set_fuel_prices.py is in the directory python_scripts. I do not see the python script set_fuel_prices in the Action selection of Developer tools. I have the following available to me from this integration

Fuel Prices: Find fuel stations from location
Fuel Prices: Find fuel prices from location
Fuel Prices: force_update
Scripts: find_fuel

When I run the Scripts: find_fuel I get the error
Failed to perform the action script/find_fuel. Template output exceeded maximum size of 262144 characters

Have you added the python script integration?
Double check your configuration.yaml and make sure the following line is in there:

python_script:

Like I’ve said before, this is because the template is internally rendered when passing the data to the python script. There is a maximum buffer of 256kb in Home Assistant for this.

Lower the radius or limit the data that is being sent to the python script with

{{ data.fuels[:10] }}

This will select the top 10 results.

But the template isn’t rendered until after the python script runs?
The python script populates the sensors that the template pulls the data from.

It’s still rendered internally to send the data from the template to the script… Otherwise the script would only ever see {{ data.fuels }} and not the actual value.

just updated to HASS OS 14.2 and the integration has stopped loading

I do not recall having to input an API on setup…am,I missing something here

What data sources are you using? What is in the logs?