Issue with Parsing templates

Hi All,
I needs some assistance in getting this value to work for my MQTT climate
I have the commands all set
But the states are not working
the JSON returned when a command is set is as follows

{"power":0,"mode":"fan","speed":2,"temp":26.0,"response":"RSSL11FF00011002130311041205133406110F1075"}

My config is as follows

climate:
    - name: "MHI Ducted"
      unique_id: mhiducted
#      availability:
#        - topic: "MHI/status"
#      availability_mode: "any"
      modes:
        - "cool"
        - "auto"
        - "dry"
        - "fan_only"
        - "heat"
      mode_command_template: '{"mode": {{ value }} }'
      action_topic: "MHI/status"
      fan_modes:
        - "auto"
        - "low"
        - "medium"
        - "high"
        - "max"
      fan_mode_command_topic: "MHI"
      fan_mode_command_template: '{"speed": {{"1" if value == "low" else "2" if value == "medium" else "3" if value == "high" else "4" if value == "max" else "0"}} }'
      mode_command_topic: "MHI"   
      mode_state_topic: "MHI/status"
      mode_state_template: "{{value_json['mode'] }}"
      power_command_topic: "MHI"
      power_command_template: '{"power": {{ value }} }'
      temperature_command_topic: "MHI"
      temperature_command_template: '{"temp": {{ value }} }'
      payload_off: "false"
      payload_on: "true"
      payload_available: "connected" 
      optimistic: false
      retain: false
      temperature_state_topic: "MHI/status"
      temperature_state_template: "{{value_json['temp'] }}"
      min_temp: 16
      max_temp: 30
      temp_step: 0.5

I get the error when i change the mode
Error parsing value: ‘value_json’ is undefined (value: {“power”:0,“mode”:“cool”,“speed”:3,“temp”:26.0,“response”:“RSSL11FF00011002120312041205133406110F1075”}, template: {{value_json[‘mode’] }})

When i put the json in a debugger
i get a invalid character at position 2 before the RSSL

Can anyone assist?

I am not very sure here, but the first character of the “response” value is rendering as a box after you paste it, which suggests that something about the response from the hardware is off. That’s vaguely consistent with the debugger message, although I’m not sure what “position 2” is so it’s hard to be precise.

Can you give more formation about the hardware and how exactly you’re interfacing it with MQTT?

This is the project i’m trying to interface with

I would prefer to clean up the JSON if possible then to rewrite the ESP board

When i paste the MQTT reponse in a json parser this is what comes up

Error: Parse error on line 1:
...mp":26.0,"response":"RSSL11FF0001100213
-----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'

Its like there is an STX character or escape character it doesnt like.

You’re obviously getting a non-ASCII character at the start of the value string. I am not sure if that’s the cause of your ultimate issue, but it’s suspect—because the JSON is otherwise seemingly entirely (and deliberately) ASCII.

With that said, I don’t have any other ideas because I’m not experienced with the setup you’re trying to make work. Hopefully someone else here will have ideas.

Indeed. This what I get when pasting the return value in vscode:

image

That’s a control character (start of text). It’s ASCII, but not in the way I meant the term.

What’s actually giving you that message? If you have the ability to filter the string before it’s parsed by whatever gives that error, maybe you can remove that character. If it always happens at the start of the response field, it’s easy enough to remove?

I tested this on my system and it worked. Try it on yours and let me know the result.

mode_state_template: >
  {% set vjson = value.replace('\x02','') | from_json %}
  {{ vjson['mode'] }}

Thanks. That worked!
One last thing with the states for the fan speed…
How do i change that back from a numeric back to “low/med/high/max”

fan_mode_state_template: >
  {% set vjson =value.replace('\x02','') | from_json %}
  {{ vjson['speed'] }}
  {% if vspeed == "1" %} low
  {% elif vspeed == "2" %} medium
  {% elif vspeed == "3" %} high
  {% elif vspeed == "4" %} max
  {% else %} auto
  {% endif %}

Im not sure where i have gone wrong

What’s vspeed? It isn’t defined in this code. Did you mean to use vjson['speed'] instead of vspeed in the if conditions? Alternatively, you could add

{% set vspeed = vjson['speed'] %}

Thank Dominic, but still no go…

      fan_mode_state_template: >
        {% set vjson =value.replace('\x02','') | from_json %}
        {% set vspeed = vjson['speed'] %}
        {% if vspeed == "1" %} low
        {% elif vspeed == "2" %} medium
        {% elif vspeed == "3" %} high
        {% elif vspeed == "4" %} max
        {% else %} auto
        {% endif %}

Are you seeing it say “auto”? If so, drop the double-quotes around the speed values (1, 2 etc.) since they’re integers, not strings, in the sample JSON you posted.

Otherwise, what’s happening?

fan_mode_state_template: >
  {% set vjson = value.replace('\x02','') | from_json %}
  {{ { 1: 'low',
       2: 'medium',
       3: 'high',
       4: 'max' }.get(vjson.speed, 'auto') }}

Sorry my bad…
I tested in the template editior and it checked out ok without the quotes but forgot to remove the quotes from my configuration.yaml file.

Glad to hear that it solved the original problem, namely the inability to process the received payload due to an unprintable character that corrupted the JSON structure.

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which indicates to others that the topic has been solved. This helps other users find answers to similar questions.