MQTT config... what am I not seeing?

I’m trying to add a MQTT switch. It’s not the first one, but I don’t know why I can’t make it work.

Here is the config I use

  - platform: mqtt
    name: "aire corte"
    state_topic: "stat/sonoff_aire/POWER1"
    command_topic: "cmnd/sonoff_aire/POWER1"
    availability_topic: "tele/sonoff_aire/LWT"
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    optimistic: false
    qos: 1
    retain: true    

It is exactly the same config I have for the rest of the switches I have based on Tasmota as well.

But, if I go to services and issue this message through mqtt.publish it works perfectly

{
"topic": "cmnd/sonoff_aire/POWER1",
"payload": "ON"
}

Why is not working from HA, but it is from MQTT? I have tried so many things and I’m starting to get blind…

What does your mqtt log say when you try to activate the switch from the front end?

try retain: false

I didn’t see anything posted in the log

I’m out now, but the rest of the MQTT switches I have are with retain: true…

retain wont stop it working but may cause you greif with unexpected on / off events.

I’ll test it when I’m back (I left it unplugged) and will report back.

I can’t see any issue with your config considering the service you called with the dev tools.

Very strange.

Ok, what I’m seeing is that it’s not updating the state. If the sonoff is off and I click on it it will turn on, but the state is not changed in HA. so I can’t turn it off again.

I’ve been playing with the retains and such as recommended in the video, but nothing changed.

I have tasmota 6.5.0 installed and its a TH16

Ok, fixed, I had to use state_topic: "stat/sonoff_aire/POWER" instead of POWER1

There is a Home Assistant auto discovery config in Tasmota. It is much easier to set up, it will always set up the correct entities and most of all, it will set up all of the available entities.
E.g. if you have a TH-16 it means you will have a switch and 2 sensors, if configured individually this means you will need 3 config entries. With discovery only the discovery config is needed and it will send all 3 entities to HA.
Entities will then just popup in your setup, no restart required and if you add new devices, all you have to do is configure it from the sonoff web interface.

For this you will need to do everything in the config of tasmota (webinterface)
To do this you will have to reset the sonoff to a basic module and restart.
Then go to console and type the following: SetOption19 on
Now change the module to the desired device (in your case TH16)
Change the mqtt config and set it to your own
Restart once more and voila your device is ready for use. To check if it actually worked go to the configuration screen in your Home Assistant setup and go to integrations. Click on the mqtt integration and find your new devices there.

This is the simplest way to do it as far as I know and a great way to use if if you want to swap around your sonoffs later on (as you do not need the individual setup anymore). Just change from tasmota and ready!