b1mb4m
December 22, 2020, 3:55pm
1
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.
you can’t use regular yaml in the template editor. it only accepts jinja2 style templates.
so when you are telling it to return the value of the template “{{ value_json.DHT11.Temperature }}” it can’t because you haven’t told it what the value of the variable “value_json” is yet.
you will need to declare the contents of that variable in the editor by using the ‘set’ command.
so something like this:
{% set value_json = {'temperature':'1', 'humidity':'2'} %}
{{value_json.temperature}}
{{value_j…
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
petro
(Petro)
December 22, 2020, 4:37pm
2
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'] }}"
finity
December 22, 2020, 4:48pm
3
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:
And I tested it in the template editor using all of petro’s examples as well and it works there too:
petro
(Petro)
December 22, 2020, 5:05pm
5
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?
b1mb4m
December 22, 2020, 5:10pm
6
I didn’t have time to edit the post with the photo … There is no entry. Can’t (json, jinja2) test in template without input … I understand. Thank you.