Support the insertion of variables in the "resource" field of REST sensor

As a user, I can pass a variable { input_text.some_text} in the resource field of the REST sensor in my configuration.yaml so that I can make the integration more dynamic with HA

Below is a concrete example:

Current situation:

I am having a REST sensor that fetches the information about the gasoline station in a certain radius around my home’s position where the price is the most affordable.
I can’t seem to be able to pass any variable in the query parameters of the resource endpoint.

- platform: rest
  name: Cheapest Diesel around me
  unique_id: cheapest_diesel_around_me
  resource: https://data.economie.gouv.fr/api/records/1.0/search/?dataset=prix-carburants-fichier-instantane-test-ods-copie&q=&sort=-prix_valeur&facet=id&facet=adresse&facet=ville&facet=prix_maj&facet=prix_nom&facet=com_arm_name&facet=epci_name&facet=dep_name&facet=reg_name&refine.prix_nom=Gazole&geofilter.distance=XXX%2CYYY%2C5000
  json_attributes_path: "$.['records'][0].fields"
  json_attributes:
      - adresse
      - com_name
      - dep_name
      - geom
  value_template: "{{ value_json['records'][0].fields.prix_valeur }}"
  unit_of_measurement: "€"

Expected situation:

I would like to be able to do something a bit like this (see the end of the resource URL)

- platform: rest
  name: Cheapest Diesel around me
  unique_id: cheapest_diesel_around_me
  resource: https://data.economie.gouv.fr/api/records/1.0/search/?dataset=prix-carburants-fichier-instantane-test-ods-copie&q=&sort=-prix_valeur&facet=id&facet=adresse&facet=ville&facet=prix_maj&facet=prix_nom&facet=com_arm_name&facet=epci_name&facet=dep_name&facet=reg_name&refine.prix_nom=Gazole&geofilter.distance=<MY_VARIABLE>
  json_attributes_path: "$.['records'][0].fields"
  json_attributes:
      - adresse
      - com_name
      - dep_name
      - geom
  value_template: "{{ value_json['records'][0].fields.prix_valeur }}"
  unit_of_measurement: "€"

In the above, I am adding a variable as the value of the query parameter “geofilter.distance” so that I can adjust the value in Home Assistant, and have the sensor respond accordingly.

For example, I can easily adjust the radius (in meters) around which I am looking the cheapest gasoline station
XXX%2CYYY%2C5000 => XXX%2CYYY%2C10000

That would be really grateful of the REST integration properly supports variables like this.
Thanks

Don’t forget to vote for your own request.

Apologies if I’m missing something here, but shouldn’t you use resource_template instead of resource?

resource_template: "http://example.com?some_param={{ states('sensor.something') }}"

Yes you can. I was confusing this with the payload option that can’t be templated.

1 Like