MQTT Switch Not Publishing, but mqtt.publish works fine

Hello

I am new to Home assistant and am falling over at the first hurdle. Despite trawling forums for a solution I am clearly missing something trivial. I am at the fundamental understanding stage where I am just getting things to talk to each other before I automate

I am trying to switch an RF socket via a tasmota flashed sonoff rf bridge. RfKey1 is set to turn the socket on. RfKey2 to turn it off. Within the tasmota console this works correctly when the commands are issued.

Home assistant sees the MQTT sensor message if the doorbell is activated and the badge changes to “on”, suggesting the RF Bridge is communicating with the MQTT broker and HA receiving it

However if I operate the switch to turn on / off the socket on the HA overview then nothing happens, the switch icon changes to the “on” icon in the overview, but the rf bridge console doesn’t receive any message. I have installed the android “mqtt client” app and subscribed to all variations of cmnd/sonoff/rfkey but no message is ever received.

However if I call the mqtt.publish service in developer tools and send

{ “topic”: “cmnd/sonoff/rfkey1”, “payload”: “rfkey1”}

If I leave the payload blank, ie “”, then this works aswell

Then the rf bridge receives the message (and also my phone) and the socket operates as expected

To me it seems as though everything is talking to each other but the switch entity does not publish the mqtt command (or at least to the address I am expecting it to)

I have used mosquitto on the same PI as HA, and also installed it on a separate PI and re-referenced everything, but got the same results whereby it works when you call a service mqtt.publish, but not through clicking the “Power On” icon to “on”

I am assuming I am missing a link between the two, but cannot for the life of me work out what that is

Many thanks for taking the time to help

configuration.yaml snippet:

mqtt:
broker: 192.168.0.xx
port: 1883
username: xxxxxx
password: xxxxxxx

switch:

  • platform: mqtt
    name: “Power On”
    command_topic: “cmnd/sonoff/rfkey1”
    payload_on: “”
    payload_off: “rfkey1off”

binary_sensor:

  • platform: mqtt
    name: “Doorbell”
    payload_on: “4906099”
    payload_off: “4906099off”
    state_topic: “tele/sonoff/RESULT”
    value_template: “{{ value_json.RfReceived.Data}}”

To help people check your configs please quote them as per the blue box at the top of the page. Helps to ensure indentations etc are correct.

What exactly “works as well” when the payload is an empty-string? Are you saying neither command, an empty-string or “rfkey1”, will turn on the light? I believe that.

If “rfkey1off” is the command to turn it off, what’s the command to turn it on? Perhaps “rfkey1on”? Because I doubt it’ll turn on upon receiving an empty-string.

Apologies for the formatting, here are the relevant bits again

If I call the mqtt.publish service in developer tools and send

{ “topic”: “cmnd/sonoff/rfkey1”, “payload”: “rfkey1”}

then the socket turns on.

If I send

{ “topic”: “cmnd/sonoff/rfkey1”, “payload”: “”}

ie I leave the payload blank, ie “”, then this also switches the socket on

“rfkey1off” is just random payload. To turn the socket off I currently use a rfkey2 switch command with all the code the same as “rfkey1”

As i am using a sonoff rf bridge there is no space between rfkey and the key number (that produces separate commands such as learning mode etc), so it seems I cannot separate the topic from the payload. I have tried all variations for the topic and payload to no success

The bit I dont understand is that it is successful when i manually call mqtt.publish service, but when operating the switch icon on HA overview why isn’t this command published to the mqtt broker the same way?

I hope this clarifies my issue

Thanks

mqtt:
  broker: 192.168.0.xx
  port: 1883
  username: xxxx
  password: xxxx

switch:
  - platform: mqtt
    name: “Power On”
    command_topic: “cmnd/sonoff/rfkey1”
    payload_on: “”
    payload_off: “rfkey1off”``

Publishing an empty-string to cmnd/sonoff/rfkey1 will turn on the light? Well, that seems like atypical behavior. According to Tasmota documentation, a payload of empty-string will cause it to report its status.

From Tasmota’s Wiki:

Side Note: For many commands, an empty payload is a query . If you are using mosquitto_pub , you can issue an empty payload using the -n command line option. If your MQTT client cannot issue an empty payload, you can use the single character “?” instead.

Managed to solve problem, simple as it turns out.

I used the

mosquitto_sub -v -h broker_ip -p 1883 -t ‘#’

to listen to all mqtt messages on all topics and found the when operating the switch the “” were included.

So I was listening for topic

cmnd/sonoff/rfkey1

but was actually publishing

“cmnd/sonoff/rfkey1”

So I have got around that issue

The empty payload issue I suppose I wasn’t sending an empty payload but “”, which does actually still turn the switch on

Many thanks