I have this set up on another home automation system and needed to set the “SwitchTopic” in the console. The default on my Sonoffs seems to be 0. I set it to 1 which sends a message with the default topic to MQTT. I have not tried this on HA yet so hope you will update this post with your success.
I just studied my response and I guess it wasn’t too helpful to you? ; ) If you are seeing the MQTT output in the console then you must have the “SwitchTopic” set to 1 already. Sorry. It may help someone else though.
From what you posted, it looks like all you’ve done is, instead of triggering the relay directly with gpio14, you’ve now set it so that it does it via the Mqtt server.
To help with fault finding, comment out or delete the lines in your configuration relating to availability until your confident that the rest of the setup is working.
I have no clue why state topic is not showing up in the console screen. Instead, command topic is being listed.
On the other hand when i use the relay of sonoff, state topic gets listed:
07:41:37 MQT: stat/sonoff1/RESULT = {“POWER”:“ON”}
07:41:37 MQT: stat/sonoff1/POWER = ON (retained)
07:41:41 MQT: stat/sonoff1/RESULT = {“POWER”:“OFF”}
07:41:41 MQT: stat/sonoff1/POWER = OFF (retained)
I came across this problem as well, so I think I know what is wrong here.
Your sensor will be shown as ‘unavailable’ as long as HA has not received a message of
"Online"
on the topic you set:
"tele/alarm/LWT"
I am not sure if you are doing it as well, but I fed my sonoff with my own code.
I set up a retained will message with “Offline” before the sonoff connects with the broker.
After it connected it publishes “Online”.
If you set it up like this, your online/offline state should work like a charm and your sonoff represented correctly.
Heres my code:
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
blink_now();
// Attempt to connect
if (client.connect(CLIENTID, MQTT_USER, MQTT_PASSWORD, MQTT_AVAILABILITY_TOPIC_OUT,2,1,"offline"))
{
Serial.println("connected");
// Once connected, publish an announcement...
client.publish(MQTT_AVAILABILITY_TOPIC_OUT,"online", true);
subscribe_to_topics();
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
The Sonoff-Tasmota firmware does not publish the state of the switch in any way! It only send commands (directly to the corresponding relay or via MQTT to a topic).
It seems correct that the sonoff sends a command ON or OFF as in the console log.
My guess it that the Sonoff is publishing the availability only on the main topic (the one controlling the load) and not on the secondary switch topic “alarm”. @ravi_09x you should try to change only the availability_topic to the one you are using in the config for the switch.
@namadori - Thanks for the pointer. I’ve been working on this problem for 2 days not (sad I know but I am new to this).
Switching the reed sensor to Switch Topic 2allowed me to get the sensor to send separate topics enabling me to change the status of my sensor to “open” and “closed”.
So far it seems that sensor status is updated by telemetry only.
I have SwitchMode changed to 2 and SwitchTopic to Alarm.
If sensor state chanded, on console is cmnd/Alarm2/POWER1 = ON only, no stat/Alarm2/POWER1 = ON (retained) the status on UI changes after tele/sonoff2/SENSOR = {"Time":"2018-03-02T08:21:39","Switch1":"ON"} is sent by sensor/sonoff2 respectively