Cannot get MQTT work with door reed sensor

HI All ,

I am tryig to configure HA with NodeMCU and Reed door switch. I followed this guide. https://medium.com/@patrickeasters/integrating-existing-home-security-sensors-with-mqtt-6d41f09ff41d

NodeMCU part is done and working, but for the love of GOD, I cannot get HA to recognize the sensor via MQTT. The moment I place below code . I loose complete web access to HASS.IO. All other mqtt devices are working but this. Node MCU is working good as I can see terminal output in ESPlorer. But HA does not show anything when I try to add the mqtt code. The configuration checker says all valid!, but when I restart HA I loose complete access to web interface for hass.io. Unless I remove the below code then all is back nicely. Please assist.

  • platform: mqtt
    state_topic: “pat/alarm/Front Door”
    name: “Front Door”
    payload_on: “open”
    payload_off: “closed”
    device_class: opening

my mqtt component is as followes

mqtt:
broker: 192.168.1.174
port: 1883
client_id: node1
username: username
password: password
discovery: true
discovery_prefix: homeassistant

Have you configured it as binary_sensor ?
Your code doesn’t show this.
And please read the blue box on top of every page here how to format codeblocks.

ok yes forgot the binary_sensor, this gave me a small circle at the top of the page, nothing like the tut I followed. see image (https://raw.githubusercontent.com/patrickeasters/smart-house/master/_etc/img/outside.png?v1) Also the icon I get at the top always stays at open, even when I close the reed switch. I however can see “Pin 1 has changed to 1” and “Pin 1 has changed to 0” status in ESPlorer meaning the read switch and nodeMCU are working fine. What else is missing form HA yaml file … thanks for such a prompt reply.

regards
Sammy

Is the spacing shown what you have in yaml file?

If HA stops working when component added to config files that is normally result of space issue or misplaced quotes. I forget proper spacing but below seems right

platform: mqtt
  state_topic: "pat/alarm/Front Door"
  name: "Front Door"
  payload_on: "open"
  payload_off: "closed"
  device_class: opening

mqtt:
  broker: 192.168.1.174
  port: 1883
  client_id: node1
  username: username
  password: password
  discovery: true
  discovery_prefix: homeassistant

EDIT
Placing blank space after text also causes HA not to start or component to fail to start

If you want your sensors in cards, you have to group them.
Are you sure the NodeMCU publishes to your mqtt broker and the right topic?
Verify this with a mqtt client.

Ok small progress. I made few changes in the code

binary_sensor:

  • platform: mqtt
    state_topic: “pat/alarm/Front_Door”
    name: “Front Door”
    payload_on: “open”
    payload_off: “closed”
    device_class: opening
    retain: true
    qos: 0

Now for testing I am using mosquitto cmd utility and when I type the below command on command line I get the response correctly in HA

mosquitto_pub -h 192.168.1.174 -t pat/alarm/Front_Door -m “open”
mosquitto_pub -h 192.168.1.174 -t pat/alarm/Front_Door -m “closed”

in both the state I see HA correctly displaying open and close door. BUT, via actual Reed switch it still is not working. When I bring the reed switch in close position I can see the message in ESPlorer “Pin 1 has changed to 1” and when I remove the reed switch I see “Pin 1 has changed to 0” BUT the same is not communicated to HA. however if I run manual mqtt command HA responds …

Also sharing my config.lua file

– let’s define the pins
– Map the GPIO pin number to a MQTT topic
map = {}
map[1] = “Front_Door”
map[2] = “Back_Door”
map[5] = “Garage_Entry”

– MQTT config
topic_prefix = “pat/alarm/”
status_topic = “status”

– Map MQTT payloads
payload = {}
payload[0] = “closed”
payload[1] = “open”

No idea where and what I am missing

Please assist?

Do you see any messages on the given topics if you use the mosquitto_sub client?
mosquitto_sub -h 192.168.1.174 -t "#" -v
If not, there’s something wrong with the lua config.
I don’t see any value for the mqtt broker adress.

Below is the response …

core-ssh:~# mosquitto_sub -h 192.168.1.174 -t “#” -v
pat/alarm/Front Door closed
pat/alarm/Back Door closed
pat/alarm/Garage Entry closed
pat/alarm/Front_Door open
pat/alarm/Back_Door open
pat/alarm/Garage_Entry open

I dont understand the first three. And yes even I think something is wrong in the lua config. Let me see if I can re download the original and re edit and re upload. Will report …

ok friend …I got things to work. With a tiny issue still there, First of all for those who might be stuck in the same problem, The issue was with the init.lua, where the command “wifi.sta.config(SSID,PSK,0)” had to be changed to "wifi.sta.config {ssid=“SSID”, pwd=“SSIS paswword”}. After that all worked good.
BUT I have a TINY issue … The state of door on HA is reversed, meaning when I close the reed switch it shows open and vice versa. I thought I need to simply reverse the payload in config.lua file and also reverse the same in yaml file so the changes config became as bwlow …

– let’s define the pins
– Map the GPIO pin number to a MQTT topic
map = {}
map[1] = “Front_Door”
map[2] = “Back_Door”
map[5] = “Garage_Entry”

– MQTT config
topic_prefix = “pat/alarm/”
status_topic = “status”

– Map MQTT payloads
payload = {}
payload[0] = “open”
payload[1] = “closed”

yaml code

binary_sensor:

  • platform: mqtt
    state_topic: “pat/alarm/Front_Door”
    name: “Front Door”
    payload_on: “closed”
    payload_off: “open”
    device_class: opening
    retain: true
    qos: 0

which is reversed to what was earlier . BUT BUT this did not change anything. in HA it still was reversed. Closing the reed shows open and vice versa.

Can you assist me in this last step …

Regards
Sammy

Well, if you change the payload in lua and HA, it’s the same as before.
Just change it in lua and leave it in HA as it was before. :slightly_smiling_face:

hehe …ok now I got it … thanks … I made the changes and now all is good… THANKS A TON for staying there and helping me out …people like you are the soul of these communities …:slight_smile:

BTY a last suggestion if you can give. Now I want the notification on my mobile, what the best approach. any notification component from HA should I use or IFTTT is the better approach, Basically whats the best approach to get this done. Thanks again … :slight_smile:

Regards
Sammy

There are many notification components. That’s your decision.
I’m using the ios app for notifications.

thank you …will give a shot … :slight_smile: