How to integrate remote controls

I am quite new to HA and I am struggling with the best way to model manual remote controls.
I have two systems with manual remote controls which i want to integrate into home assistant.

System 1 can send a mqtt message when a button is pressed on the remote. Here I used the mqtt sensor to listen to these messages and change its state accordingly. Then I set the expire_after attribute to 1 second. Now i have a sensor which state changes i can use for triggers. This works for me even if I have the feeling that there should be a more elegant way to do it. Would it be a better solution to create an automation that translates the mqtt messages into events and then use the events as triggers?

sensor:
  - platform:            mqtt
    name:                rc_rc1_btn1
    state_topic:         "MS0/fhem_rc/rc_rc1_btn1/stat/state"
    expire_after:        1
    unit_of_measurement: "onoff"

System 2 can make a http request when a button is pressed on the remote. Here I am really lost. How could I use the http requests to trigger state changes? Or would it again be a better solution to create events?

I need some best practice here.

Hello @sejnub and welcome to your community!

Yaml is very frustrating in the :blush:

From my understanding you want to control your MQTT device with Home Assistant, is that is so, the best way is to make a Script: https://home-assistant.io/components/script/
I also use a MQTT to IR device have a look at my script’s examples:

script:
  ## IR Volume +
  q_media_volume_up:
    alias: 'Volume Up'
    sequence:
      - service: mqtt.publish
        data:
          topic: home/commands/MQTTtoIR
          payload: '2155835535'

  ## IR Volume -
  q_media_volume_down:
    alias: 'Volume Down'
    sequence:
      - service: mqtt.publish
        data:
          topic: home/commands/MQTTtoIR
          payload: '2155868175'

  ## IR Mute
  q_media_mute:
    alias: 'Mute TV'
    sequence:
      - service: mqtt.publish
        data:
          topic: home/commands/MQTTtoIR
          payload: '2155841655'

For the HTTP have a look at https://home-assistant.io/components/rest_command/

Thanks for your reply!

But no, I am talking about incoming “information” that I want to use for sensors not about actions.

  • From system 1 my home assistant receives mqtt messages.
  • From system 2 my home assistant receives http post requests.

I at the moment think that the best way to integrate these incoming “information” is to convert them into events with a json formatted payload so that in ha I can use these events for all kinds of stuff, especially for triggers in automations.

My first question is: Is it a good idea to convert the incoming stuff into events with a json formatted payload?

My second question is: How do I convert them? Here I have some problems because ha generates incorrect json using single quotes and not double quotes. But this I will post as a seperate question.

Take a look to the REST API

Sorry for misunderstanding!

MQTT sensor msg don’t need to be in json, take a look at the MQTT sensor docs https://home-assistant.io/components/sensor.mqtt/ the very last example of the brightness isn’t in json.

For example look at the scripts i posted earlier, I could make a sensor to read home/commands/MQTTtoIR when I send one IR code it will display on the sensor. Then I can use an automation for when the sensor detects one of the desire codes to do whatever I want

sensor:
  - platform: mqtt
    state_topic: 'home/commands/MQTTtoIR'
    name: MQTT to IR

the automation:

automation:
  - alias: "Toggle Living Room light when TV muted/unmuted"
    initial_state: True
    hide_entity: False
    trigger:
      - platform: state
        entity_id: sensor.mqtt_to_ir
        to: '2155835535'
    action:
      - service: light.toggle
        entity_id: light.living_room

If I do it your way then the following would happen:

When I push the same botton on my remote control severall times then only the first pressing is detected because the state would be “on” after the first pushing and it wouldn’t change if I pushed the button again. This is a problem.

That is one reason why i had the idea that it would be a better idea to model external events like the pushing of a button as ha events. And now I am trying to find the right way to write automations that convert these external button pushes (which enter ha via mqtt and http) into ha events.But I am really strugging with the documentation on event actions and the questions if event data should be json.

I hope I made my problem understandable.

Why cant just turn the switch off in the automation and set a count up to count the number of times it beening press then after a minute reset the count back to 0

Don’t ask how to do only been for a couple months lol