Relay with MQTT commands issue

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.

You are absolutely right, that’s why I was asking for help.

Could you be a bit more precise in how to do it? It would be nice and I could learn from that …
Many thanks!

My point was that after all the help you’ve received, you continue to operate in a manner that shows little benefit from it. For example, your self-certified “solution” (restructuring the topic) is just a happy accident and not a recommended course of action for others; it’s a misleading solution.

MQTT Discovery is enabled by SetOption19.

Thanks for the help!