UK Fuel Finder

Excellent! I did it as a script in python as it just ran in the background and updated a fairly simple command_line sensor - but top job on the integration! Love it!!!

The beecho01 version uses the old data sources so thats why I wrote the python version.

I’ve added a few more pops, bells and whistles to the python script in the meantime - now it will:

  • automatically do full UK station cache management
  • incremental station and price updates
  • auto-fixing of obviously incorrect data submitted by stations
  • auto-refresh data when needed
  • handle multiple sensors
  • multiple fuel type reporting
  • API backoff and delayed pulls
  • query by station ID, station name and brand (supports regex)
  • cache health check
  • debug options
  • unified JSON output structure
  • allow you to force rebuild the cache

You can just setup the query you want to run and the script will manage everything in the background each time it runs - updating whatever it needs to update as it goes. Set and forget style.

I’ve put it up on GitHub if anyone wants to play (it doesn’t need Home Assistant to be useful).

I add this line to configuration.yaml:

command_line: !include command_line.yaml

… then this in command_line.yaml in the /config directory:

# Petrol Prices
- sensor:
    name: "Local Petrol Prices"
    unique_id: local_petrol_prices
    scan_interval: 3600
    command_timeout: 900
    command: "python3 /config/scripts/uk_fuel_finder.py  --lat 52.2345 --lon -1.82645 --radius-miles 8 --fuel-types e10"
    value_template: "{{ value_json.state }}"
    icon: "{{ value_json.details.icon }}"
    state_class: measurement
    json_attributes:
      - mode
      - found
      - errors
      - details
      - stations
      - last_update

And this markdown card to display prices:

  - type: markdown
    title: Local Fuel Prices
    content: >-
      {%- if states('sensor.local_petrol_prices') | int | default(0, true) >= 1
      -%}
        <center>
        <table width="100%">
        {%- for station in
             state_attr('sensor.local_petrol_prices', 'stations')
             | selectattr('e10_price', 'defined')
             | sort(attribute='e10_price')
        -%}
          <tr>
            <td>{{ station.brand.split(' ')[0] | title }}, <font size=2>{{ station.name | title }}</font></td>
            <td><a href="https://waze.com/ul?ll={{ station.lat }}%2C{{ station.lon }}&navigate=yes&zoom=17">{{ station.postcode | upper }}</a></td>
            <td align="right">{{ station.e10_price }}p</td>
          </tr>
          <tr>
            <td colspan="3"><font size=1 color="grey">Open: {{ iif(station.open_today is not none, station.open_today, "NA") }}, Dist: {{ station.distance_miles }} miles, Updated: {{ station.e10_updated | as_timestamp | timestamp_custom('%a %-d %b %Y %H:%M', true, 0)}}</font></td>
          </tr>
        {%- endfor -%}
        </table>
        </center>
        <p><font size=1 color="grey">Updated: {{ state_attr('sensor.local_petrol_prices', 'last_update') | as_timestamp | timestamp_custom('%a %-d %b %Y %H:%M', true, 0)}}</font></p>
      {%- else -%}
        <center>No Fuel Prices Available</center>
      {%- endif -%}
    card_mod:
      style:
        ha-markdown $:
          ha-markdown-element: |
            td {
              border: none !important;
              padding: 0px !important;
            }

… form which you get something like (where the postcodes are active links to Waze to direct you to the station - just for fun):