HA and Sonoff (Basic) Not Toggling State

Hi, yet another HA newbie here, stuck and asking for help.I have read and tried a lot of suggestions in other posts talking about similar problems, but none seem to work for me, hence this post.

I have HA, Mosquitto and Python all running ok on my Pi 2 B. (all fully up to date). The sonoff is flashed with Tasmota 5.4

The problem I have, is that when I use HA to ‘Toggle’ the sonoff , the led just flashes, but does not change state. The sonoff console log records the response as {“Command”:“Unknown”}.

If I set up two puTTY sessions, one as mosquitto_sub and one as mosquitto_pub and send a command to the sonoff, it works ok, but if I use HA, the mosquitto_sub shows the payload as on or off appropriately, but the sonoff led falshes but does not change state.

My configuration.yaml is

  - platform: mqtt
    name: "Master Bedroom Light"
    state_topic: "sonoff/master_bedroom_1/status"
    command_topic: "cmnd/sonoff/master_bedroom_1/switch"
    qos: 1
    payload_on: "Turned ON Main Light"
    payload_off: "Turned OFF Main Light"
    optimistic: false

puTTY pub and sub commands are:

$ mosquitto_pub -h hassbian -u <user> -P <password> -t cmnd/sonoff/master_bedroom_1/power -m "2"

$ mosquitto_sub -h hassbian -u <user> -P <password> -t cmnd/sonoff/master_bedroom_1/# -v

Any suggestions to get the sonoff behaving it’s self will be most gratefully received.

Your command_topic is different in your command and your configuration.yaml, also your state_topic starts with sonoffand your command line on putty starts with cmnd

PS: Are you sure your payloads have to be that long? Maybe they have to be something like “ON” and “OFF”

Thanks for your reply. The confiuration.yaml should be power, but in trying to solve the issue i have tried power, light and lastly switch, all of which show the same problem. I copied the .yaml just before i put it back to power, sorry for the error. The payload will be put back to ON or OFF, but i changed it to try to make sure i was seeing the right data in the puTTY screens.
I have put cmnd into the state topic, and restarted HA, but the problem still exists.:cry:

It is unclear to me exactly what topic and payload you expect to turn the payload on or off, and whether or not it works with using mosquitto_pub.

Do you have the sonoff working at all? If so with what command?

H, If I use:

$ mosquitto_pub -h hassbian -u <user> -P <password> -t cmnd/sonoff/master_bedroom_1/power -m "2"

the sonoff will toggle fine. and the puTTY sub screen shows

cmnd/sonoff/master_bedroom_1/power 2

If I use HA to try to toggle the sonoff with configuration.yaml as below ( or with cmnd/, sta/t, or nothing prefixing sonoff in the state_topic)

  - platform: mqtt
    name: "Master Bedroom Light"
    state_topic: "stat/sonoff/master_bedroom_1/status"
    command_topic: "cmnd/sonoff/master_bedroom_1/power 2"
    qos: 1
    payload_on: "Turned ON Main Light"
    payload_off: "Turned OFF Main Light"
    optimistic: false

I get

cmnd/sonoff/master_bedroom_1/power 2 Turned ON Main Light

and the sonoff led flashes once but does not toggle the sonoff and the message

stat/sonoff/master_bedroom_1/RESULT = {"Command":"Unknown"}

appears in the log,

The payload description is only so I can track which method triggered the message in the _sub screen

Hoping that’s a clearer explanation, as I am not finding it easy to explain.

TIA for your patience

Good. The first part of the message showing is the command_topic cmnd/sonoff/master_bedroom_1/power and the second part is the payload 2, so your yaml should be

  - platform: mqtt
    name: "Master Bedroom Light"
    state_topic: "stat/sonoff/master_bedroom_1/status"
    command_topic: "cmnd/sonoff/master_bedroom_1/power"
    qos: 1
    payload_on: "2"
    payload_off: "1"
    optimistic: false

I have assumed that 1 is used to turn the sonoff off, if not replace the payload_off field with whatever you send to do that.

The second part is the state_topic, which should be sent by the sonoff after it is turned on or off. This should match the state_topic: field exactly. The payload should also be the same as for the commands on or off. If HA doesn’t get the matching status message shortly after sending the command, it resets the UI back to its previous state.

Let us know if it works.:slight_smile:

Edit:
Rereading your post, the 2 payload might be toggling the sonoff, rather than turning it on, in which case you need to replace the payloads with whatever turns it on and off, rather than toggle. These are probably 1 and 0, but you might have to look at the sonoff to find out.

Thanks for the reply.
I copied info into the config.yaml and restarted HA and the sonoff now does what it should. I also tried the payload on / off with 1 & 0 but this didn’t work.

Thank you for your help. I’m afraid that the biggest part of my problem was me, I thought the payload “ON” / “OFF” were a human messages, not machine one’s!!:cowboy_hat_face:

Thanks Aagain

1 Like

GPBenton

Just a quick update. Your edited comments are correct. after I rebooted HA, I tried again and

0 turns OFF the sonoff
1 turns ON the sonoff and
2 Toggles the sonoff.

Cheers

1 Like