Check whether fuel prices fall below a certain value

Hello,

I am complety new to Homeassistant. Perhaps someone can help me with my problem.

I get the current fuel prices from tankerkoenig.
now I try to notify myself when the fuel price falls below 1.40 €.

i made a binary_sensor to check and set it Alert or Normal.

the actually state of example “sensor.tankerkoenig_freie_tankstelle_diesel” is : 1.399

- platform: template
    sensors:
      billigster_spritpreis:
        value_template: 
          '{% if states.sensor.tankerkoenig_freie_tankstelle_diesel.state | int < 1.4  %}
            Alert
          {% else %}
            Normal
          {% endif %}'
        friendly_name: 'billigster_spritpreis'

but he always shows me as state of “billigster_spritpreis” : “off”

I think I’m making a mistake in the if condition?

thank you

Binary sensor templates must resolve to true or false.

Also you have made a multi-line template. These must have the multi-line indicator > and must not be quoted, unlike single line templates which must be quoted.

Also read the warning here. Use the states() method when you can.

Use the |float filter if your number has a floating point part (right of the decimal point), use |int when your sensor is an integer and has no decimal part (or you want to disregard it).

- platform: template
    sensors:
      billigster_spritpreis:
        value_template: >
          {{ states('sensor.tankerkoenig_freie_tankstelle_diesel') | float < 1.4  }}
        friendly_name: 'billigster_spritpreis' # this can have spaces and capital letters is you want.

This will have the states on and off, like all binary sensors. You can change how this is displayed in Lovelace by applying a device_class to the sensor config.

This is also the legacy format for binary template sensors. You should look at using the new format.

template:
  - binary_sensor:
      - name: 'Billigster Spritpreis'
        state: >
          {{ states('sensor.tankerkoenig_freie_tankstelle_diesel') | float < 1.4  }}
        device_class: problem # as an example

If you want an entity with the states Alert and Normal you will have to create a template sensor instead of a template binary sensor.

1 Like

thank you for your time and answer, i read the linked pages. to have a better understanding of homeassistant.

i made a few days before a group, there are 3 prices of 3 gas stations in there.
is it possible to put all the 3 gas stations to one sensor or must i use 3 templates?

Rather than a sensor use an automation to alert you

trigger:
  - platform: numeric_state
    entity_id: sensor.tankerkoenig_freie_tankstelle_diesel
    below: 1.4
  - platform: numeric_state
    entity_id: sensor.your_second_sensor
    below: 1.4
  - platform: numeric_state
    entity_id: sensor.your_third_sensor
    below: 1.4
action:
  service: notify.notify
  data:
    title: "Fuel Alert"
    message: "{{ trigger.to_state.name }} is below 1.40 €."

i use telegram for notifications, anyways when i try to save the automation, i get a error

Error: Error rendering data template: UndefinedError: ‘dict object’ has no attribute ‘to_state’

is this because i start the automation to test? he has no trigger name?

Yes. You must adjust one of the sensors (do this in developer tools / states). You cant trigger it manually.

Also you must have a notification service configured.

1 Like

okay thats works fine, now i want to send the location (latitude,longitude) with the automation.

i testet it with this :

service: notify.ha_messenger
data:
    title: Send location
    message: Location updated.
    data:
      location:
        latitude: {{ trigger.to_state.latitude }}
        longitude: {{ trigger.to_state.longitude }}

my sensor from tankerkoenig gives me this back :

attribution: 'Data provided by https://creativecommons.tankerkoenig.de'
brand: ARAL
fuel_type: diesel
station_name: Aral Tankstelle
street: Bamenohler Straße
house_number: '113'
postcode: 57413
city: Finnentrop
latitude: 51.1684723
longitude: 7.977023
is_open: true
unit_of_measurement: €
friendly_name: Aral Bamenohl
icon: 'mdi:gas-station'

is it not possible over “{{ trigger.to_state.latitude }}” ?
i tried “latitude: {{ trigger.to_state.attributes.latitude }}” too, but no luck.

i get no message when i use the code, when i add random numbers it works.

THX

EDIT

i found the Problem, i forget the ‘’
the correct code must be

latitude: '{{ trigger.to_state.attributes.latitude }}'
        longitude: '{{ trigger.to_state.attributes.longitude }}'

You need to access the sensor attribute, which you can do via the trigger entity id:

service: notify.ha_messenger
data:
    title: Send location
    message: Location updated.
    data:
      location:
        latitude: "{{ state_attr(trigger.entity_id, 'latitude') }}"
        longitude: "{{ state_attr(trigger.entity_id, 'longitude') }}"

The available trigger variables for each type of trigger platform are listed here:

1 Like

thank you, i edited my last post, i get it to work by another way, is your way better?

Your way is fine too.

1 Like

helllo

i have a another question, i use a new automation to answer my telegram to a /update command, he should send me the station_name of a sensor. I tried many methods, this is my last try, but nothing worked.

states.sensor.tankerkoenig_freie_tankstelle_diesel.station_name