Devember2020 - Door Sensors Home Automation

Hello I thought I’d toss in my possibly entry to Devember 2020, I don’t think this will include a lot of coding, I know the raspberry Pi python coding can be extremely simply to use. But its a project I’ve had most the pieces for but simply never did.

The Problem
I live in BC. Canada. So we have lots of problems with bears, cougars, raccoons etc. We have an attached shed (basically a garage) with a chest freezer and garbage containers. Biggest issue we have is people in the house seem to forget to close this door. Which is extremely dangerous being in a bear zone.

The other being in a Canadian home in winter, overnight sometimes the doors won’t be completely closed, or during the day when people let the dogs out, they will forget to close the door and just leave the screen door closed. In the summer its fine, in the winter… to put it lightly parts of the house become freezing. So instead of getting in fights with family members, and being annoyed when I find doors have been left open. My plan is to make a Monitoring system, that I can check, get notifications on, or on a display somewhere that shows the status of the doors.

The Setup:
I know a super easy setup would be buy a zwave or wireless door sensor. But I hate most IoT devices, and I don’t feel like having them communicating with China.

So, using 3x Reed Switches (typically used in security systems), with a single Raspberry Pi connected to a UL in wall wiring (will be going through some walls), powered by a PoE ethernet connection, have it monitor the switches and send the information through a MQTT payload to a VM Running Home Assistant.

The Goal:
To be able to view the status of the doors on home assistant web portal.
Have my phone be notified or my computer notified that the shed, backdoor or front door has been open for an extended period of time (say 5 minutes)
Possibly make a display showing status that can be easily glanced at to know status of doors say in the kitchen or bedroom.
Have it be isolated as possible from the rest of the network.
Be as open source and secure as possible. Be as DIY as possible, because it seems like a fun project
Be cheap as possible, no subscriptions no extra frees.
To help avoid conflict with family. Ha

Possible issues:
I don’t know for sure if there will be an easy way to get push notifications on my phone without using the Home Assistant App (because I believe that costs money).

Not entirely sure if Home assistant has the ability to send emails or notifications, so the raspberry pi might need to be the device that actually sends the notifications.

I may run into problems, where home assistant thinks the doors are still open even if they are closed and vice versa. So it would need an update checker? After 2 minutes verify if the doors are still open or still closed, and not only rely on a single mqtt payload to be received. Just for some added redundancy.

So that’s my project for this Devemeber, hoping to get this done :3


I am not a HomeAssistant expert but I do use it.
As far as push notifications, you can setup Home Assistant for external access with out paying for it. There is a wiki on how to do it on the site. I will tell you that it is just easier to pay for the service (5USD) a month. That pays for active development, hosting, and bug fixes/feature releases. Support open source.

Where you are going to spend most of your time is in the automation section. This is where you can setup rules and device interactions.

If you setup external access to your Home Assistant, you could use Google, Alexa, or Siri to send you notifications.

Lastly, you can run a mail server or have Home Assistant sent email to you via the add-ons section. You could also just setup mutt on your pi and use the script section of Home assistant to use that to send email.

The site is very detailed and there are videos everywhere to cover what you are looking to do. Good luck.and how often

So as an update because its the 30th of December. The functional Code and pictures of the door sensors working.

I spent a very long time trying to understand and design a custom android app to get notifications on it through Firebase. I did get notifications to work. But after mucking around with it, I just made a phillips bulb change colour instead of dealing with phone notifications. Simply because I don’t think I want to go through firebase, or google based options. (I don’t need google to know everytime a door in my house has been open for longer than 1-5 minutes)

I’m not an amazing programmer, so I have some likely ugly code in python to read the inputs, activate LED’s based on those inputs, and send through mqtt the state of those sensors. I installed python, installed the pip installer, and installed paho-mqtt through pip. To send the mqtt code to the HassIO Home Assistant.

The Python Code:
import RPi.GPIO as GPIO
import time
import paho.mqtt.client as mqtt

gPin = 24 #Green LED Pin
rPin = 23 #Red LED Pin
bPin = 18 #Blue LED PIN
fiPin = 17 # Front Door Input
biPin = 27 # Back Door Input
siPin = 22 # Shed Door Input
user = “USER”
passwd = “PASSWORD”
frPublish = “pi/doors/front”
bkPublish = “pi/doors/back”
sdPublish = “pi/doors/shed”
openPayload = “1”
closePayload = “0”

