Add a door sensor to any door with a Sonoff basic

August 15, 2018

Today on the hookup we’re going to look at how to easily add a wired door sensor to a tasmotized sonoff basic.

Every once and a while it’s nice to do something simple, a project that can be completed in under an hour that provides great new functionality.  In my video about my most used smart home products I mentioned that I’ve gotten a bunch of use out of the door sensors on our interior doors, specifically the one on my daughter’s bedroom door.

When my daughter was a bit younger we had an incident where she got sick and and ran to the bathroom to throw up.  Normally my wife and I would have heard her from downstairs and gone to help, but we happened to be enjoying some nice weather out on our patio that night.  By the time we figured out something was wrong the bathroom was a disaster and my daughter was a bit distraught.

To prevent this from happening again I’ve installed a door sensor on her bedroom door that flashes the patio lights if her door is opened after her bedtime.  The best part about this project was how much functionality it adds, for such a small amount of time and money invested.

As you probably know, a sonoff basic is a five dollar internet connected relay that powers itself off your mains voltage.  It works natively with the eWeLink app, but I really don’t like the idea of using a cloud service to control mains power devices in my house, so I’ve never even installed that app.  Instead all of my sonoff devices run a custom firmware called Tasmota.  If you’ve never flashed a sonoff with tasmota I’m going to send you over to DrZzs in just a minute for the most recent tutorial on how to get it flashed.  If you get a new sonoff basic the holes that you need to access on the circuit board come filled with solder.  DrZzs has a fancy 3d printed tool that he uses in this video, but since we need to be able to use those holes for our project I’m going to suggest that you just install some header pins in there instead of using the 3d printed tool, to do this you’ll need a soldering iron.

Don’t turn the video off! Soldering is a great skill to have and it’s not nearly as hard as you think.  A cheap soldering kit like this one is all you need, and for $20, you get a ton of good stuff with it.

To easily add the header pins I’d recommend that you add them one at a time.  So take your header pins and cut them at the notches in plastic.  We do this so that we don’t have to heat up all 4 holes at the same time.  We’ll hold the pin with a pair of tweezers or pliers, but you want to make sure that you are holding the plastic part, not the metal part.  This will prevent the soldering iron from heating up your tool, which will also make the pin heat up faster.  Just line up the pin with the hole on the circuit board and apply heat to the top of the pin,  it will quickly heat up and transfer that heat onto the solder on the circuit board, letting you push the pin through easily.  Once you’ve got those 4 pins installed, you’ll be ready to flash tasmota, and like I said, if you’ve never done it before DrZzs has a great walk through of the current best way to get it done.  Click this link to go there now, but don’t forget to come back here when you’re done!

Once you’ve got tasmota flashed, take a look around your room and decide which device you’d like to be able to automate.  Keep in mind that the relay on a sonoff is only rated at 10A, so I wouldn’t recommend space heaters or portable air conditioning units, but pretty much anything else should be fine.  In my daughters room she has an air purifier that provides white noise when she is sleeping, but she doesn’t like to have it on when she is watching shows on her ipad in her room.  This way she can just ask her echo dot to turn off the “annoying fan”, and the sonoff will shut off power to it.  But that’s not really the star of this show.

This sonoff is also responsible for monitoring the state of my daughter’s door, and setting it up is really easy.  All you need is a magnetic reed switch, a little bit of wire, and a soldering iron.  Links to my favorite reed switches and the wire I use for my door sensors are in the description.

All we need to do is connect our reed switch to GPIO14 and ground on our sonoff.  You can either solder some wire directly to the headers we put in earlier, or you can use a female jumper wire to make it easier to connect and disconnect.   I recommend the latter because it makes putting your sonoff back together much easier once we’ve got everything connected, you can see that I drilled a medium sized hole in the top of my sonoff cover to be able to run the wires in through the top.  If you’re worried about someone messing with these wires you can always fill the hole with hot glue once you’ve got everything set up to make it a little more permanent.

You can run your wire for reed switch under the baseboard to the door.  I actually had clearance on mine to be able to put the magnet under the door so it is completely hidden.  Once you’ve got everything in place you just need to run some console commands in the tasmota interface and add some entries to your home assistant configuration.

The first thing we need to do is set up our switch.  After navigating to the IP address of your sonoff you’ll click on configuration, and then configure module.  We’re going to hit the dropdown box for GPIO14 and select Switch2, hit save, and then hit main menu.

Tasmota is going to think that you want switch2 to control the relay, so you’ll notice that opening and closing your door will shut off the device you are controlling, in my case the air purifier.  The next thing we need to do is decouple the switch from the relay.  We’ll do this using console commands, so click on console in the main menu and type “switchtopic” space, and then whatever you want to call this door and hit enter.  It’s probably a good idea to make sure you’re in switchmode1 so type is switchmode2 space 1 and press enter.  The last thing we want to do is retain last message that the switch sent in the event that homeassistant restarts.  To do this you’ll type in “SwitchRetain on” and press enter.

Next you’ll need to set up home assistant, or openhab, or whatever you’re using to monitor your doors to respond to the mqtt messages we just set up, I’m going to show the setup in home assistant.  You’ll want to add a binary sensor to your configuration.yaml file.  We’re going to define our platform as MQTT, name it whatever you want,  our state topic will be C M N D, front slash, then whatever you named your switchtopic, front slash, switch two.  Define the payload on as on and the payload off as off, and finally the device class as door.

You can also setup a switch in home assistant for whatever you’re controlling.  This is what that configuration looks like.  You’ll notice that the main difference is that the state topic and command topic are just power, instead of power2.

At this point you can save, check your config, and restart home assistant.  Now that the you’ve got the sensor up and running you just need to decide what you want it to do.  I do my automations in node red, but you should of course use whatever you’re most comfortable with.

Thank you so much to my new patreon supporters this week, your support and kind words really motivate me to keep working hard on these videos.  This time next week I’ll announce the winner of my first giveaway where I’m going to send one of my patrons all of the hardware required to make my DIY motorized shades.  Even if you don’t support me on patreon, I’d like to thank who has watched and commented on my content, I’ve really enjoyed the opportunity to help the community grow.  If you enjoy my videos, please consider subscribing, and as always, thanks for watching the hookup.

Sonoff Basic 4 pack $6.50/ea (amazon prime): https://amzn.to/2P9I5Ww
My recommended soldering kit with $5 off coupon: https://amzn.to/2B6G2zv
My favorite reed switches: https://amzn.to/2Mp3yw2
My security wire (this is a huge 500ft bundle, buy a smaller amount if you’re not into making a bunch of projects): https://amzn.to/2vLTCTD

Tasmota: https://github.com/arendst/Sonoff-Tasmota

Visit my website: http://www.thesmarthomehookup.com
Follow me on Twitter: @TheHookUp1

Support my channel:
Patreon: https://www.patreon.com/thehookup
Music by www.BenSound.com

Related Posts