MQTT Button to Send IR Commands

Hi everyone! I’ve been crashing my way through Home Assistant and feel like I’ve made reasonably good progress but now I’ve come to a halt grinding halt with MQTT and getting commands to devices.

I have an IR bridge (Genio brand) that I have converted to Tasmota via OTA. I can see IR commands it receives in the Tasmota console interface and I can use the HA MQTT interface to publish commands to the device. Most importantly, I can see the published commands appear in the Tasmota Console - so I know HA and the IR Bridge are talking.

Where I am stuck is joining the dots to make a card in Lovelace to send the IR commands to MQTT to then pass to my devices.

Below shows the command being published, and then the second image is the successful processing of the command in the IR bridge.

publish

Where the topic is cmnd/ir_bridge_mbr/IrSend and the payload is {“Protocol”:“NEC”,“Bits”:32,“Data”:“0x4EBB926D”,“DataLSB”:“0x72DD49B6”,“Repeat”:0}} can someone enlighten me on how to create a button in Lovelace to execute this command?

From what I can work out it seems to relate to creating a script and also something in my configuration.yaml file but I just can’t seem to get it working.

You define a mqtt switch or publish it with script or automation. Then you add switch or script to the card as a “button”.

Given I am so deflated I feel like I am asking for this to be drawn in crayon for me…but…are you able to give me an example of what you mean?

Does that mean I define the MQTT switch in config.yaml and then add as a button in the dashboard and that’s all that should be needed?

If so, how do I write that switch? I’m looking for the examples of what I tried at the moment to show what I did.

Thank you for taking the time to reply!

Well, I got some success!

In my config.yaml I put this:

switch:
  - platform: mqtt
    name: "TV"
    command_topic: "cmnd/ir_bridge_mbr/irsend"
    payload_on: '{"Protocol":"NEC","Bits":32,"Data":"0x4EBB926D","DataLSB":"0x72DD49B6","Repeat":0}'

My challenge now being that I need the button to turn off after the command is sent.

Anyone able to help with suggestions on how to do that?

You’d be better using a script, no on or off state to worry about then.

It sounds a bit like you’ve dived straight into the deep end, if so I’d suggest you go back to basics and understand how automations and scripts etc work by trying some of the examples, will make your life much easier going forward.

Appreciate that, but hence the reason I’m looking for some help to join the last of the dots. I’ve got to a point where I can flash a device and get it to talk with HA and now I can get it to talk with a switch, but I need to understand how to get the switch to turn off once pressed. When I read into scripts I feel like I’m going in to circles. If there is a known primer that is good, outside of the official manual, then I’d love to be pointed in the right direction. And I know it’s a stretch but with what I’ve provided if anyone can help with the last parts of the code required I feel like it’s going to unlock quite the puzzle for me.

You could add the same into payload_off from payload_on as I assume it is just a toggle IR ?

Maybe use a button rather than a switch ? calling an mqtt.publish command:

Something like this (UNTESTED):

type: button
name: TV
tap_action:
  action: call-service
  service: mqtt.publish
  service_data:
    topic: cmnd/ir_bridge_mbr/irsend
    payload: '{"Protocol":"NEC","Bits":32,"Data":"0x4EBB926D","DataLSB":"0x72DD49B6","Repeat":0}'

Just add a payload_off to the switch.

The off part would be the exact same data if the IR is a toggle switch. Toggles will not show on / off graphics correctly in HA, but it won’t matter since it’s sending the exact same command.

(as note, devices will often have separate power codes even if the button isn’t physically present on the remote. Some may even have features you didn’t know about. You can use a cell phone IR app to see if compatible remotes exist, try different models from the same manufacturer)

And I’m sorry if I wasn’t being clear, but a Button is specific type of element in HA speak. I meant the word to be used in more generic sense … it’s a graphic you click on

- platform: mqtt
    name: "TV"
    command_topic: "cmnd/ir_bridge_mbr/irsend"
    optimistic: true
    payload_on: '{"Protocol":"NEC","Bits":32,"Data":"0x4EBB926D","DataLSB":"0x72DD49B6","Repeat":0}'

    #//Next Line is off, change as needed.

    payload_off: '{"Protocol":"NEC","Bits":32,"Data":"0x4EBB926D","DataLSB":"0x72DD49B6","Repeat":0}'

The button card is the best way to go but again with a script not a switch to avoid the unecessary ‘off’ shenanigans. There’s an example of how to use it with a script at the bottom of the page @Holdestmade linked to.

As regards the script itself you just need to insert the data you’ve already written into one of the examples, that’s how we all learnt.

Another tip, when replying to a specific post, click the ‘reply’ button in that post or the person your responding to won’t know you have :slight_smile:

Erm…you might want to learn to use search as well…

Thank you all for your help and advice - really appreciate it!

For anyone who is looking for this in the future, well hopefully this is suitable for what yo need! What is below is working, although probably the longhand way of doing it.

In my configuration.yaml I have my switches set up like below, one for each button on the remote:

switch:
  - platform: mqtt
    name: "mbr_tv_power"
    optimistic: true
    command_topic: "cmnd/ir_bridge_mbr/irsend"
    payload_on: '{"Protocol":"NEC","Bits":32,"Data":"0x4EBB708F","DataLSB":"0x72DD0EF1","Repeat":0}'

Then in scripts.yaml I have the following for each button:

mbr_tv_power:
    alias: "MBR - TV On"
    sequence:
      - service: homeassistant.turn_on
        data:
         entity_id: switch.mbr_tv_power

Then lastly in my card on the dashboard I have the following, which I have ultimately set into horizontal stacks:

entity: script.mbr_tv_power
icon: 'mdi:power'
name: MBR TV On
show_name: false
tap_action:
  action: call-service
  service: script.mbr_tv_power
service_data: null
entity_id: script.mbr_tv_power
type: button

With all of that done, I now seem to be up and running!

1 Like

@Bobby_Nobble I had searched, for hours on end but I couldn’t make the link between where I was to where I needed to be. That said I have tried to share my end result here to help others who may get stuck in the same spot. Thanks again for your help though!

@chris14 great call on looking up discreet codes, I recall hunting those down back in the day and hopefully for my TV they exist. It is a budget model so I’m not holding much hope that they exist, but it’s really only for the source inputs where they would be handy. For everything else it’s pretty straight forward as the button is a singular function button. Thanks again!

No problem but you’re still using that switch which is especially pointless given you’re using a script now anyway. If you follow that post I linked to it has a script exactly how you need it, just slot your data in.

Another little tip which should save you some headaches when searching next time, use Google and just stick home assistant at the front of the search, is far better at finding what you want than the forum search. I just stuck ‘home assistant script to send mqtt’ in and that was the first result :slight_smile:

I am also trying to send IR Commands but using the device attached.

Is this also possible using MQTT? RaspberryPI 4

Thanks