{"id":951,"date":"2013-10-29T12:24:52","date_gmt":"2013-10-29T12:24:52","guid":{"rendered":"http:\/\/dollarunderscore.azurewebsites.net\/?p=951"},"modified":"2014-12-04T15:55:50","modified_gmt":"2014-12-04T14:55:50","slug":"wake-me-up-when-traffic-calms-down-home-automation","status":"publish","type":"post","link":"https:\/\/p0wershell.com\/?p=951","title":{"rendered":"Wake me up, when traffic calms down&#8230; (Home Automation)"},"content":{"rendered":"<p>Why do home automation with PowerShell?<\/p>\n<p>There are certainly other solutions out there which are great, even excellent. For me personally, it&#8217;s mainly because it&#8217;s fun to be able to build parts of it by yourself, you learn a lot by doing it, but you can also base your tasks on almost any piece of information out there.<\/p>\n<p>I&#8217;ll give you an example!<\/p>\n<p>I&#8217;ve started to go to work after the traffic calms down in the morning, I have a pretty good idea of when this usually happens, but sometimes there is no traffic jams at all, and sometimes it&#8217;s completely hopeless.<\/p>\n<p>Wouldn&#8217;t it be nice to be able to utilize live traffic information on the internet, and based on that trigger your wake up call? At least I thought so \ud83d\ude42<\/p>\n<p>First of all, try to find a provider for traffic information near you, and make sure you don&#8217;t break their ToS by fetching that information in a automatic way (ie. web scraping).<\/p>\n<p>I won&#8217;t go into detail on how to build a webscrape-cmdlet right now, but I&#8217;ll show you how to use it when it&#8217;s done. (A guide to web scraping available <a href=\"https:\/\/p0wershell.com\/?p=3831\" title=\"Ordering pizza with PowerShell (web scraping guide) \u2013 Part 1\">here<\/a>, <a href=\"https:\/\/p0wershell.com\/?p=3991\" title=\"Ordering pizza with PowerShell (web scraping guide) \u2013 Part 2\">here<\/a> and <a href=\"https:\/\/p0wershell.com\/?p=2651\" title=\"Web scraping with PowerShell (Getting a package trace from a postal service)\">here<\/a>.)<\/p>\n<p>This is the script I run every morning, I think the code comments will be enough to explain how it works:<\/p>\n<p>[PowerShell]<br \/>\n# Import the Telldus module<br \/>\nImport-Module &#8216;.\\Telldus.psm1&#8217;<\/p>\n<p># Import the Module containing your traffic parser<br \/>\nImport-Module &#8216;.\\WebDataModule.psm1&#8217;<\/p>\n<p># Set your home and work address<br \/>\n$HomeAddress=&#8221;Homeroad 1, MyTown&#8221;<br \/>\n$WorkAddress=&#8221;Workaround 1, WorkTown&#8221;<\/p>\n<p># Set a max traveltime limit (in this case, in minutes)<br \/>\n[int] $TravelTimeLimit = 30<\/p>\n<p># I want it to be under this value for $NumberOfTimes consecutive times<br \/>\n[int] $NumberOfTimes = 3<\/p>\n<p># Make sure it does&#8217;nt get $True on first run<br \/>\n[int] $CurrentTravelTime = $TravelTimeLimit+1<\/p>\n<p># Reset variable to zero<br \/>\n[int] $NumberOfTimesVerifiedOK = 0<\/p>\n<p># Run until the traveltime limit has been passed enough times<br \/>\nwhile ($NumberOfTimesVerifiedOK -lt $NumberOfTimes) {<\/p>\n<p>    # Reset variable<br \/>\n    $CurrentTravelTime = $null<\/p>\n<p>    # Load new data, the &#8220;Get-Traffic&#8221;-cmdlet is my traffic parser<br \/>\n    [int] $CurrentTravelTime = Get-Traffic -FromAddress $HomeAddress -ToAddress $WorkAddress | select -ExpandProperty TravelTime<\/p>\n<p>    # Check if it is below your traveltime limit, and that it is not $null (cmdlet failed)<br \/>\n    # Increase $NumberOfTimesVerifiedOK if it was ok, or reset to zero if it wasn&#8217;t<br \/>\n    if ($CurrentTravelTime -ne $null -AND $CurrentTravelTime -lt $TravelTimeLimit) {<br \/>\n        $NumberOfTimesVerifiedOK++<br \/>\n    }<br \/>\n    else {<br \/>\n        $NumberOfTimesVerifiedOK = 0<br \/>\n    }<\/p>\n<p>    # Write current status<br \/>\n    Write-Output &#8220;Traffic has been verified as OK $NumberOfTimesVerifiedOK consecutive times&#8221;<\/p>\n<p>    # Pause for a while before checking again, 10 minutes or so&#8230;<br \/>\n    Start-Sleep -Seconds 600<br \/>\n}<\/p>\n<p># The while loop will exit when traveltime has been verified enough times.<\/p>\n<p># Write status<br \/>\nWrite-Output &#8220;Initiating sunrise, current travel time to $WorkAddress is $CurrentTravelTime minutes, and has been below $TravelTimeLimit for $NumberOfTimes consecutive times.&#8221;<\/p>\n<p># Time to initiate the &#8220;sunrise effect&#8221;<\/p>\n<p># Set the device id for the lamp you want to light up<br \/>\n$BedroomLampDeviceID=&#8221;123456&#8243;<\/p>\n<p># Set start dimlevel<br \/>\n$SunriseDimlevel = 1<\/p>\n<p># Set how much it should increase everytime we &#8220;raise&#8221; it<br \/>\n$SunriseSteps = 5<\/p>\n<p># Set your Telldus credentials<br \/>\n$Username=&#8221;my_email@telldus.com&#8221;<br \/>\n$Password=&#8221;MySecretPassword&#8221;<\/p>\n<p># Kick off the &#8220;sunrise-loop&#8221;<br \/>\nwhile ($SunriseDimlevel -lt 255) {<br \/>\n    # Write some status<br \/>\n    Write-Output &#8220;Setting dimlevel to $SunriseDimlevel&#8221;<\/p>\n<p>    # Set the new dimlevel<br \/>\n    Set-TDDimmer -Username $Username -Password $Password -DeviceID $BedroomLampDeviceID -Level $SunriseDimlevel<\/p>\n<p>    # Sleep for a while (30 seconds makes the &#8220;sunrise&#8221; ~30 minutes long depending on your $SunriseSteps value)<br \/>\n    Start-Sleep -Seconds 30<\/p>\n<p>    # Set the next dimlevel<br \/>\n    $SunriseDimlevel=$SunriseDimlevel+$SunriseSteps<br \/>\n}<\/p>\n<p># Set the lamp to full power (loop has exited) and exit<br \/>\nSet-TDDimmer -Username $Username -Password $Password -DeviceID $BedroomLampDeviceID -Level 255<br \/>\nWrite-Output &#8220;Maximum level is set.&#8221;<\/p>\n<p>[\/PowerShell]<\/p>\n<p>This script is scheduled to run in the morning (on week days) around the earliest time I want to go up, the first loop will run until traffic calms down, and then start the &#8220;sunrise&#8221;-loop which will run until the light reaches its maximum level (255).<\/p>\n<p>You could of course turn on other stuff as well, like a coffee brewer (make sure you don&#8217;t do this while you are away&#8230;), a radio, play some music or something else.<\/p>\n<p>That is one of the (many!) pro&#8217;s of doing things with PowerShell! \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Why do home automation with PowerShell? There are certainly other solutions out there which are great, even excellent. For me personally, it&#8217;s mainly because it&#8217;s fun to be able to build parts of it by yourself, you learn a lot by doing it, but you can also base your tasks on almost any piece of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[231,21,241,841],"tags":[521,501,511],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3Zj0A-fl","_links":{"self":[{"href":"https:\/\/p0wershell.com\/index.php?rest_route=\/wp\/v2\/posts\/951"}],"collection":[{"href":"https:\/\/p0wershell.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/p0wershell.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/p0wershell.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/p0wershell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=951"}],"version-history":[{"count":0,"href":"https:\/\/p0wershell.com\/index.php?rest_route=\/wp\/v2\/posts\/951\/revisions"}],"wp:attachment":[{"href":"https:\/\/p0wershell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/p0wershell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=951"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/p0wershell.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}