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