JSON parsing problem with climate control via MQTT with ESP32 device

Hi, I have a JSON parsing problem on my mqtt climate control project, I want to change temperature on climate using MQTT and ESP32 device, which is connected to climate, but I always receiving parsing error. I’m using MQTT HVAC configuration from HA website and I’m added template to my own Home Assistant. ESP32 sending changes to “ACesp32/state” topic and I’m sending data to “ACesp32/command” and I’m already use “soll” data to control climate and “on” to turn on/off.

This is my connection tree:
Climate <----> ESP32 <---->MQTT broker<----> Home Assistant

My HA code:

mqtt:  
  climate:
    - name: "Climate 211"
      modes:
        - "off"
        - "heat"
      #mode_command_topic: "ACesp32/command"
      #mode_command_template: "{{ ac_json[0].conf.on }}"
      mode_state_topic: "ACesp32/state"
      mode_state_template: "{{ ac_json[0].conf.on }}"
      temperature_command_topic: "ACesp32/command"
      #temperature_command_template: "{{ value_json[0].conf.soll }}"
      current_temperature_topic: "ACesp32/command"
      current_temperature_template: "{{ ac_json[0].conf.soll }}"
      max_temp: 33
      min_temp: 10
      precision: 1.0

My template code:

{% set ac_json = [
  {
    "ist": 23,
    "aussen": 34.5,
    "conf": {
      "on": true,
      "turbo": false,
      "eco": false,
      "soll": 22,
      "melle": false,
      "mode": 1,
      "fan": 0
    }
  }
] %}

{{ac_json[0].conf.soll}}
{{ac_json[0].conf.on}}

My errors (1st when receiving data from ESP32 after temperature change, 2-4 when sending data to change temperature):

When uncomment mode_command_topic and template also receiving in logs (when try to change mode):

Logger: homeassistant.components.websocket_api.http.connection
Source: helpers/template.py:422
Integration: Home Assistant WebSocket API (documentation, issues)
First occurred: 14:56:49 (2 occurrences)
Last logged: 14:56:54

[139850823158080] UndefinedError: 'ac_json' is undefined
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 420, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1933, in _render_with_context
    return template.render(**kwargs)
  File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/local/lib/python3.10/site-packages/jinja2/sandbox.py", line 303, in getitem
    return obj[argument]
jinja2.exceptions.UndefinedError: 'ac_json' is undefined

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 200, in handle_call_service
    await hass.services.async_call(
  File "/usr/src/homeassistant/homeassistant/core.py", line 1744, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1781, in _execute_service
    await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)(
  File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 208, in handle_service
    await service.entity_service_call(
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 678, in entity_service_call
    future.result()  # pop exception if have
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 943, in async_request_call
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 715, in _handle_entity_call
    await result
  File "/usr/src/homeassistant/homeassistant/components/mqtt/climate.py", line 829, in async_set_hvac_mode
    payload = self._command_templates[CONF_MODE_COMMAND_TEMPLATE](hvac_mode)
  File "/usr/src/homeassistant/homeassistant/components/mqtt/models.py", line 160, in async_render
    self._command_template.async_render(values, parse_result=False)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 422, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: 'ac_json' is undefined

You can’t use ac_json in that climate configuration. Use value_json instead.

That won’t fix everything, though. It looks from your error messages that ACesp32/state is returning a number — 17.0 is the first one in your error messages. Your mode_state_template is trying to look up a dictionary value from a number, so that will still fail.

value_json[0].conf.soll should return 22.

1 Like