MQTT RF Switch config

I have a Sonoff RF Bridge with Tasmota firmware and configured in HA works fine. I recently bought a 4 channel RF relay and paired with RF bridge. I am trying to configure in HA as MQTT switch, but I am finding difficulties. The switch state on the front-end is going off in a second when triggered. How to fix? My config is

platform: mqtt
name: “Rf Switch1”
state_topic: “stat/sonoff_bridge/RESULT”
command_topic: “cmnd/sonoff_bridge/RfKey1”
payload_on: “ON”
payload_off: “OFF”
qos: 1
retain: true

The switch is triggered without any issues, but the switch state in frontend goes to off. To switch-off the RF, I need to again press the switch on the front-end, and the switch goes to off.

I have answered this question too often on the forums, so I have decided to try and write a faq for inclusion in the documentation. Please try this to see if it helps, and hopefully give some indication on what I need to improve :grinning: The tasmota firmware normally returns a good status message - although I don’t actually have any to say how to set it up.

{% linkable_title Switch Button turns back %}

Whenever I press a switch it turns back after a second

This is normally due to the switch not receiving a message with the topic state_topic: to indicate the new state of the switch. The payload of this message must match either the payload_on: or payload_off: parameter.

There are two ways to fix this, depending on your type of switch

Fix the returned status message

This is the ideal solution. The physical switch returns an MQTT message with the topic defined in state_topic: and a payload as defined in payload_on: or payload_off: as appropriate.

To see what message is being sent by the switch, use the tools described in MQTT Testing (such as mqtt_sub) to display all messages being sent to the broker.

If the returned status message has a payload that is formatted in some way (such as in JSON), it can be decoded using a template specified in the value_template: parameter. The result of the template still has to match the payload_on and payload_off parameters.

Set the switch to optimistic

If your physical switch does not return a status message, you can indicate this to Home Assistant by setting the parameter optimistic: true. In this case, Home Assistant does not wait for a response from the switch, so you have no indication that the physical switch actually changed state, which is not ideal.

1 Like

When I toggle the switch from HA panel, the RF switch getting switched on and the MQTT data in RF bridge is
{RfKey1: “Learning sent”}. I have configured the RfKey as toggle mode, so for switch on / switch off, the MQTT data received is same.
I have tried the optimistic mode, the frontend icon changed from toggle to power/on/off icon. The RF relay is switching on only when I press power/on icon and NOT power/off icon.

So what is the topic and payload of the MQTT message being sent? You can determine this from the MQTT testing page

The output of the MQTT is below when I press the RfKey1 button on RF Bridge.

stat/sonoff_bridge/RESULT {“RfKey1”:“Learned sent”}
stat/sonoff_bridge/RESULT {“RfKey1”:“Learned sent”}
stat/sonoff_bridge/RESULT {“RfKey1”:“Learned sent”}

Is this the response you get when you press the switch on the HA front end? That is the response we need to code in to the HA config so that it recognizes the response.

If that is the response to the command sent by HA, then there is no way to determine what the state of the switch is. I would be surprised though, because I thought tasmota was more intelligent than that.

Yes, for RFKey it is the response in Tasmota console, and can’t determine the state.

But have you seen what MQTT messages get sent by using mosquitto_sub, or some other such client. I’m sorry, but I really don’t trust some debugging console output from something that isn’t making sense.

The about output is from mosquitto_sub client only. The example from this page

The output is same in Tasmato console too

OK, it seems in your setup the tasmota isn’t sending any response we can decode to determine its current state, so the only option I can see is optimistic mode.

When you tried that, I notice you said


If you need the MQTT data to be the same, you need to set payload_off and payload_on to be the same.

Be aware though, that with this the HA front end will quite likely not be in the same state as the actual relay, as any change in the relay from a remote source will not be reflected in HA.

To avoid any confusion, it might be simpler to represent the relay in HA as an script that sends an automation to toggle the switch, rather than a switch that may be in the wrong state. Something like

      - service: mqtt.publish
        topic: 'cmnd/sonoff_bridge/RfKey1'
        payload: 'ON'

When I add this code to my scripts.yaml I get this error

 Invalid config for [script]: [topic] is an invalid option for [script]. Check: script->script->candle_wall->sequence->0->topic. (See /config/configuration.yaml, line 37). 

topic: can not be used?


      - service: mqtt.publish
          topic: 'cmnd/sonoff_bridge/RfKey1'
          payload: 'ON'