[FIXED] Door sensor home assistant



Is there as new guide how to add the door sensor in home assistant? I can only find openhab guides.



I just used a generic MQTT sensor and added to my configuration.yaml file in the sensors section.

- platform: mqtt
  name: DoorSensor1
  friendly_name: My Door
  state_topic: "house/door"
  state_open: "OPENED"
  state_closed: "CLOSED"
  optimistic: false
  retain: false
  value_template: '{{ value }}'

Make sure that when you add the above to your config files that you match the MQTT topics as chosen when you setup your door sensor as shown in the videos. I also like to define human readable text for states hence the OPENED/CLOSED text so you have to expose it to Home Assistant with a value template as shown. I set the retail to false as I always want the sensor to update and never assume a previously saved state. Hope this helps.


Thanks! I added the code to my configuration.yml file but it says it not a correct configuration:
end of the stream or a document separator is expected at line 117, column 1:
- platform: mqtt


YAML is extremely particular about spaces and indentation. You need to make sure that when you cut and paste yaml code that everything “lines” up in your config file. What this error is indicating is that the initial dash, as shown is not indented and in an expected location in your config file. If you wish to copy/paste the surrounding lines I can probably give you more assistance.


Thanks for the update, i changed it and now i get another error.

  - platform: mqtt
    name: Doorsensor1
    friendly_name: My Door
    state_topic: "Doorsensor1"
    state_open: "OPENED"
    state_closed: "CLOSED"
    optimistic: false
    retain: false
    value_template: '{{ value }}'


Invalid config for [switch.mqtt]: required key not provided @ data[‘command_topic’]. Got None. (See ?, line ?).

But i dont need the command topic right?


Moved it to the sensor part in the configuration file. it is now showing on top.

Only thing, if i restart the HASS it is not getting the current state. So i have to open the door for an update. is there a script for that?


Oops! I make the assumption that the code would be placed under a key in your config file. You should have the code under a main section (key) like so:

  - platform:  mqtt

At that point you can then just use the code block as I originally provided.

If your config file uses included config files to break up the main config file you will probably see something similar to
sensor: !include sensors.yaml

If that is the case, you need to post the code into the indicated file.


This most likely can be controlled by the retain settings for mqtt. In my sample script I have retain set to false as I normally set most of my sensors to poll the sensor and update mqtt on a set interval. You can force the status to be the last known state by setting retain to true in the config block.

This should resolve your issue with a restart of HA showing the last saved state until the next update is posted. Do note that this can cause an issue if the state of the sensor changes during the reboot time, or more likely, a disruption in connectivity. Resolving that would require going into the INO file and changing a few settings to alter how the sensor posts updates to the mqtt server.

For more in depth discussion on retain states and mqtt see the following two YouTube videos of Rob’s from The Hook Up. I realize that the first one references the Tasmota firmware but it is packed with relevant information with respect to mqtt based IoT devices.


Thanks a lot! It’s okay how it works now.
It will just update when its activated again.

Now i have another issue with my led strip from mk smarthouse, but i can only find scripts for openhab not for home assistant.
I was trying to add some code and figure it out but i cant get it to work…


All discussions on the LED strips for Home Assistant should be here Adding ledstrip to Home Assistant HASSIO and not in the Door sensor topic.


Hi @freedog96150 @dave

I am also trying to add door sensor to home assistant.

Host Operating System Home Assistant OS 6.4
Installed Add-ons Node-RED (10.0.1), Mosquitto broker (6.0.1), AirCast (3.1.1), Terminal & SSH (9.1.3), File editor (5.3.3), Samba share (9.5.1), Duck DNS (1.13.0)

Mqtt Logs
1632634310: New client connected from as MainDoorSensor1 (p2, c1, k10).

when I used following code
platform: mqtt
name: “Main Door”
state_topic: “Raiba-Smarthome/security/MainDoorSensor1”
state_open: “OPENED”
state_closed: “CLOSED”
optimistic: false
retain: false
value_template: ‘{{ value }}’

Getting Error: Component error: Sensor - Integration ‘Sensor’ not found.

please suggest.


Syntax in yaml is very particular. If the code you posted above is in your main configuration.yaml file as shows, it will fail. You need the correct indentation AND the correct locations of the various lines.

Assuming that all in in the configuration.yaml file you would need to indent everything under the sensor: and if multiple sensors, you need to preface each platform with a - so that it looks like - platform: mqtt with all indentations lined up under the dash.

If your sensor file is a separate file, then you no longer need to specify sensor each time.

This can be a tough topic to explain when we do not know how you have your configuration files setup. Sorry if this does not clear up the confusion but creates more.


This is my Configuration.yaml file. can you help me what should should i mention in separate sensor file and if can corelate that in main file.

Configure a default setup of Home Assistant (frontend, api, etc)


base_url: raibasmarthome.duckdns.org
ssl_certificate: /ssl/fullchain.pem
ssl_key: /ssl/privkey.pem

discovery: true
platform: mqtt
name: “Hall Fan”
command_topic: “cmnd/hallfan/POWER”
state_topic: “stat/hallfan/POWER”
qos: 1
payload_on: ON
payload_off: OFF
retain: true


platform: mqtt

name: “MainDoorSensor1”

qos: 0

state_topic: “Raiba-SmartHome/security/MainDoorSensor1”

state_open: “OPEN”

state_closed: “CLOSED”

title: TasmoAdmin
icon: mdi:lightbulb-on

Text to speech


  • platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml


@mattkaczynski I Help, I am already feeling helpless.


are you saying below will proper code under configuration.yaml.

-platform: mqtt
name: “Main Door”
state_topic: “Raiba-Smarthome/security/MainDoorSensor1”
state_open: “OPENED”
state_closed: “CLOSED”
optimistic: false
retain: false
value_template: ‘{{ value }}’