{"id":317,"date":"2016-06-21T12:57:25","date_gmt":"2016-06-21T17:57:25","guid":{"rendered":"http:\/\/lisa.rushworth.us\/?p=317"},"modified":"2016-06-21T12:57:25","modified_gmt":"2016-06-21T17:57:25","slug":"using-bc-and-command-substitution-in-openhabs-exec-binding","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=317","title":{"rendered":"Using BC And Command Substitution In OpenHAB&#8217;s Exec Binding"},"content":{"rendered":"<p>My husband has been\u00a0setting up OpenHAB to control our home automation. Our dimmers\u00a0are very direct &#8211; there&#8217;s a z-Wave binding that you set to 100 if you want it at 100%, set it to 18 if you want it at 18%, and so on. We have a handful of Zigbee bulbs, though, which are not so direct. We are controlling these bulbs through a Wink hub by running a curl command with the exec binding.<\/p>\n<p>The OpenHAB exec binding runs a shell with a command string passed in from the -c parameter. Thus far, I have not found anything that runs within a shell not work in the exec binding. This includes <a href=\"http:\/\/pubs.opengroup.org\/onlinepubs\/9699919799\/utilities\/V3_chap02.html#tag_18_06_03\" target=\"_blank\">command substitution<\/a> {I personally use the backtick format instead of the $(command) format, but I expect the later to be equally functional}.<\/p>\n<p>What is command substitution (without having to read the Open Group Base Specifications linked above)? If you run<\/p>\n<pre>kill `pidof java`<\/pre>\n<p>the shell takes the component within the backticks, evaluates it, and then takes the standard output and places that into the command. When &#8220;pidof java&#8221; returns &#8220;938 984 1038&#8221;, the command above becomes &#8220;kill 938 984 1038&#8221;.<\/p>\n<p>We want to set the value to the OpenHab value (0-100) scaled to the Wink value (0-255 for GE Link bulbs) using command substitution with bc (an arbitrary precision calculator language). To evaluate a mathematical expression, echo the expression text and pipe it to bc. To set a bulb to 75% of its maximum brightness, our post data is &#8220;nodeId=a&amp;attrId=aprontest -u -m9 -t2 -v`echo 2.55*75\/1|bc`&#8221;.<\/p>\n<p>Notice the divide by 1 at the end &#8212; that&#8217;s to turn a decimal value into an integer. If you use just 2.55*75, you post a value of 191.25 which throws an error. In bc&#8217;s language, \/ returns the quotient &#8212; this isn&#8217;t *rounding* but rather truncating the decimal portion( i.e. bc 9.99999\/1 = 9).<\/p>\n<p>We configure the OpenHAB item to take the selected value (the %2$s below), scale the value with bc, and insert the result into the curl command. We use a similar technique to read the data from Wink and present the scaled value through OpenHAB.<\/p>\n<p>The item entry in our <em>sitemap<\/em>.items file:<\/p>\n<p>Dimmer\u00a0 DS_Pantry_Bulb_Level\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8220;Bulb (Pantry Downstairs) [%d]&#8221;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;slider&gt;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (gZigbeeBulb,gDS_Pantry,gLight)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 { exec=&#8221;&lt;[\/bin\/sh@@-c@@echo `\/usr\/bin\/curl \\&#8221;http:\/\/wink.hub.address\/set_dev_value.php\\&#8221; -s -d \\&#8221;nodeId=a&amp;attrId=aprontest -l -m9;\\&#8221;|grep Level|grep -oP \\&#8221;\\\\d+\\\\D+\\\\K\\\\d+\\\\D+\\\\K\\\\d+\\&#8221;` \/2.55|bc:3600000:] &gt;[*:\/bin\/sh@@-c@@\/usr\/bin\/curl \\&#8221;http:\/\/wink.hub.address\/set_dev_value.php\\&#8221; -s -d \\&#8221;nodeId=a&amp;attrId=aprontest -u -m9 -t2 -v`echo 2.55*%2$s\/1|bc`;\\&#8221;]&#8221;}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My husband has been\u00a0setting up OpenHAB to control our home automation. Our dimmers\u00a0are very direct &#8211; there&#8217;s a z-Wave binding that you set to 100 if you want it at 100%, set it to 18 if you want it at 18%, and so on. We have a handful of Zigbee bulbs, though, which are not &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33,29],"tags":[45,43,44],"class_list":["post-317","post","type-post","status-publish","format-standard","hentry","category-coding","category-technology","tag-coding","tag-home-automation","tag-openhab"],"_links":{"self":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/317","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=317"}],"version-history":[{"count":5,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/317\/revisions"}],"predecessor-version":[{"id":333,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/317\/revisions\/333"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}