Door sensor with Sonoff Basic

I want to use the GPOI 14 as a magnetic door sensor input. By following various posts I have done the following:

  1. Configured GPIO 14 as ‘09 Switch 1’
  2. Set Switchmode to 2
  3. Created a switch topic as ‘Alarm’

So GPIO 14 is now independent from the relay and other controls(through HAss or native button) do not affect the GPIO input.

Getting this feedback from console when i switch GPIO14 states:
20:44:19 MQT: cmnd/alarm/POWER1 = OFF
20:44:20 MQT: cmnd/alarm/POWER1 = ON

I have created a sensor entity in my configuration file and not sure if this is the way to go.


  • platform: mqtt
    name: “DoorStatus”
    state_topic: “stat/alarm/POWER1”
    command_topic: “cmnd/alarm/POWER1”
    availability_topic: “tele/alarm/LWT”
    qos: 1
    payload_on: “ON”
    payload_off: “OFF”
    payload_available: “Online”
    payload_not_available: “Offline”
    retain: true
  • platform: yr

Now the sensor badge says ‘unavailable’.

Please help!

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.

So where is the response in the console that the state topic has been updated as that’s what HA listens for not the command topic.

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.

What firmware are you using on the sonoff ?

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)

Yes, that’s what i wanted. Because i don’t really want to use the relay but the door sensor.

I am using 5.11.1

I commented out the availability configs, still no response inn the HA

availability_topic: "tele/alarm/LWT"
payload_available: "Online"
payload_not_available: "Offline"

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


on the topic you set:


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...");
    // Attempt to connect
      // Once connected, publish an announcement...
      client.publish(MQTT_AVAILABILITY_TOPIC_OUT,"online", true);
    } else {
      Serial.print("failed, rc=");
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying

hope that helps!

From the Tasmota Wiki:

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.

Think you misunderstood what I meant, you are still switching the relay, just indirectly.

I think @ravi_09x used the command SwitchTopic 2 to use the input to publish to a different topic “Alarm” without triggering the load.
Check here:

@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”.

I got it working too. All i had to do was make the state topic as:
state_topic: “cmnd/alarm/POWER1”

I´ve solved it :grinning:
Sensor definition:

  -      platform: mqtt
         name: "Alarm"
         state_topic: "tele/sonoff/SENSOR"
         value_template: "{{ value_json['Switch1'] }}"
         unit_of_measurement: "ON/OFF"

Telemetry period on Sonoff module I´ve changed to 10 secs otherwise status change will be changed after predefined 5 mins.

Now it shows sensor named Alarm, showing status 10 secs after changed .

1 Like

If you need it, with SwitchMode and SwitchTopic you can have immediate feedback on change, without the delay and without the repeated status messages.

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

1 Like

There’s also now the SensorRetain command on tasmota

Whenever HA or Sonoff restarts the retained state of the sensor is published again so HA knows if it’s opened or closed

1 Like