I wanted to contribute a guide with my own implementation of this after many hours of trial and error.
Why this method?
- Combines fuel types into a single sensor for a fuel station.
- Easily add more stations or fuel types in your configuration
- Once set up add new station in only 2 lines (Just entity name and state needed to add another sensor)
- No manual helpers needed to access large amounts of data
- Clean configs
- Add another station in seconds
- Configures icon to gas station from config
- Configures unit of measurement to ‘¢/L’ or ‘$/L’
Downloading the data
As always you need a “Data consumer” API KEY from fuelpricesqld.com.au which you will put in your secrets.yaml
as FUEL_API_TOKEN: FPDAPI SubscriberToken=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
I am using @thechancer 's code for getting the JSON data into home assistant.
configuration.yaml:
rest:
- resource: https://fppdirectapi-prod.fuelpricesqld.com.au/Subscriber/GetCountryFuelTypes?countryId=21
headers:
Authorization: !secret FUEL_API_TOKEN
Content-Type: application/json
sensor:
- name: fuel_types
value_template: "fuels"
json_attributes:
- Fuels
scan_interval: 86400
- resource: https://fppdirectapi-prod.fuelpricesqld.com.au/Subscriber/GetFullSiteDetails?countryId=21&geoRegionLevel=1&geoRegionId=XYZ
headers:
Authorization: !secret FUEL_API_TOKEN
Content-Type: application/json
sensor:
- name: fuel_sites
value_template: "sites"
json_attributes:
- S
scan_interval: 86400
- resource: https://fppdirectapi-prod.fuelpricesqld.com.au/Price/GetSitesPrices?countryId=21&geoRegionLevel=1&geoRegionId=XYZ
headers:
Authorization: !secret FUEL_API_TOKEN
Content-Type: application/json
sensor:
- name: fuel_prices
value_template: "prices"
json_attributes:
- SitePrices
scan_interval: 300
NOTE: If copying this data, change the geoRegionId from XYZ to your suburb, or change geoRegionLevel from 1 (suburb level) to 2 (city level) or 3 (state level) data collection
Creating the JINJA template to avoid re-using code:
A JINJA template allows you to re-use redundant (identical) code, this reduces the code from 5-10 lines to 1 line in EACH ATTRIBUTE that only shows the data that needs to be edited, I cannot overstate enough how much cleaner and less redundant this made my config.
This is mine located at /config/custom_templates/fuel.jinja
:
{% macro fuel_price(siteID, fuelID) %}
{{ state_attr('sensor.fuel_prices', 'SitePrices') | selectattr('SiteId', '==', siteID) | selectattr('FuelId', '==', fuelID) | map(attribute='Price') | first | float / 10}}
{% endmacro %}
The macro receives an input of siteID and fuelID (values from the API, use POSTman to get these values easily), selects the correct data, takes the 4 digit price value (e.g 1234) and divides by 10 to give you cents per litre (123.4¢/L), you can alternatively change the division to 1000 to give you dollars per litre ($1.234/L).
(RECOMMENDED) Create a sensor for a combined fuel station with multiple fuel types as attributes:
I created template sensors neatly using the previous JINJA template in sensors.yaml
(or configuration.yaml
if you have not separated your sensors into a separate file).
Change all instances of siteId to the same fuel station, change the fuelId in the state line to the fuel type you primarily fill with and change the FuelTypeX and fuelId to fuels you are interested in from that station.
template:
- sensor:
- name: "Fuel Station Name"
icon: mdi:gas-station
state: "{% from 'fuel.jinja' import fuel_price %}{{ fuel_price(siteId, fuelId) }}"
unit_of_measurement: '¢/L'
attributes:
FuelType1: "{% from 'fuel.jinja' import fuel_price %}{{ fuel_price(siteId, fuelId }}"
FuelType2: "{% from 'fuel.jinja' import fuel_price %}{{ fuel_price(siteId, fuelId) }}"
EXAMPLE
sensors.yaml
, where siteId is 61401118 (7/11 Vulture Street), the primary fuelIDs is 12 (e10), and also includes 5 (Unleaded 95), 8 (Unleaded 98), and 3 (Diesel) as attributes:
template:
- sensor:
- name: "7eleven East Brisbane (Vulture Street)"
icon: mdi:gas-station
state: "{% from 'fuel.jinja' import fuel_price %}{{ fuel_price(61401118, 12) }}"
unit_of_measurement: '¢/L'
attributes:
E10: "{% from 'fuel.jinja' import fuel_price %}{{ fuel_price(61401118, 12) }}"
U95: "{% from 'fuel.jinja' import fuel_price %}{{ fuel_price(61401118, 5) }}"
U98: "{% from 'fuel.jinja' import fuel_price %}{{ fuel_price(61401118, 8) }}"
Diesel: "{% from 'fuel.jinja' import fuel_price %}{{ fuel_price(61401118, 3) }}"
NOTE: The siteID MUST be within the data you downloaded (for me this was GeoRegionId as my suburb). The sensor will fail if the site is not downloaded
Create a sensor for a fuel station’s individual fuel type:
This option is if you:
- Only care about a single fuel type (E.g if you only fill up with E10)
- Want to use a card that does not allow you to select attributes, only states
- If you want to create Template sensors in the GUI instead of yaml
- Prefer one sensor for each fuel station’s fuel type
NOTE: This can get messy if you track lots of stations or fuels using this method. An example helpers list could include: Fuel Station 1 E10, Fuel Station 1 U95, Fuel Station 1 E98, Fuel Station 1 Diesel, Fuel Station 2 E10, ... etc.). You are essentially creating a helper for a single station's single fuel type
{% from 'fuel.jinja' import fuel_price %}{{ fuel_price(61401118, 12) }}
Summary
In my sensors.yaml I am using 26 lines to track 4 fuel types from 3 stations. That show up as helpers for each station with the primary fuel type as the state and 3 alternate fuel types in the attributes which can be exposed using the Tile Card.
To track another station, copy the sensor definition from - name:
and place it below the one above it, then change the siteID in each of the new lines. Very quick and easy to add new stations.
Let me know if there are any issues or errors, but this is working perfectly for me.