Value_json is undefined

I’m trying to implement an RGB LED switch controller over MQTT. I have followed the example found at https://home-assistant.io/components/light.mqtt/ to get it integrated with HA.

The configuration.yaml section for the switch is:

# Example configuration.yml entry
light 1:
  platform: mqtt
  name: "Test Light RGB"
  state_topic: "office/1stfl/light/status"
  command_topic: "office/1stfl/light/switch"
#  brightness_state_topic: "office/1stfl/brightness/status"
#  brightness_command_topic: "office/1stfl/brightness/set"
  rgb_state_topic: "office/1stfl/rgb/status"
  rgb_command_topic: "office/1stfl/rgb/set"
  state_value_template: "{{ value_json.state }}"
#  brightness_value_template: "{{ value_json.brightness }}"
  rgb_value_template: "{{ value_json.rgb | join(',') }}"
  qos: 0
  payload_on: "ON"
  payload_off: "OFF"
  optimistic: false

The code works as expected, but I keep getting the following errors logged to home-assistan.log every few seconds:

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/homeassistant/helpers/template.py", line 35, in render_with_possible_json_value
    return render(hass, template, variables)
  File "/usr/local/lib/python3.4/dist-packages/homeassistant/helpers/template.py", line 61, in render
    raise TemplateError(err)
homeassistant.exceptions.TemplateError: UndefinedError: 'value_json' is undefined
16-05-06 19:06:25 homeassistant.core: Bus:Handling <Event mqtt_message_received[L]: payload=ON, qos=0, topic=office/1stfl/light/status>
16-05-06 19:06:25 homeassistant.helpers.template: Error parsing value
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/homeassistant/helpers/template.py", line 59, in render
    }).render(kwargs).strip()
  File "/usr/local/lib/python3.4/dist-packages/jinja2/environment.py", line 989, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/local/lib/python3.4/dist-packages/jinja2/environment.py", line 754, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.4/dist-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
  File "/usr/local/lib/python3.4/dist-packages/jinja2/sandbox.py", line 329, in getattr
    value = getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'value_json' is undefined

The error is certainly related to the light 1 switch, but I’m just not sure what’s going wrong. I am running 0.18.2. Do anyone have any good suggestions to what might be causing this?

Regards,
Thomas

thnilsen,

I was having a similar problem with my setup and I realized one of my sensors was reporting a malformed JSON record, namely a switch was failing to quote a string value. To illustrate, consider this sample sensor data:

{ "timestamp": 1472476326, "state": on }

Notice the lack of quotes on the “state” value. This causes the python JSON parser to fail and hence explains the error you see on your log files. The correct record should be

{ "timestamp": 1472476326, "state": "on" }

You might want to check your logs to see if this is the case.

Hope this helps.

I’m having a hard time getting json to work.

my log says:

2017-10-04 04:57:15 ERROR (MainThread) [homeassistant.helpers.template] Error parsing value: 'value_json' is undefined (value: {"temperature":90.31999969,"humidity":56,"heatIndex":98.20548248,"distance":199,"signal":-64,"time":"10m"}, template: {{ value_json.temperature }})

The configuration for the sensor is:

  - platform: mqtt
    name: "Garage Temperature"
    state_topic: "garage/sensor"
    value_template: '{{ value_json.temperature }}'
    unit_of_measurement: "F"

does anyone can see what is wrong?

I have thousands of them since Jun 16 related to Ring Doorbell.

2018-06-19 21:30:21 ERROR (MainThread) [homeassistant.helpers.template] Error parsing value: ‘value_json’ is undefined (value: container not found, template: {{ value_json is not none and value_json.state == “running” }})