MQTT to Dynamic IR Command via Template

Hi all -

I’m new to templates, so go easy on me! I’m trying use the payload from MQTT (JSON) send up to 3 dynamic IR commands (d1, d2, d3). I wrote my YAML as follows:

alias: TestMQTT
description: ""
trigger:
  - platform: mqtt
    topic: homeassistant/playc
condition: []
action:
  - service: remote.send_command
    data:
      device: Sony CD Player
      command: {{ trigger.payload_json.d1 }}
    target:
      device_id: xxxxxxxxxxxxxxxxxxxxx
mode: single```

Home assistant changes the code to:

alias: TestMQTT
description: ""
trigger:
  - platform: mqtt
    topic: homeassistant/playc
condition: []
action:
  - service: remote.send_command
    data:
      device: Sony CD Player
      command:
        "[object Object]": null
    target:
      device_id: xxxxxxxxxxxxxxxxxxxxx
mode: single

So, clearly I’m doing something wrong and I’d be grateful for any pointers. Thanks in advance!

Easy fix. Wrap the template in quotes.

      command: '{{ trigger.payload_json.d1 }}'

Reference: Important template rules

That was easy! Thanks, @123 !!

1 Like

You’re welcome!

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

For more information about the Solution tag, refer to guideline 21 in the FAQ.

1 Like