{"id":1405,"date":"2017-12-29T18:30:00","date_gmt":"2017-12-29T18:30:00","guid":{"rendered":"https:\/\/zogspat.tk\/blog\/?p=1405"},"modified":"2017-12-30T12:45:16","modified_gmt":"2017-12-30T12:45:16","slug":"automating-philps-hue-sensor-functionality","status":"publish","type":"post","link":"https:\/\/the-plot.com\/blog\/?p=1405","title":{"rendered":"Automating Philips Hue Motion Sensor Functionality"},"content":{"rendered":"<p>OK, this turned out to be a *lot* more complicated than I thought. Here&#8217;s what I wanted to do: we have a Hue motion sensor in the bathroom which I&#8217;ve set up to do nothing during the day, and then come on at night. Nothing too controversial there. But I wanted to have a 3rd option: for a certain section of the night &#8211; say after midnight &#8211; to come on at a nightlight setting.<\/p>\n<p>There&#8217;s a &#8216;<a href=\"http:\/\/labs.meethue.com\/formulas\/huelabs\/three-times-is-a-charm\">3 times a charm formula<\/a>&#8216; from the Hue Labs which sounded promising, but that only allows you to set the idle time after motion activation. So I started looking at the API. There were a few obvious options I tried which didn&#8217;t work [like simply setting the brightness &#8211; over-written by whatever scene you have set in the accessory setup].<\/p>\n<p><a href=\"https:\/\/developers.meethue.com\/content\/better-rules-motion-sensor\">This article<\/a> set me on the right track for what I needed to do insofar as rule settings. By toggling changes on the smartphone app and then looking at the results in the Clip API debugger I narrowed down the changes to two rules called &#8216;MotionSensor 5.night-on&#8217; and &#8216;MotionSensor 5.night-dark-on&#8217;. [Obviously the number is dependent on the identifier the sensor is registered under.]<\/p>\n<p>This isn&#8217;t enough on its own. I gave up on this after my first pass, waiting for a rainy day to come back to it &#8211; today.<\/p>\n<p>I ended up using OWASP ZAP to proxy the Hue app traffic, to see what it was doing when you submit a change to the sensor configuration. That&#8217;s quite interesting actually: I was initially a bit puzzled at what looked like a mountain of traffic on first pass, but what in part turns out to be a very zealous keep-alive &#8211; roughly twice a second.<\/p>\n<p>Long and short of it is that resetting the scene config using the app overwrites a &#8216;resourcelink&#8217; with new scene specific values, as well as updating the rules.<\/p>\n<p>I&#8217;ve roughed this out with my own iOS app just to check that it works in a way that I can transplant to another setup [a cron job on a Raspberry Pi would be a reasonable candidate]. To toggle on the &#8216;dim at night after midnight&#8217; setting I make 3 API calls. You&#8217;ll need to fiddle around with the clip tool to get the equivalents for your hub &#8211; specifically, your sensor number will be different. For me, these are:<\/p>\n<ul>\n<li>Rule: MotionSensor 5.night-on<\/li>\n<li>Rule: MotionSensor 5.night-dark-on<\/li>\n<li>Resourcelink:\u00a0MotionSensor 5<\/li>\n<\/ul>\n<p>For the rules, I Post the same &#8216;actions&#8217; payload to both of the corresponding endpoints. It looks like:<\/p>\n<pre>{\"actions\": [\r\n                {\r\n                        \"address\": \"\/groups\/YourActionNumber\/action\",\r\n                        \"method\": \"PUT\",\r\n                        \"body\": {\r\n                                \"scene\": \"YourDimSceneIDHere\"\r\n                        }\r\n                },\r\n                {\r\n                        \"address\": \"\/sensors\/YourStateNumber\/state\",\r\n                        \"method\": \"PUT\",\r\n                        \"body\": {\r\n                                \"status\": 1\r\n                        }\r\n                }\r\n        ]\r\n}\r\n<\/pre>\n<p>Then for the resourcelink, I post:<\/p>\n<pre>{\r\n    \"name\": \"MotionSensor 5\",\r\n    \"description\": \"MotionSensor 5 behavior\",\r\n    \"type\": \"Link\",\r\n    \"classid\": 10020,\r\n    \"owner\": \"OwnerIDHere\",\r\n    \"recycle\": false,\r\n    \"links\": [\r\n              \"\/sensors\/5\",\r\n              \"\/sensors\/6\",\r\n              \"\/sensors\/19\",\r\n              \"\/groups\/5\",\r\n              \"\/rules\/4\",\r\n              \"\/rules\/5\",\r\n              \"\/rules\/6\",\r\n              [etc.....]\r\n              \"\/scenes\/YourDimSceneIDHere\",\r\n              \"\/scenes\/RecoverRoomSceneIDHere\"\r\n              ]\r\n}\r\n<\/pre>\n<p>The sensor and rule set you&#8217;re linking to will be specific to your own setup.<\/p>\n<p>I&#8217;ve no idea what that &#8216;recover scene&#8217; is. I&#8217;m guessing it&#8217;s something that&#8217;s created when you configure a sensor in the first instance, and is some sort of default state.<\/p>\n<p>There&#8217;s a corresponding 3 API calls which I need to make to toggle back to the scene for &#8216;bright pre-midnight&#8217;, replacing the scene ID accordingly.<\/p>\n<p>At the point I gave up &#8211; where I was just making the rule changes &#8211; I could tell that it wasn&#8217;t right by loading up the config in the app. The partial configuration &#8211; without the resourcelink change &#8211; manifest itself simply as &#8216;do nothing&#8217; for the nighttime slot. Resetting this restored the full config. I guess there&#8217;s always the danger that if you run wild and free with setting values incorrectly, you might invalidate the ruleset to the point where unpicking it could be a problem. All I can say is that the above approach works for me, but test carefully!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>OK, this turned out to be a *lot* more complicated than I thought. Here&#8217;s what I wanted to do: we have a Hue motion sensor in the bathroom which I&#8217;ve set up to do nothing during the day, and then &hellip; <a href=\"https:\/\/the-plot.com\/blog\/?p=1405\">Continue reading <span class=\"meta-nav\">&rarr;<\/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":[5],"tags":[],"class_list":["post-1405","post","type-post","status-publish","format-standard","hentry","category-tech"],"_links":{"self":[{"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1405","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1405"}],"version-history":[{"count":8,"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1405\/revisions"}],"predecessor-version":[{"id":1414,"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1405\/revisions\/1414"}],"wp:attachment":[{"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}