Problem with REST sensor when JSON has key named 'values'

Hi!

I’ve spent the last 3 hours trying to figure out what’s the problem with my rest sensor, but I’m unable to solve it. Maybe someone can help me out.

I’m trying to get the temperature in a nearby beach. This is the json that I’m getting from the data provider:

{"name": "temp", "beautyName": "Sea surface temperature", "lat,lon": ["39.472", "2.532"], "values": 27.38160514831543, "units": ["Celsius", "\u00baC"], "localtime": "2021/9/12 20:4"}

This is the configuration for my REST sensor:

- platform: rest
  name: Playa El Mago (Temperatura)
  resource_template: http://seaboard.ws.socib.es/temp/{{ now().year }}/{{ now().day }}/{{ now().month }}/{{ now().hour }}/{{ now().minute }}/39.472/2.532/
  method: GET
  json_attributes:
    - name
    - values
    - lat,lon
    - localtime
  value_template: "{{ value_json['values'] | float }}"
  device_class: temperature
  unit_of_measurement: "°C"

But the value returned by the sensor is always NaN:

At first I thought that the problem was the use of the “values” keyword, that’s why I used the brackets notation instead of the dots notation.

However, If I try this in the developers tools tab:

{% set value_json = {"name": "temp", "beautyName": "Sea surface temperature", "lat,lon": ["39.472", "2.532"], "values": 27.38160514831543, "units": ["Celsius", "\u00baC"], "localtime": "2021/9/12 20:4"} %}

{{ value_json['values'] | float }}

It works OK:
imagen

Hope someone can help me. Thank you in advance.

Because that’s what it returns. Here’s the resource you’re requesting (coming from Dev->Templates and at the current time for me):

http://seaboard.ws.socib.es/temp/2021/12/9/15/32/39.472/2.532/

And here’s what it returns in a browser:

{"name": "temp", "beautyName": "Sea surface temperature", "lat,lon": ["39.472", "2.532"], "values": "NaN", "units": ["Celsius", " "], "localtime": "2021/12/9 15:32"}

What URL were you using to get the output in your post?

1 Like

The problem is that the URL has the date in the wrong order:

YEAR/DAY/MONTH/HOUR/MINUTE
     ^^^ ^^^^^
resource_template: http://seaboard.ws.socib.es/temp/{{ now().year }}/{{ now().day }}/{{ now().month }}/{{ now().hour }}/{{ now().minute }}/39.472/2.532/

Swap the day and month and then it works correctly.

resource_template: http://seaboard.ws.socib.es/temp/{{ now().year }}/{{ now().month }}/{{ now().day }}/{{ now().hour }}/{{ now().minute }}/39.472/2.532/

This URL:

http://seaboard.ws.socib.es/temp/2021/9/12/15/32/39.472/2.532/

reports the today’s temperature correctly (screenshot from Firefox):

Screenshot from 2021-09-12 19-05-42

1 Like

Thank you!!!

I cannot believe I missed that!! I had the right URL while debugging in firefox, but not on home assistant :man_facepalming: :man_facepalming: :man_facepalming: :man_facepalming: