Hello everyone, I wanted to share my recent experience with integrating MQTT and dynamic IR commands in Home Assistant. For those who might be interested, I was trying to use MQTT payloads to send dynamic IR commands to my Sony CD Player. Initially, I ran into some issues, but I managed to figure it out, and I’d love to share my journey and solution with you all!
The Challenge
I started by setting up an MQTT trigger in Home Assistant. My goal was to send IR commands to my Sony CD Player based on JSON payloads received from a specific topic. Here’s what my initial configuration looked like:
yaml
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
However, when I tested this setup, I encountered an issue where the command was being interpreted as [object Object] instead of the expected value. This was frustrating, but I knew I could figure it out with some troubleshooting.
The Solution
After some research and experimentation, I realized that the issue was with how I was accessing the payload data. The payload_json filter wasn’t working as expected in this context. Instead, I needed to use the json filter to parse the payload correctly.
Here’s the corrected configuration that worked for me:
yaml
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
By using trigger.payload | json('d1'), I was able to correctly extract the d1 value from the JSON payload and send it as the IR command. This change made all the difference!
Extending the Solution
Once I got the basic setup working, I decided to take it a step further. I wanted to send up to three dynamic IR commands (d1, d2, d3) in a single payload. To achieve this, I modified my configuration to handle multiple commands sequentially:
yaml
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 - service: remote.send_command
data:
device: Sony CD Player
command: {{ trigger.payload | json(‘d2’) }}
target:
device_id: xxxxxxxxxxxxxxxxxxxxx
mode: single - service: remote.send_command
data:
device: Sony CD Player
command: {{ trigger.payload | json(‘d3’) }}
target:
device_id: xxxxxxxxxxxxxxxxxxxxx
mode: single
This setup allows me to send multiple IR commands in one MQTT message, which is incredibly useful for complex automation scenarios.
Final Thoughts
I’m thrilled to share that this setup is now working perfectly for me. It’s amazing how a little bit of troubleshooting and the right configuration can turn a frustrating problem into a powerful solution. I hope this post helps anyone else who’s trying to achieve similar automation with MQTT and IR commands.
If anyone has further questions or suggestions, feel free to reach out. I’d love to hear about how others are using MQTT and IR commands in their smart home setups!
Happy automating! ![]()