Python script to translate MQTT topics

I’m new to Home Assistant and I have running on a Raspberry Pi 3. I have a Arduino MQTT Client connected to my private Mosquito Broker that subscribes to a topic “rftx” and publishes to “rfrx” The “rftx” topic takes a rf code and transmits it to control my Etekcity RF Outlets. The “rfrx” topic publishes any codes picked up by a receiver. I want to use the “rfrx” topic as state control for a switch. The current YAML for my switch is below:

  • platform: mqtt
    command_topic: “rftx”
    payload_on: “333107”
    payload_off: “333116”

I would like to integrate a state topic so I could use the existing remote that came with the outlet to turn it on and off, and have the state of the outlet change in Home Assistant. In order to integrate a state topic, if I understand the docs correctly, I need to have a topic that has either ON or OFF published to it. The easiest solution I can think of is to make a python program that listens to the “rfrx” MQTT topic and the “rftx” MQTT topic and determines the state from both of those. Is this the proper way to tackle this, and if so, how do I setup the script within Home Assistant so that it starts on boot, etc.

I have this solution, which may work for you, if the codes HA sends are the same as the remote sends

  - platform: mqtt
    command_topic: "rtfx"
    state_topic: "rtfx"
    payload_on: "333107"
    payload_off: "333116"

What happens is that the HA switch component listens to the MQTT topic, and alters the switch state. This happens when either the topic is sent from the switch component, or it is received from the remote control via the receiver and MQTT.

Does the state topic need to change when the command topic is called with payload_on? The current way I have it setup, the “rfrx” topic only changes state with an external remote button press, not by Home Assistant calling the command_topic. Thanks,

I just tried it and it works, thanks for your help @gpbenton!

1 Like