{"id":131,"date":"2016-08-29T08:44:53","date_gmt":"2016-08-29T14:44:53","guid":{"rendered":"http:\/\/draith.com\/?p=131"},"modified":"2016-08-29T08:44:53","modified_gmt":"2016-08-29T14:44:53","slug":"why-not-series-powershell-ifttt-and-smartthings","status":"publish","type":"post","link":"https:\/\/draith.com\/?p=131","title":{"rendered":"&#8216;Why Not?&#8217; Series &#8211; PowerShell, IFTTT, and Smartthings"},"content":{"rendered":"<p>Ever wanted to turn your kitchen lights off from a command line?<\/p>\n<p><em><strong>OF COURSE YOU HAVE. <\/strong><\/em><\/p>\n<p>In another addition of my &#8216;Why Not?&#8217; series, I explore how to bring the power of IFTTT and SmartThings to PowerShell.<\/p>\n<p>SmartThings is a home automation suite that consists of a central hub, z-wave or Zigbee switches, outlets, light bulbs, smoke detectors, water sensors, etc&#8230; Thousands of devices exist that integrate with the SmartThings system, and it has an accessible API. I swear, this is not a paid advertisement. It just happens to be the system I use in my home automation. One night I was sitting at home wondering why I had to use an app or even my Harmony remote to turn off the lights in my man-cave. I was working on a PowerShell script, and that&#8217;s when it hit me &#8211; &#8216;Why Not?&#8217; &#8211; Why can&#8217;t I use PowerShell to turn those off these lights?<\/p>\n<p>Let&#8217;s assume you have SmartThings setup in your home already, and that you have signed into the <a href=\"http:\/\/SmartThings API\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/graph.api.smartthings.com\/<\/a> portal at least once. If that is done &#8211; we simply need to head over to <a href=\"https:\/\/ifttt.com\" target=\"_blank\" rel=\"noopener noreferrer\">IFTTT<\/a>. If you need a primer on IFTTT, head here: <a href=\"https:\/\/ifttt.com\/wtf\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/ifttt.com\/wtf<\/a>. Yeah, I am NOT going to change that link &#8211; it&#8217;s awesome. If you already have an IFTTT account, <a href=\"https:\/\/ifttt.com\/login\" target=\"_blank\" rel=\"noopener noreferrer\">sign in<\/a>. If not, <a href=\"https:\/\/ifttt.com\/join\" target=\"_blank\" rel=\"noopener noreferrer\">sign up<\/a>. Once signed in, we will want to add a new channel. The channel we want to add, ironically enough, is called SmartThings. Click on the &#8220;Channels&#8221; link, search for SmartThings, and click on the Icon (should be the only one returned if you search correctly). Click the Icon.<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-136\" src=\"http:\/\/draith.com\/wp-content\/uploads\/2016\/08\/IftttFindSmartThings.jpg\" alt=\"IftttFindSmartThings\" width=\"506\" height=\"458\" srcset=\"\/wp-content\/uploads\/2016\/08\/IftttFindSmartThings.jpg 506w, \/wp-content\/uploads\/2016\/08\/IftttFindSmartThings-300x272.jpg 300w\" sizes=\"auto, (max-width: 506px) 85vw, 506px\" \/><\/p>\n<p>Now click on the GIANT connect button on the right hand side. It will take you to the SmartThings Api login page. Sign in, and you are greeted with this:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-137\" src=\"http:\/\/draith.com\/wp-content\/uploads\/2016\/08\/iftttpicklocation.jpg\" alt=\"iftttpicklocation\" width=\"397\" height=\"471\" srcset=\"\/wp-content\/uploads\/2016\/08\/iftttpicklocation.jpg 397w, \/wp-content\/uploads\/2016\/08\/iftttpicklocation-253x300.jpg 253w\" sizes=\"auto, (max-width: 397px) 85vw, 397px\" \/><br \/>\nPick the Hub\/Location that you want to integrate with IFTTT. When you do, you are shown a list of devices connected to your SmartThings hub:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-138\" src=\"http:\/\/draith.com\/wp-content\/uploads\/2016\/08\/iftttpickdevices.jpg\" alt=\"iftttpickdevices\" width=\"361\" height=\"940\" srcset=\"\/wp-content\/uploads\/2016\/08\/iftttpickdevices.jpg 361w, \/wp-content\/uploads\/2016\/08\/iftttpickdevices-115x300.jpg 115w\" sizes=\"auto, (max-width: 361px) 85vw, 361px\" \/><br \/>\nSelect the devices you want to control, and press the &#8220;Authorize&#8221; button. You will be taken back to IFTTT. Don&#8217;t try to add any recipes yet &#8211; we need one more channel to make this work. In order to get IFTTT to trigger, we can use either the DO channel\/app (which I am going to ignore in this demo), or we can use the Maker channel. Search for, and add the Maker channel just like we did for the SmartThings channel.<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-139\" src=\"http:\/\/draith.com\/wp-content\/uploads\/2016\/08\/iftttmaker.jpg\" alt=\"iftttmaker\" width=\"1057\" height=\"432\" srcset=\"\/wp-content\/uploads\/2016\/08\/iftttmaker.jpg 1057w, \/wp-content\/uploads\/2016\/08\/iftttmaker-300x123.jpg 300w, \/wp-content\/uploads\/2016\/08\/iftttmaker-1024x419.jpg 1024w, \/wp-content\/uploads\/2016\/08\/iftttmaker-768x314.jpg 768w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><br \/>\nWhen you add the Maker Channel, a key is automatically generated for you. Keep this key handy &#8211; we will be using it soon. I am not showing you my key, cause I barely know you guys &#8211; and it is unique to my recipes.<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-142\" src=\"http:\/\/draith.com\/wp-content\/uploads\/2016\/08\/iftttmakerkey.jpg\" alt=\"iftttmakerkey\" width=\"701\" height=\"407\" srcset=\"\/wp-content\/uploads\/2016\/08\/iftttmakerkey.jpg 701w, \/wp-content\/uploads\/2016\/08\/iftttmakerkey-300x174.jpg 300w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Great &#8211; now what? Let&#8217;s add a recipe!! Click the &#8220;Create a New Recipe button&#8221;, and you are shown the typical IFTTT recipe builder page that looks like:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-143\" src=\"http:\/\/draith.com\/wp-content\/uploads\/2016\/08\/iftttthis.jpg\" alt=\"iftttthis\" width=\"955\" height=\"227\" srcset=\"\/wp-content\/uploads\/2016\/08\/iftttthis.jpg 955w, \/wp-content\/uploads\/2016\/08\/iftttthis-300x71.jpg 300w, \/wp-content\/uploads\/2016\/08\/iftttthis-768x183.jpg 768w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><br \/>\nClick on the &#8220;this&#8221; portion, and search for\/select the Maker channel:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-145\" src=\"http:\/\/draith.com\/wp-content\/uploads\/2016\/08\/iftttmakerthis.jpg\" alt=\"iftttmakerthis\" width=\"572\" height=\"415\" srcset=\"\/wp-content\/uploads\/2016\/08\/iftttmakerthis.jpg 572w, \/wp-content\/uploads\/2016\/08\/iftttmakerthis-300x218.jpg 300w\" sizes=\"auto, (max-width: 572px) 85vw, 572px\" \/><br \/>\nPick the &#8220;Receive Web Request&#8221; tile. We now need to name our trigger. These will be unique to each device, and unique to the function we are calling. For example, if I want to turn on and off my Man-Cave Lights, I need to specify two unique triggers. Leave out spaces in this name.<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-146\" src=\"http:\/\/draith.com\/wp-content\/uploads\/2016\/08\/iftttmakertrigger-mc.jpg\" alt=\"iftttmakertrigger-mc\" width=\"785\" height=\"401\" srcset=\"\/wp-content\/uploads\/2016\/08\/iftttmakertrigger-mc.jpg 785w, \/wp-content\/uploads\/2016\/08\/iftttmakertrigger-mc-300x153.jpg 300w, \/wp-content\/uploads\/2016\/08\/iftttmakertrigger-mc-768x392.jpg 768w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><br \/>\nCreate the trigger. BOOM &#8211; we are back the equation:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-147\" src=\"http:\/\/draith.com\/wp-content\/uploads\/2016\/08\/iftttthat.jpg\" alt=\"iftttthat\" width=\"1021\" height=\"297\" srcset=\"\/wp-content\/uploads\/2016\/08\/iftttthat.jpg 1021w, \/wp-content\/uploads\/2016\/08\/iftttthat-300x87.jpg 300w, \/wp-content\/uploads\/2016\/08\/iftttthat-768x223.jpg 768w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><br \/>\nClick the &#8216;THAT&#8217; section. We are dropped back to the channel search page &#8211; this time it&#8217;s the search for the Action channel &#8211; type in and select &#8216;SmartThings&#8217;. You should see a list of all the fun things that SmartThings brings to IFTTT.<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-148\" src=\"http:\/\/draith.com\/wp-content\/uploads\/2016\/08\/iftttactionSmartthings.jpg\" alt=\"iftttactionSmartthings\" width=\"1088\" height=\"576\" srcset=\"\/wp-content\/uploads\/2016\/08\/iftttactionSmartthings.jpg 1088w, \/wp-content\/uploads\/2016\/08\/iftttactionSmartthings-300x159.jpg 300w, \/wp-content\/uploads\/2016\/08\/iftttactionSmartthings-1024x542.jpg 1024w, \/wp-content\/uploads\/2016\/08\/iftttactionSmartthings-768x407.jpg 768w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><br \/>\nChoose the &#8216;Switch On&#8217; tile. You can now pick the switch you want to interact with. In my example I will choose &#8216;Man Cave Lights&#8217;<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-149\" src=\"http:\/\/draith.com\/wp-content\/uploads\/2016\/08\/iftttchoosedevice.jpg\" alt=\"iftttchoosedevice\" width=\"861\" height=\"505\" srcset=\"\/wp-content\/uploads\/2016\/08\/iftttchoosedevice.jpg 861w, \/wp-content\/uploads\/2016\/08\/iftttchoosedevice-300x176.jpg 300w, \/wp-content\/uploads\/2016\/08\/iftttchoosedevice-768x450.jpg 768w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><br \/>\nOne final step &#8211; click &#8220;Create Recipe&#8221;. Done! Now, create another new recipe, following the same steps, except name the Maker trigger something like &#8216;Man_Cave_Lights_Off&#8217;, and make sure you select the &#8216;Switch Off&#8217; tile in the action section. You should now have 2 recipes &#8211; one for turning the lights on, another for turning the lights off. If you check the My Recipes section, you should have something along the lines of these two:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-150\" src=\"http:\/\/draith.com\/wp-content\/uploads\/2016\/08\/iftttrecipes.jpg\" alt=\"iftttrecipes\" width=\"1039\" height=\"632\" srcset=\"\/wp-content\/uploads\/2016\/08\/iftttrecipes.jpg 1039w, \/wp-content\/uploads\/2016\/08\/iftttrecipes-300x182.jpg 300w, \/wp-content\/uploads\/2016\/08\/iftttrecipes-1024x623.jpg 1024w, \/wp-content\/uploads\/2016\/08\/iftttrecipes-768x467.jpg 768w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p>We are done in IFTTT for the moment &#8211; let&#8217;s head over to PowerShell.<\/p>\n<p>This is pretty straight-forward, actually. We need to craft a URL with this format: https:\/\/maker.ifttt.com\/trigger\/{event}\/with\/key\/{key}. The {event} is the Maker event we specified when we created the recipe &#8211; they {key} is the unique key that was generated when we added the Maker channel. All we need to do is craft the URL and send the web-request.<\/p>\n<pre class=\"lang:ps decode:true \">$MakerKey = 'abcdefghijklmnopqrstuvwxyz'\n$BaseURL = 'https:\/\/maker.ifttt.com\/trigger\/'\n$EndURL = \"\/with\/key\/$MakerKey\"\n$event = 'Man_Cave_Lights_On'\n$url = $BaseURL+$event+$endurl\nInvoke-WebRequest -uri $url -UseBasicParsing|Select-Object -Property content\n<\/pre>\n<p>Again, we use the -UseBasicParsing parameter to keep from firing up IE initial config. If we have done everything right, we should be greeted with something along the lines of:<br \/>\nContent<br \/>\n&#8212;&#8212;-<br \/>\nCongratulations! You&#8217;ve fired the Man_Cave_Lights_On event<\/p>\n<p>Want proof? Here you go!<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-155\" src=\"http:\/\/draith.com\/wp-content\/uploads\/2016\/08\/20160827_234150.gif\" alt=\"20160827_234150\" width=\"640\" height=\"360\" \/><\/p>\n<p>Once you add the Maker Channel, it opens a world of possibility up when it comes to IFTTT.  For example, I later added my Harmony remote as a channel and was able to turn my AV system on and off and perform a large number of automations from PowerShell.  Oh, the trouble we will get into&#8230;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ever wanted to turn your kitchen lights off from a command line? OF COURSE YOU HAVE. In another addition of my &#8216;Why Not?&#8217; series, I explore how to bring the power of IFTTT and SmartThings to PowerShell. SmartThings is a home automation suite that consists of a central hub, z-wave or Zigbee switches, outlets, light &hellip; <a href=\"https:\/\/draith.com\/?p=131\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;&#8216;Why Not?&#8217; Series &#8211; PowerShell, IFTTT, and Smartthings&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[10,16,24],"class_list":["post-131","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-ifttt","tag-powershell","tag-why-not"],"_links":{"self":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=131"}],"version-history":[{"count":0,"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/131\/revisions"}],"wp:attachment":[{"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}