Template Value Sensor stop working. Value is "unknown"

Glad to hear you like it.

You may be interested to know that I have another solution that I feel works more efficiently. It pre-processes the contents of the URL using jq (a command-line JSON processor). It means that the sensor will receive only the data you want (for gasolinera1, gasolinera2, gasolinera3, etc) instead of containing all the data returned by the URL. However, for this to work, your Home Assistant must be running on Linux and contain the jq software (I am using Ubuntu and jq is already installed). If you are using hass.io then I donā€™t think jq is available.

Thanks, my HA is on a raspberry pi 2. I started with it for playing, but now is really helpful for me. Donā€™t worry, this solution is enough. I modify URL in order to receive ā€œonlyā€ 200 Gas Station Info only with the Diesel Price:
https://sedeaplicaciones.minetur.gob.es/ServiciosRESTCarburantes/PreciosCarburantes/EstacionesTerrestres/FiltroMunicipioProducto/4354/4

OK, if youā€™re happy with the current solution then all is well.

Just to show you what I was experimenting with, the following jq query extracts three matching IDEESS items then formats them into a simple dictionary consisting of id and precio.

.ListaEESSPrecio[] | select( .IDEESS == ("3213", "3217", "12878") ) | {id: .IDEESS, precio: .PrecioProducto} 

The result is very compact and can be stored in an attribute to be used by the Template Sensors (gasolinera1, gasolinera2, etc).

{
  "id": "3217",
  "precio": "1,269"
}
{
  "id": "3213",
  "precio": "1,275"
}
{
  "id": "12878",
  "precio": "1,209"
}

Anyway, just an idea to keep in mind should you need it in the future.

1 Like

Hereā€™s one more solution that stores much less data in the RESTful sensor. It also stores it in the sensorā€™s state as opposed to an attribute.

  - platform: rest     
    resource: https://sedeaplicaciones.minetur.gob.es/ServiciosRESTCarburantes/PreciosCarburantes/EstacionesTerrestres/FiltroMunicipioProducto/4354/4
    name: gasolinera
    value_template: >
      {%- for y in  value_json.ListaEESSPrecio | selectattr('IDEESS', 'in', '3089, 4520, 4337, 12878') | list -%}
        {{y.IDEESS}}:{{(y.PrecioProducto).replace(',','.')}},
      {%- endfor -%}
    scan_interval: 86400

In the template, change '3089, 4520, 4337, 12878' to all the IDEESS values you need. The sensorā€™s state value will look something like this (very compact and less than 255 characters):

4520:1.279,12878:1.209,3089:1.189,4337:1.259,

Each Template Sensor matches the desired IDEESS value and gets the associated precio (using the function regex_findall_index).

      gasolinera1:
        friendly_name: Gasolinera1
        value_template: >-
          {{ states('sensor.gasolinera') | regex_findall_index("12878:(\d+\.?\d+)") }}
        unit_of_measurement: 'ā‚¬/L'
      gasolinera2:
        friendly_name: Gasolinera2
        value_template: >-
          {{ states('sensor.gasolinera') | regex_findall_index("4520:(\d+\.?\d+)") }}
        unit_of_measurement: 'ā‚¬/L'

Hereā€™s how it appears in the States page:

1 Like

Sounds good, but I donā€™t know how to programming that ! :wink:

Iā€™m very quick!! sorry, and thanks again!!