pantherale0 / ha-fuelprices

A generic and extensible integration with pyfuelprices to retrieve local (or remote) fuel price data.
MIT License
16 stars 2 forks source link

X lowest prices for specific type of gas around a location #6

Closed poudenes closed 2 months ago

poudenes commented 8 months ago

Checklist

Is your feature request related to a problem? Please describe.

Not related to a problem

Describe the solution you'd like

Create sensors. Imagine you want 5 lowest prices

sensor.lowest_price_1_home sensor.lowest_price_2_home sensor.lowest_price_3_home sensor.lowest_price_4_home sensor.lowest_price_5_home

sensor.lowest_price_1_work sensor.lowest_price_2_work sensor.lowest_price_3_work sensor.lowest_price_4_work sensor.lowest_price_5_work

Compare the data and put lowest station in 1, then the next one in 2 etc.

This make it possible to add them in a map

  - type: map
    entities:
      - entity: sensor.gas_station_euro95_home_lowest_price_1
        name: LP 1
      - entity: sensor.gas_station_euro95_home_lowest_price_2
        name: LP 2
      - entity: sensor.gas_station_euro95_home_lowest_price_3
        name: LP 3
      - entity: sensor.gas_station_euro95_home_lowest_price_4
        name: LP 4
      - entity: sensor.gas_station_euro95_home_lowest_price_5
        name: LP 5
    geo_location_sources: []
    auto_fit: true

Describe alternatives you've considered

none

Additional context

none

pantherale0 commented 8 months ago

Sounds good, perhaps we could look at just creating a single lowest price entity? Then we can use the attributes to store the rest. I think that would provide a cleaner look anyway. What do you think? You would still be able to automate off of state changes to the attributes and create template sensors for something more advanced.

I might move all of the entities over to the sensor platform to be honest anyway, I'm not really using the device_tracker platform how its supposed to be used lol.

poudenes commented 8 months ago

That sounds nice as well. One sensor with lowest price as state. And 4 others in attributes. But how will you store all the information that belong to the stations then?

source_type: gps
E10: 1.969
B7: 1.769
E5: 2.159
id: directlease_3097
name: Buitenveldert (wz)
address: Parnassusweg 850
postal_code: 1081LC
latitude: 52.33594
longitude: 4.86848
brand: BP
available_fuels:
  E10: 1.969
  B7: 1.769
  E5: 2.159
currency: EUR
last_updated: "2024-01-06T13:40:34.262640"
next_update: "2024-01-07T13:55:35.093557"
friendly_name: Buitenveldert (wz)

This will be a very long attribute list if you separate them with a lowest1 lowest2 before every attribute. Then 5 different sensors make more sense to keep the information clean for every station.

pantherale0 commented 8 months ago

Very good point, didn't think of that. I'll have a look. Always great having another opinion, thank you 👍

pantherale0 commented 7 months ago

You can potentially use a template entity to help with this:

{%- set devices = integration_entities('fuel_prices') %}
{%- set data = namespace(fuel=[]) %}
{%- set fuel_type = 'B7' %}
{%- for device in devices %}
  {%- set data.fuel = data.fuel + [{"id": device, "name": states(device), "price": state_attr(device, fuel_type)}] %}
{%- endfor %}
{%- set data = (data.fuel | sort(attribute='price')) %}

Change fuel_type to whatever fuel you want to target and then the output of data will be a sorted list with 3 properties, entity id, name of fuel station and price. To access cheapest use data[0] for example.

The integration isn't currently setup for this type of entity and would require a new type of entity handler for completely dynamic data. For now this will go on the backburner while I sort some personal stuff out.

pantherale0 commented 2 months ago

coming soon :smiley:

image

at the moment very basic as the module does not usually return any fuel station information when the relevant python apis are called.