Template Value Sensor stop working. Value is "unknown"

Hello all, this is my first message (sorry for English mistakes):

I had a working template value json sensor from this link:

sensor:   
  - platform: jsonrest     
    resource: https://sedeaplicaciones.minetur.gob.es/ServiciosRESTCarburantes/PreciosCarburantes/EstacionesTerrestres/FiltroProvincia/28    
    name: gasolinera     
    scan_interval: 86400

- platform: template
  sensors:      
    gasolinera1:
      friendly_name: Gasolinera1
      value_template: >-
              {%- for petrol in states.sensor.gasolinera.attributes.ListaEESSPrecio -%}
                {%- if petrol["IDEESS"] | int  == 12878 -%}  
                  {{ petrol["Precio Gasoleo A"] | capitalize | replace(",",".") }} 
                {%- endif -%}
              {%- endfor -%}
      unit_of_measurement: '€/L'  


From two months ago, in a HA upgrade, sensor stop working. I've studied how to fix it, but unfortunately I'm not a programmer and I cannot fix it. Actually, the sensor says "unknown" in his state, but directly in a JSON Interpreter, the value es correct and all variables are correct.

Thanks a lot in advance

I’m not familiar with the jsonrest platform and I can’t find it in the list of Integrations. Is this a custom component?

There is a REST integration that may work for you and it’s the RESTful Sensor.

Thanks, but I had test both (jsonrest and rest instead) but always fail. Before the past HA upgrade, all works perfectly (even with jsonrest)

Thanks for response

When I enter this URL in my browser:

https://sedeaplicaciones.minetur.gob.es/ServiciosRESTCarburantes/PreciosCarburantes/EstacionesTerrestres/FiltroProvincia/28

the response I receive:


is over 14000 characters long (and not in JSON format).

An entity’s state can only store a maximum of 256 characters.

Which version of Home Assistant were you using when it worked?


EDIT
I looked at the linked article and discovered jsonrest is a custom component.

0.99.

As I said, in 0.95 version it works. In this template, there is a FOR function as first to walk through in all data; then there is an IF function looking for IDEESS (identifier of a petrol station) and, at last, take the value of DIESEL (Precio Gasoleo A in spanish).

I used a Json web for read all data in a formatted way: https://jsoneditoronline.org
(Open-> Open URL -> and paste the URL with all data)

Look at the Developer Tools > States page and find sensor.gasolinera. What value does it report for its state?


EDIT
Yes, you’re right. I used jsoneditoronline.org and it shows the data to be in JSON. Interestingly my browser didn’t render it that way.

1 Like

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