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


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:{{ now().year }}/{{ now().day }}/{{ now().month }}/{{ now().hour }}/{{ now().minute }}/39.472/2.532/
  method: GET
    - 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:

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):

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:

     ^^^ ^^^^^
resource_template:{{ 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:{{ now().year }}/{{ now().month }}/{{ now().day }}/{{ now().hour }}/{{ now().minute }}/39.472/2.532/

This URL:

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: