Relay with MQTT commands issue

Situation:
I put together a tasmotized Wemos D1 Mini and a relay in order to control some lights.
The system is working: from the web page of the wemos I can turn it on and off.
I put the following code the configuration.yaml:

  - platform: mqtt
    name: "Light"
    icon: mdi:lightbulb
    state_topic: "tele/sonoff5/POWER"
    state_on: "ON"
    state_off: "OFF"
    command_topic: "cmnd/sonoff5/POWER Toggle"

The issue:
The entry reacts well to all the changes (if I turn it off, icon goes off - if I turn it on, it goes on), BUT I can`t command it from the dashboard: as soon as I try change the status, it turns back to the previous one. What is wrong in the MQTT command?
Many thanks

I think

    command_topic: "cmnd/sonoff5/POWER"

Here a short feedback from the console:

    13:44:47 CMD: stat/sonoff5/power
    13:44:47 MQT: tele/sonoff5/RESULT = {"POWER":"ON"}
    13:44:47 MQT: tele/sonoff5/POWER = ON (salvato)
    13:45:35 CMD: Cmnd/sonoff5/power
    13:45:35 MQT: tele/sonoff5/RESULT = {"POWER":"ON"}
    13:45:35 MQT: tele/sonoff5/POWER = ON (salvato)
    13:46:17 CMD: Cmnd/sonoff5/power Toggle
    13:46:17 MQT: tele/sonoff5/RESULT = {"POWER":"OFF"}
    13:46:17 MQT: tele/sonoff5/POWER = OFF (salvato)
    13:46:22 MQT: tele/sonoff5/RESULT = {"POWER":"ON"}
    13:46:22 MQT: tele/sonoff5/POWER = ON (salvato)

The command POWER gives me the same as “power on”, while “power Toggle” turns the relay on/off according to the state of the relay. However it works only from the Tasmota web page, not from homeassistant.
Where is the mistake? In the code? In the Tasmota setup?

This is how I have mine:

- platform: mqtt
  name: "Outside Light"
  state_topic: "stat/sonoff01/POWER"
  command_topic: "cmnd/sonoff01/POWER"
  qos: 1
  payload_on: "ON"
  payload_off: "OFF"
  retain: true

Hi Bluey!
Thanks…
But it doesn’t work. In this way I don’t have depicted in HA the right state of the relay and I cannot turn the moduli on/off from HA
Just a question: how do you setup the moduli in tasmota?
Here is what I did …


Thanks

Ah my bad here, apologies I just realised this is on wemos D1 mini, my yaml was for a sonoff basic so will be slightly different on the config page in Tasmota due to the differing GPIO pins etc.

Could you post your configuration please? I will adapt mine accordingly …

which config would you like me to post?

The MQTT’s one of the sonoff basic module that you are using … (like the picture I posted) … let see if I could receive some inspiration

If you don’t specify the payload values, default values will be used (see documentation). That means the default “ON” and “OFF” messages are published to the command_topic.

I recommend you try @nickrout’s suggestion and change the command_topic to "cmnd/sonoff5/POWER"

ok here is a snapshot from the console:

And here is a snapshot of the module config page:


Note GPIO14 is in use as I have a momentary switch to also add physical switch operation of the module.
Is this what you need?

Thanks.
This is the version I am using:

- platform: mqtt
    name: "Semenzaio"
    icon: mdi:lightbulb
    state_topic: "stat/sonoff5/POWER"
    command_topic: "cmnd/sonoff5/POWER"
    payload_on: "ON"
    payload_off: "OFF"
    qos: 1
    retain: true

This is how it is shown in HA

28

and this is what, at the same time, the tasmota web page reports.

No, not really. I will not use a phisical switch …
But maybe I have to change the GPIO I am using … (?)

That doesn’t surprise me. You set retain: true. The MQTT broker now stores (retains) the command topic’s message. Whenever the Wemo D1 connects to the broker, it will receive the last message published to the command topic (independently of Home Assistant).

You should purge the message from the command topic and then set retain: false. Here’s an easy way to purge a retained message:

OK. Got it and changed it in the code and in the module by

switchretain off
buttonretain on
buttonretain off

but still confused because:

  • if I put

state_topic: "stat/sonoff5/POWER"

it doesn’t change the status of the relay in HA when I turn it on/off from the switch page. On the contrary, it works fine with:

state_topic: "tele/sonoff5/POWER"

  • when I change the status in HA, it turns for a second and then come back as it was … meaning the broker is still retaining (and I did it wrong?)

Whenever debugging MQTT issues, I always recommend using an MQTT client to observe topic messages. You want to confirm the message is what you expect. Don’t just blindly try a thousand and one things and hope one of them works.

Use an MQTT client, like MQTT.fx or MQTT-spy or even mosquitto_sub, subscribe it to a topic, like stat/sonoff5/POWER and tele/sonoff5/POWER, and observe what is received when you turn the device on/off (using its web page).

OK. Thanks, will do it

SOLVED!

  - platform: mqtt
    name: "Semenzaio"
    icon: mdi:lightbulb
    state_topic: "%topic%/sonoff5/POWER"
    command_topic: "%topic%/sonoff5/cmnd/POWER"
    payload_on: "ON"
    payload_off: "OFF"
    qos: 1
    retain: false

:grinning::heart_eyes::muscle::+1:

1 Like

Sorry but changing the topic’s structure isn’t a “solution”. Many people successfully use the default topic structure.

In addition, %topic% is a tasmota token (variable) and is used to customize the topic (see documentation). It’s not normally meant to be used literally.

The fact you needlessly changed the topic structure and included a tasmota variable (without actually using it as a variable) suggests you don’t have an adequate grasp of MQTT or tasmota. All you achieved was to ensure the state_topic was identical in Home Assistant and the device (don’t have to change its structure to achieve that).

I strongly caution others not to assume the reported “solution” is in fact any kind of solution at all.

Why not just use MQTT discovery and let the software do the config for you. It’s what computers are good for. Program and forget.