GPIO.setup(rPin, GPIO.OUT)
GPIO.setup(gPin, GPIO.OUT)
GPIO.setup(bPin, GPIO.OUT)

GPIO.setup(fiPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(biPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(siPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

haClient = mqtt.Client()
haClient.username_pw_set(user, passwd)

haClient.connect(broker, 1883)

frcheck =0
bkcheck =0
sdcheck =0
while 1:
#Front Door Input:
if GPIO.input(fiPin):
GPIO.output(gPin, GPIO.HIGH) # Green Led On
if frcheck != 1:
haClient.publish(frPublish, openPayload)
frcheck = 1
GPIO.output(gPin, GPIO.LOW) # Green Led Off
if frcheck !=2:
haClient.publish(frPublish, closePayload)
frcheck = 2
#Back Door Input:
if GPIO.input(biPin):
GPIO.output(rPin, GPIO.HIGH) # Red Led On
if bkcheck !=1:
haClient.publish(bkPublish, openPayload)
bkcheck = 1
GPIO.output(rPin, GPIO.LOW) # Red Led Off
if bkcheck !=2:
haClient.publish(bkPublish, closePayload)
bkcheck = 2
#Shed Door Input:
if GPIO.input(siPin):
GPIO.output(bPin, GPIO.HIGH) # Blue Led On
if sdcheck !=1:
haClient.publish(sdPublish, openPayload)
sdcheck = 1
GPIO.output(bPin, GPIO.LOW) # Blue Led Off
if sdcheck !=2:
haClient.publish(sdPublish, closePayload)
sdcheck = 2

except KeyboardInterrupt: # If CTRL+C is pressed, exit cleanly:
GPIO.cleanup() # cleanup all GPIO

The Home Assistant Configure File. This adds the “Binary Sensors” as Entities for On or Off States. The device class makes it so it will show it as a door with Open / Closed states, not an on / off. Or be displayed as a progress bar or something.

- platform: mqtt
name: ‘Front Door’
unique_id : ‘detect.door.frnt’
state_topic: “pi/doors/front”
device_class: “door”
payload_on: “1”
payload_off: “0”

- platform: mqtt
name: ‘Back Door’
unique_id : ‘detect.door.back’
state_topic: “pi/doors/back”
device_class: “door”
payload_on: “1”
payload_off: “0”

- platform: mqtt
name: ‘Shed Door’
unique_id : ‘detect.door.shed’
state_topic: “pi/doors/shed”
device_class: “door”
payload_on: “1”
payload_off: “0”

For now, I made a simple automation that changes a scene. That will change the colour of one of the phillips hue bulbs if the door was open for too long.

(Note: even tho the type of sensor is set to door, for open / close, it still needs to read it as on or off. Which was a bit confusing when trying to get it to work)
- id: door_notification
alias: New Door Notification
- entity_id: binary_sensor.back_door
from: ‘off’
platform: state
to: ‘on’
for: ‘00:05:00’
condition: []
- scene: scene.door_warning

This code is just copied for each sensor. With minor differences IE: Front door wait time is only a minute. I do need to add a “return the bulb to the original state” But currently I just manually change the scene back to what I had it set to.

So for future options:

I would maybe like to change the settings to allow an email. As an example if the front door was detected as open for longer than 20 minutes, to email a family member that lives close so they can check.

Maybe have temperature sensor for the back door, so if its left open in the summer and its above 16c overnight, it won’t notify anyone, if its below 15c then it will. Lots of interesting possibilities.

For now, it is functioning fairly reliably. I did have a problem where the raspberry Pi installation seemed to get corrupted, and it wouldn’t boot requiring a reflash. So we’ll see if I have a problem again.

I would like to tidy it more, by adding a better case. And have everything soldered. But I need more parts to finish that, so for now I will use it as is.

The breadboard for the sensors: (2 Led’s are on because it thinks the back and shed door is open)

The Home Assistant Panel For Door Sensors:
Screenshot from 2020-12-30 14-03-15

1 Like