{"id":421,"date":"2018-08-15T22:16:37","date_gmt":"2018-08-16T02:16:37","guid":{"rendered":"http:\/\/www.thesmarthomehookup.com\/test_install\/?p=421"},"modified":"2023-02-08T17:06:15","modified_gmt":"2023-02-08T22:06:15","slug":"add-a-door-sensor-to-any-door-with-a-sonoff-basic","status":"publish","type":"post","link":"http:\/\/www.thesmarthomehookup.com\/test_install\/add-a-door-sensor-to-any-door-with-a-sonoff-basic\/","title":{"rendered":"Add a door sensor to any door with a Sonoff basic"},"content":{"rendered":"<p><iframe width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/qZqmkhpJ55A\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen=\"\"><\/iframe><\/p>\n<p>Today on the hookup we\u2019re going to look at how to easily add a wired door sensor to a tasmotized sonoff basic.<\/p>\n<p>Every once and a while it\u2019s nice to do something simple, a project that can be completed in under an hour that provides great new functionality.&nbsp; In my video about my most used smart home products I mentioned that I\u2019ve gotten a bunch of use out of the door sensors on our interior doors, specifically the one on my daughter\u2019s bedroom door.<\/p>\n<p>When my daughter was a bit younger we had an incident where she got sick and and ran to the bathroom to throw up.&nbsp; 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.&nbsp; By the time we figured out something was wrong the bathroom was a disaster and my daughter was a bit distraught.<\/p>\n<p>To prevent this from happening again I\u2019ve installed a door sensor on her bedroom door that flashes the patio lights if her door is opened after her bedtime.&nbsp; The best part about this project was how much functionality it adds, for such a small amount of time and money invested.<\/p>\n<p>As you probably know, a sonoff basic is a five dollar internet connected relay that powers itself off your mains voltage.&nbsp; It works natively with the eWeLink app, but I really don\u2019t like the idea of using a cloud service to control mains power devices in my house, so I\u2019ve never even installed that app.&nbsp; Instead all of my sonoff devices run a custom firmware called Tasmota.&nbsp; If you\u2019ve never flashed a sonoff with tasmota I\u2019m going to send you over to DrZzs in just a minute for the most recent tutorial on how to get it flashed.&nbsp; If you get a new sonoff basic the holes that you need to access on the circuit board come filled with solder.&nbsp; 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\u2019m going to suggest that you just install some header pins in there instead of using the 3d printed tool, to do this you\u2019ll need a soldering iron.<\/p>\n<p>Don\u2019t turn the video off! Soldering is a great skill to have and it\u2019s not nearly as hard as you think.&nbsp; A cheap soldering kit like this one is all you need, and for $20, you get a ton of good stuff with it.<\/p>\n<p>To easily add the header pins I\u2019d recommend that you add them one at a time.&nbsp; So take your header pins and cut them at the notches in plastic.&nbsp; We do this so that we don\u2019t have to heat up all 4 holes at the same time.&nbsp; We\u2019ll 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.&nbsp; This will prevent the soldering iron from heating up your tool, which will also make the pin heat up faster.&nbsp; Just line up the pin with the hole on the circuit board and apply heat to the top of the pin,&nbsp; it will quickly heat up and transfer that heat onto the solder on the circuit board, letting you push the pin through easily.&nbsp; Once you\u2019ve got those 4 pins installed, you\u2019ll be ready to flash tasmota, and like I said, if you\u2019ve never done it before DrZzs has a great walk through of the current best way to get it done.&nbsp; Click this link to go there now, but don\u2019t forget to come back here when you\u2019re done!<\/p>\n<p>Once you\u2019ve got tasmota flashed, take a look around your room and decide which device you\u2019d like to be able to automate.&nbsp; Keep in mind that the relay on a sonoff is only rated at 10A, so I wouldn\u2019t recommend space heaters or portable air conditioning units, but pretty much anything else should be fine.&nbsp; In my daughters room she has an air purifier that provides white noise when she is sleeping, but she doesn\u2019t like to have it on when she is watching shows on her ipad in her room.&nbsp; This way she can just ask her echo dot to turn off the \u201cannoying fan\u201d, and the sonoff will shut off power to it.&nbsp; But that\u2019s not really the star of this show.<\/p>\n<p>This sonoff is also responsible for monitoring the state of my daughter\u2019s door, and setting it up is really easy.&nbsp; All you need is a magnetic reed switch, a little bit of wire, and a soldering iron.&nbsp; Links to my favorite reed switches and the wire I use for my door sensors are in the description.<\/p>\n<p>All we need to do is connect our reed switch to GPIO14 and ground on our sonoff.&nbsp; 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.&nbsp;&nbsp; I recommend the latter because it makes putting your sonoff back together much easier once we\u2019ve 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.&nbsp; If you\u2019re worried about someone messing with these wires you can always fill the hole with hot glue once you\u2019ve got everything set up to make it a little more permanent.<\/p>\n<p>You can run your wire for reed switch under the baseboard to the door.&nbsp; I actually had clearance on mine to be able to put the magnet under the door so it is completely hidden.&nbsp; Once you\u2019ve 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.<\/p>\n<p>The first thing we need to do is set up our switch.&nbsp; After navigating to the IP address of your sonoff you\u2019ll click on configuration, and then configure module.&nbsp; We\u2019re going to hit the dropdown box for GPIO14 and select Switch2, hit save, and then hit main menu.<\/p>\n<p>Tasmota is going to think that you want switch2 to control the relay, so you\u2019ll notice that opening and closing your door will shut off the device you are controlling, in my case the air purifier.&nbsp; The next thing we need to do is decouple the switch from the relay.&nbsp; We\u2019ll do this using console commands, so click on console in the main menu and type \u201cswitchtopic\u201d space, and then whatever you want to call this door and hit enter.&nbsp; It\u2019s probably a good idea to make sure you\u2019re in switchmode1 so type is switchmode2 space 1 and press enter.&nbsp; The last thing we want to do is retain last message that the switch sent in the event that homeassistant restarts.&nbsp; To do this you\u2019ll type in \u201cSwitchRetain on\u201d and press enter.<\/p>\n<p>Next you\u2019ll need to set up home assistant, or openhab, or whatever you\u2019re using to monitor your doors to respond to the mqtt messages we just set up, I\u2019m going to show the setup in home assistant.&nbsp; You\u2019ll want to add a binary sensor to your configuration.yaml file.&nbsp; We\u2019re going to define our platform as MQTT, name it whatever you want,&nbsp; our state topic will be C M N D, front slash, then whatever you named your switchtopic, front slash, switch two.&nbsp; Define the payload on as on and the payload off as off, and finally the device class as door.<\/p>\n<p>You can also setup a switch in home assistant for whatever you\u2019re controlling.&nbsp; This is what that configuration looks like.&nbsp; You\u2019ll notice that the main difference is that the state topic and command topic are just power, instead of power2.<\/p>\n<p>At this point you can save, check your config, and restart home assistant.&nbsp; Now that the you\u2019ve got the sensor up and running you just need to decide what you want it to do.&nbsp; I do my automations in node red, but you should of course use whatever you\u2019re most comfortable with.<\/p>\n<p>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.&nbsp; This time next week I\u2019ll announce the winner of my first giveaway where I\u2019m going to send one of my patrons all of the hardware required to make my DIY motorized shades.&nbsp; Even if you don\u2019t support me on patreon, I\u2019d like to thank who has watched and commented on my content, I\u2019ve really enjoyed the opportunity to help the community grow.&nbsp; If you enjoy my videos, please consider subscribing, and as always, thanks for watching the hookup.<\/p>\n<p>Sonoff Basic 4 pack $6.50\/ea (amazon prime): <a href=\"https:\/\/amzn.to\/2P9I5Ww\">https:\/\/amzn.to\/2P9I5Ww<\/a><br \/>\nMy recommended soldering kit with $5 off coupon: <a href=\"https:\/\/amzn.to\/2B6G2zv\">https:\/\/amzn.to\/2B6G2zv<\/a><br \/>\nMy favorite reed switches: <a href=\"https:\/\/amzn.to\/2Mp3yw2\">https:\/\/amzn.to\/2Mp3yw2<\/a><br \/>\nMy security wire (this is a huge 500ft bundle, buy a smaller amount if you&#8217;re not into making a bunch of projects): <a href=\"https:\/\/amzn.to\/2vLTCTD\">https:\/\/amzn.to\/2vLTCTD<\/a><\/p>\n<p>Tasmota: <a href=\"https:\/\/github.com\/arendst\/Sonoff-Tasmota\">https:\/\/github.com\/arendst\/Sonoff-Tasmota<\/a><\/p>\n<p>Visit my website: http:\/\/www.thesmarthomehookup.com\/test_install<br \/>\nFollow me on Twitter: @TheHookUp1<\/p>\n<p>Support my channel:<br \/>\nPatreon: <a href=\"https:\/\/www.patreon.com\/thehookup\">https:\/\/www.patreon.com\/thehookup<\/a><br \/>\nMusic by www.BenSound.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today on the hookup we\u2019re going to look at how to easily add a wired door sensor to a tasmotized sonoff basic. Every once and a while it\u2019s nice to do something simple, a project that can be completed in under an hour that provides great new functionality.&nbsp; In my video about my most used [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2599,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-421","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-projects"],"acf":[],"mb":[],"mfb_rest_fields":["title","gutenberg_elementor_mode"],"_links":{"self":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts\/421","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/comments?post=421"}],"version-history":[{"count":4,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts\/421\/revisions"}],"predecessor-version":[{"id":2354,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts\/421\/revisions\/2354"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/media\/2599"}],"wp:attachment":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/media?parent=421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/categories?post=421"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/tags?post=421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}