Template UndefinedError 'value_json' is undefined

I have tried different value template , but all gave an error
Error rendering template: UndefinedError: 'value_json' is undefined

- platform: mqtt
  name: "Plug 2 Humidity"
  device_class: "humidity"
  state_topic: "tele/plug_2/SENSOR"
  value_template: "{{ value_json['AM2301'].Humidity }}"
  unit_of_measurement: "%"
  retain: true

DrZzs Configuration.yaml Sensor entry: also give a error.

sensor:
  - platform: mqtt
    state_topic: 'tele/D1mini01/SENSOR'
    name: 'Temperature'
    unit_of_measurement: '°F'
    value_template: '{{ value_json.DHT11.Temperature }}'
  - platform: mqtt
    state_topic: 'tele/D1mini01/SENSOR'
    name: 'Humidity'
    unit_of_measurement: '%'
    value_template: '{{ value_json.DHT11.Humidity }}'

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_json.humidity}}

ex

1 Like