Template Value Sensor stop working. Value is "unknown"

State: unknown

Clearly it means the jsonrest custom component is failing to retrieve and process the URL’s data. You may wish to contact the author of this custom component and report the problem you’ve encountered.

Is there any way to filter directly in the HTML or something like that? I only want to know 5 Gas Station price… :wink:

The RESTful integration allows you to specify a value_template in order to process the incoming data and select only what you require. Based on the blog post for jsonrest (and my very poor Spanish), I don’t see it mentioning that it supports a value_template.

I found this, I think i could create a Value Sensor directly for a given Gas Station:
https://sedeaplicaciones.minetur.gob.es/ServiciosRESTCarburantes/PreciosCarburantes/help

It would be helpful if there’s a way to modify the URL so it returns only the results you want. Remember, an entity’s state can only hold a maximum of 256 characters. However, an entity’s attributes don’t have this restriction. With the RESTful integration, you can store all or parts of the received data into one or more custom attributes (see json_attributes).

I found this URL filtered only with Gas Station in Madrid (City), about 200. And only for Diesel (type 4)
https://sedeaplicaciones.minetur.gob.es/ServiciosRESTCarburantes/PreciosCarburantes/EstacionesTerrestres/FiltroMunicipioProducto/4354/4

The Diesel price now have another name (PrecioProducto), so, I have modified the Template:

GASOLINERA BALLENOIL VICALVARO

  • platform: template
    sensors:
    gasolinera1:
    friendly_name: ‘Ballenoil’
    value_template: >
    {% for petrol in states.sensor.gasolinera.attributes.ListaEESSPrecio %}
    {% if petrol[“IDEESS”] | int == 12878 %}
    ‘{{ petrol[“PrecioProducto”] | capitalize | replace(",",".") }}’
    {% endif %}
    {% endfor %}
    unit_of_measurement: ‘€/L’

Rebooting and test.

no way :frowning:

Still unknown

Because the URL returns over 72000 characters. There’s no way that will be stored in the state value of sensor.gasolinera.

Screenshot%20from%202019-10-12%2013-51-21

Thanks a lot. I suppose that older HA versions support that, because it works in older version :frowning:

No. Older versions still never supported states over 255 characters.

It seems that (as @123 said) that the custom_component you are using somehow was able to extract the data and make the state less than the 255 limit. And now that custom component no longer works in the newer versions of HA.

Either contact the developer of the custom_component or use another method to get what you need. Or stay on the older version of HA that the custom component worked in.

I’ve created the following solution, using the RESTful integration, which puts the received data in an attribute (ListaEESSPrecio). The sensor’s state is simply the value of Fecha.

  - platform: rest     
    resource: https://sedeaplicaciones.minetur.gob.es/ServiciosRESTCarburantes/PreciosCarburantes/EstacionesTerrestres/FiltroProvincia/28    
    name: gasolinera
    value_template: "{{value_json.Fecha}}"
    json_attributes:
      - ListaEESSPrecio
    scan_interval: 86400

The result when viewed in the States page is this monstrosity:

I can extract PrecioProducto for the entry matching IDEESS=12878 using this template:

{{ state_attr('sensor.gasolinera', 'ListaEESSPrecio') | selectattr('IDEESS', 'eq', '12878') | map(attribute='PrecioProducto') | join(' ') | replace(',','.') }}

Here’s the result:

The following Template Sensor displays PrecioProducto for IDEESS=12878.

  - platform: template
    sensors:
      gasolinera1:
        friendly_name: Gasolinera1
        value_template: "{{ state_attr('sensor.gasolinera', 'ListaEESSPrecio') | selectattr('IDEESS', 'eq', '12878') | map(attribute='PrecioProducto') | join(' ') | replace(',','.') }}"
        unit_of_measurement: '€/L' 

Here it is in the States page:

Screenshot%20from%202019-10-12%2021-51-15

1 Like

Thanks a lot!!! I’ll try to test it as soon as possible.
:clap::raised_hands:

It works! Really really thanks you!!! Thanks a lot

1 Like

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!!