Value template value_json is udefined platform rest

I’m trying to “run” a sensor to load foreign exchange rates from a website. The output is in json. It works in practice, but I see an error in the log: undefined value_json

The examples (solutions) that I found in the discussion are only for other cases.

What is the correct notation for the template?

  - platform: rest
    name: currency_cny
    resource: https://api.exchangeratesapi.io/latest?symbols=CNY
    value_template: "{{ value_json['rates'].CNY }}"
    scan_interval: 1000

  - platform: rest
    name: currency_usd
    resource: https://api.exchangeratesapi.io/latest?symbols=USD
    value_template: "{{ value_json['rates'].USD }}"
    scan_interval: 1000

depends on the shape of your json, which you haven’t posted. As for the syntax, you can use dot or [’’] notation to get the items if they start with a alpha character. If they start with a number, you have to use [’’] notation.

all the following are valid

    value_template: "{{ value_json.rates.CNY }}"
    value_template: "{{ value_json.rates.USD }}"
    value_template: "{{ value_json['rates'].CNY }}"
    value_template: "{{ value_json['rates'].USD }}"
    value_template: "{{ value_json.rates['CNY'] }}"
    value_template: "{{ value_json.rates['USD'] }}"
    value_template: "{{ value_json['rates']['CNY'] }}"
    value_template: "{{ value_json['rates']['USD'] }}"

I’m not sure why it’s not working for you but I put your exact code in my config for the “currency_cny” and it worked exactly as expected:

ex2

And I tested it in the template editor using all of petro’s examples as well and it works there too:

it wont work there without you setting value_json. value_json is a variable that only works inside the value_template field.

See what @finity did?

I didn’t have time to edit the post with the photo … :slight_smile: There is no entry. Can’t (json, jinja2) test in template without input … I understand. Thank you.