Hi,
I am trying to add a Sonoff basic to my HA setup. have updated the sonoff to tasmoto and configured MQTT. I can publish to a topic and turn on and off the sonoff. I can also have a mqtt subscribe to the Stat and receive that when ever the sonos is changed.
The problem is when I slide the switch in HA, the sonoff turns on, but a second or two later the switch on HA slides back to the off position, but the sonoff stays on.
I have the power retain on the sonoff set to 0 code form tasmota config below
#define MQTT_BUTTON_RETAIN 1 // [ButtonRetain] Button may send retain flag (0 = off, 1 = on)
#define MQTT_POWER_RETAIN 1 // [PowerRetain] Power status message may send retain flag (0 = off, 1 = on)
#define MQTT_SWITCH_RETAIN 1 // [SwitchRetain] Switch may send retain flag (0 = off, 1 = on)
my switch.yaml is a below using the MQTT switch component
This normally happens if HA does not receive the state_topic: message shortly after sending the command_topic:. Since you have MQTT connected, check that the state topic and payload matches what is sent, and that the sonoff sends it after receiving the command by subscribing to the topic.with a command line tool, such as mosquitto_sub
$ mosquitto_sub -t "#" -v
will subscribe to all topics on the broker on the local host, so you can see what is being sent.
Please help, I´ve never felt so noob with anything, ever. I have no idea where to go, perhaps it has to do with the initial Tasmota´s configuration at flashing…
The first section of this is the state topic, and the second section, between the { } is the payload, which is in JSON format. So to get the state, you should have
That was from memory, so you had better check with the component documentation.
I can’t see any way to help with the command to turn the switch on. You will have to look at the code you put in the sonoff to see what it is expecting.
Perhaps I didn´t explain myself clearly. My problem is not subscribing, it must have something to do with config. HA shows the switch, but when I click it it returns to it´s initial state and nothing happens (switch doesn´t work)
You can’t expect to receive using this if sending via this…
command_topic: “cmnd/sonoff1/power” … this needs to be…
salon/sonoff1/cmnd in HA or change the sonoff to cmnd/sonoff1/power just remember the publish on HA needs to match the subscribe on sonoff and vice versa.
This happens when HA does not receive a state_topic: message after it transmits a command_topic: message. HA assumes that the message did not reach the sonoff and reverts to the previous state.
The are many reasons why it does not receive a state message including
The MQTT is not connected
The command_topic doesn’t match the sonoff
The payload doesn’t match what the sonoff expects
The sonoff is not sending a status message back
The sonoff is sending a status message but it doesn’t match the state_topic: or payload
Since the sonoff doesn’t switch when you send the command_topic set in HA, one of the first three is probably true.
Once you have that fixed and the sonoff turn on and off correctly when the HA switch is pressed, then you may have to look at the state_topic: to make sure that matches.
After a LOT of trial and error, I finally changed my config.yaml to:
switch:
platform: mqtt
name: “Lampara de pie”
command_topic: “salon/sonoff1/cmnd/power”
state_topic: “salon/sonoff1/POWER”
qos: 1
payload_on: “ON”
payload_off: “OFF”
retain: true
I don´t konw the “stat” part I was using (I took it from BRUH´s example on sonoff config), but obviously it wasn´t working.
I´ll finish flashing the other units I have, and post back the results, just for finishing purposes for the forum.
Thanks for sharing all this info. I’ve just received my first two Sonoff switches and I’m looking forward to getting them up and running. I too faced some issues with MQTT when I first setup a sensor. I think all the HA command and state toipics now make some sense.
Question I have for you is did you run into difficulty getting the sketch to verify and upload in Arduino IDE? I was working on this the other day in preparation for the switches arriving and I’m getting all kinds of errors in the verify while following BRUH’s instructions.
any advice you can offer would be greatly appreciated.
A ha, I see that the release needs to be 4.0.7 not the most recent one. All good on that side now. Hopefully I’ll have two Sonoff switches later tonight.
There are several ways to flash your sonoff. Visit www.superhouse.tv and search for the article about sonoff secrets.
You can also use Atom (a Hi-Tech text editor) with a program called Platformio. I find it more complete than Arduino Idea.
I wound up realizing that I had loaded the Tasmoto code with the wrong Sonoff number. When i subscribed to mosquitto and turned the switch on and off, it was showing sonoff4 when I thought it was sonoff5.
So the symptom of the switch turning on then reverting to off was because it was sending it to a device that didn’t exist.
Hi all,
I am trying everything I am able to try and still the same behavior of the hass.io switch - moves for 1 sec, than returns, no change at sonoff side at all.
Topic is correct I believe, to be more soecific I hope…
yaml:
switch:
Your command topic appears to be lower case power but according to the console 12:22:52 MQT: cmnd/sonoff/POWER = it should be POWER.
The sonoff doesn’t appear to be outputting a status topic on stat/sonoff/POWER but it is sending a periodic telemetry status update, what happens when you publish a message via MQTTBox for instance, do you get a status msg back? If you press the sonoffs button do you see an output in the console window and do you see a mqtt status msg in MQTTBox or whatever you are using to monitor it? You don’t appear to have set up the availability/LWT parts of the mqtt config in HA?
I went through all settings once more, the only thing is to be precise with “typing”
It works perfectly.
Now proceeding to the next level of the automation