I wanted a way to track gas prices in Home Assistant so I can keep an idea on the trend of fuel prices if they are going up or down so I can know when a good time to go fill up is. The first place I found to fetch this data is a website called Gas Buddy which is like a crowd sourced database of gas prices. The quality of the data can definitely vary but there are usually a fuel station or two around that have reliable people contributing so I started there.

The Gas Buddy uses a very simple GraphQL backend for it's own website so it was pretty easy to dig through that and find the relevant calls. The awesome part is there is no registration required so no need to setup an account or anything else. After doing some digging and hacking around I ended up with a Home Assistant sensor using the REST platform to fetch the relevant data from a local station.

- platform: rest
  unique_id: gasbuddy_gas_station_circlek
  name: Circle K Diesel Price
  icon: mdi:gas-station
  scan_interval: 3600
  resource: https://www.gasbuddy.com/graphql
  method: POST
  headers:
    Content-Type: application/json
  payload: >
    {
      "operationName": "GetStation",
      "variables": {
        "id": "12345"
      },
      "query": "query GetStation($id: ID!) { station(id: $id) { fuels prices { cash { postedTime price } credit { postedTime price } } } }"
    }
  unit_of_measurement: USD
  value_template: >
    {% set vars = namespace(price=None) %}
    {% if value_json.data.station.prices.3.cash != None
      and value_json.data.station.prices.3.cash.price != None 
      and value_json.data.station.prices.3.cash.price > 0 %} 
      {% set vars.price = value_json.data.station.prices.3.cash.get('price') %}
    {% endif %}
    {% if value_json.data.station.prices.3.credit != None
      and value_json.data.station.prices.3.credit.price != None
      and value_json.data.station.prices.3.credit.price > 0
      and (vars.price == None or value_json.data.station.prices.3.credit.price < vars.price) %}
      {% set vars.price = value_json.data.station.prices.3.credit.price %}
    {% endif %}       
    {{ vars.price }}
  json_attributes_path: "$.data.station"
  json_attributes:
    - fuels
    - prices

You will need to update a couple of things in here to match what you might need:

  1. The id variable to match the station you want to monitor which you can find through their website and developer tools in your browser
  2. The position of the prices array. They are in a zero indexed array in the order. It seems like it's always in Regular, Plus, Premium, Diesel order but it might vary on that fuel station.

Plug that into your Home Assistant sensors yml configuration and you should be getting data!