Template to json publish via mqtt to control LG ac via tasmota irbridge

I try to controle my LG ac unit via tasmota irbridge.


It works with a static json
but I want to use a helper to set the temperature.and I needto use a template.
I tested it in the template in and this looks ok (same message as before, no dynamics)

But if it is send via mqtt by an automation, the " are replaced with ’

The AC doesn’t understand the message wit the ’ ilo of the "

Can somebody help?

Regards
Johan

Have you found a bug, or is it working as documented?
Without delving into the Tasmota documentation, which is comprehensive, but devoid of many examples… :frowning:
Does using the double quotes get translated the other way to single quotes?
Is there an ‘as-is’ option to treat your JSON as an ASCII string and just shuffle it along untouched? Can you add the appropriate codes by using hex values tucked in at both ends?
Can you just squirt out the correct data to the right port in the correct format, bypassing MQTT mangling it altogether? WireShark may assist in looking at the content at the packet level if you don’t want to delve into Python source code.
Just tossing about ideas.

Here is how I recommend you do it. Replace input_number.test_number with the entity_id of your Input Number helper (or whatever helper you are using to store the temperature value).

actions:
  - variables:
      cmd:
        Vendor: LG
        Model: AKB75215403
        Command: Control
        Mode: heat
        Power: "off"
        Temp: "{{ states('input_number.test_number') }}"
  - action: mqtt.publish
    metadata: {}
    data:
      evaluate_payload: false
      qos: 0
      retain: false
      topic: cmnd/irbridge/test/irhvac
      payload: "{{ cmd | to_json }}"

Here is how the published payload appears in MQTT Explorer.

It is properly formatted JSON and the value of Temp is 21 which is the value of input_number.test_number.


NOTE

As a precaution, I suggest you consider adding either an int() or float() filter to the template and include a default value.

In the following example, the int filter will convert the helper’s value to an integer. If it is unable to convert it, int will use the supplied default value 19.

{{ states('input_number.test_number') | int(19) }}

Thank you for the fast reply.

I did copy your suggestion in an automation and tried to execute the action
but I get a failurecode ‘Unable to determine action @ data[0]’.

If I send the mqtt message with " then it works and tehe AC reacts.
If it is send with ’ the AC doesn’t react.
In the template editor it all looks ok (editor.pgn) but if I use a automation to send the message I receive it with ’ ilo ".
Is this a bug or a mistake from my side? Or is tasmota to picky?

After some more testing it looks like that tasmota doesn’t accept the ’ .(with the ’ it is indeed an invalid json format)

Why are the " switched to ’ if I use the template in an action, in the editor the result looks ok.

The solution was simple, (as alway :grinning:)


I was just formating the message wrong in the mqtt action.
It works fine now.
Thank you all for pointing me in the right direction.

This topic also pointed me to the solution.

The screenshot shows you copied the YAML code in the wrong place. All of that YAML does not belong in the “MQTT: Publiceer” action.

The YAML code I posted represents the actions section of an automation. It is not a single action but an entire actions section. I assumed you would use it to replace your existing automation’s entire actions section. You copied it into the mqtt.publish (MQTT: Publiceer") action and that’s why you got an error message (Unable to determine action).

That’s why my example uses a variable (cmd) to handle the JSON payload’s formatting. It makes it simpler to publish it in JSON format (using the to_json filter) instead of fussing with the correct order and quantity of double-quotes (") and commas (,).

The YAML code for your automation will look something like this (I don’t know the triggers and conditions because you never shared your automation).

alias: Your Automation
triggers:
  ... your triggers go here ...
conditions:
  ... your conditions go here, if any ...
actions:
  - variables:
      cmd:
        Vendor: LG
        Model: AKB75215403
        Command: Control
        Mode: "{{ states('input_select.hvac_mode') }}"
        Power: "on"
        Temp: "{{ states('input_number.hvac_set') }}"
  - action: mqtt.publish
    metadata: {}
    data:
      topic: cmnd/irbridge/test/irhvac
      payload: "{{ cmd | to_json }}"

Because the JSON standard uses double-quotes to delimit text, not single-quotes.

Again, creating valid JSON becomes very easy if done the way I had suggested (I have been using the same technique for a long time). Here’s an example from 2 years ago:

The linked topic demonstrates just how messy and illegible it can be to embed Jinja2 templates directly into JSON-formatted text. One missing double-quote or comma and the JSON payload is corrupted.