{"id":1010,"date":"2016-05-22T16:01:05","date_gmt":"2016-05-22T23:01:05","guid":{"rendered":"https:\/\/partofthething.com\/thoughts\/?p=1010"},"modified":"2019-10-03T23:33:56","modified_gmt":"2019-10-04T06:33:56","slug":"adding-a-ir-send-and-receive-capabilities-to-my-home-assistant-based-smart-home","status":"publish","type":"post","link":"https:\/\/partofthething.com\/thoughts\/adding-a-ir-send-and-receive-capabilities-to-my-home-assistant-based-smart-home\/","title":{"rendered":"Adding IR send and receive capabilities to my home-assistant based smart home"},"content":{"rendered":"<p><em>Or, how to control your A\/C from your phone<\/em><\/p>\n<p>You may have seen my <a href=\"https:\/\/partofthething.com\/thoughts\/?p=937\">earlier post<\/a> about my new home automation setup. Well I just upgraded it! I just finished testing out a new infrared remote control send and receive capability and it is pure awesome. Here&#8217;s what I can do now from within <a href=\"https:\/\/home-assistant.io\/\">home-assistant<\/a>:<\/p>\n<ul>\n<li>Turn on and off my new DeLonghi portable air conditioner from Costco, and set the temperature to whatever<\/li>\n<li>Turn on and off the TV and press any buttons from its remote control<\/li>\n<li>Turn on and off the stereo and virtually press any buttons from its remote control (which I never even had!)<\/li>\n<li>Use an old random blueray remote control to set scenes, turn lights on and off, etc. (basically do anything under home-assistant&#8217;s control)<\/li>\n<\/ul>\n<p>This post is about how I did it on my Raspberry Pi.<\/p>\n<p>Update: A video!<\/p>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/6I_Lfpda0hc\" allowfullscreen=\"allowfullscreen\" frameborder=\"0\"><\/iframe><\/p>\n<p><a href=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/05\/small.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-1012\" src=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/05\/small-1024x683.jpg\" alt=\"Annotated setup with IR sensors\" width=\"660\" height=\"440\" srcset=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/05\/small-1024x683.jpg 1024w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/05\/small-300x200.jpg 300w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/05\/small-768x512.jpg 768w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><\/a><\/p>\n<p><!--more--><\/p>\n<h2>Setting up LIRC and learning the A\/C remote<\/h2>\n<p><a href=\"http:\/\/www.lirc.org\/\">LIRC<\/a> is a linux library to interface with infrared (IR) remotes. Setting it up, including schematics for hooking the IR receiver and emitter to the GPIO ports of the Pi are well-described <a href=\"http:\/\/alexba.in\/blog\/2013\/01\/06\/setting-up-lirc-on-the-raspberrypi\/\">here<\/a>. So following those instructions is the first step.<\/p>\n<p>(2019 Update: <a href=\"https:\/\/github.com\/raspberrypi\/linux\/issues\/2993\">See this info<\/a>, as some raspberry pi internals related to LIRC have changed!)<\/p>\n<p>Then I had to teach LIRC about my equipment. The one I was most worried about was my DeLonghi &#8220;Pinguino&#8221; portable air conditioning unit from Costco (AN125HPEK). I was able to successfully learn the commands from the remote using LIRC in raw-capture mode. <a href=\"http:\/\/absurdlycertain.blogspot.com\/2013\/03\/lirc-raspi-remote-control-configuration.html\">This post<\/a> was very useful in figuring it out. A\/C units send full packets, including information about what mode it&#8217;s in, what numerical temperature it&#8217;s set to (not just up and down), etc. Here&#8217;s what I did:<\/p>\n<ol>\n<li>I recorded a bunch of raw data and remember the order of buttons you pressed. For example, first I had the remote at the maximum temperature and then after I started recording I pressed &#8220;decrease temperature&#8221; a bunch of times until I was at the minimum:<\/li>\n<\/ol>\n<pre class=\"toolbar:2 nums:false lang:default highlight:0 decode:true&gt;sudo mode2 -d \/dev\/lirc0 -m &gt;ac_going_down_in_celcius.dat&lt;\/pre&gt;\n&lt;p&gt;2. Then I pasted the results of those various files into a delonghi.conf file:&lt;\/p&gt;\n&lt;pre class=\" toolbar:2=\"\" nums:false=\"\" lang:default=\"\" highlight:0=\"\" decode:true=\"\">begin remote\n\nname DELONGHI\nflags RAW_CODES\neps 30\naeps 100\n\ngap 183413\n\nbegin raw_codes\n\nname OFF\n     8941     4531      505     1699      506     1697\n      510      621      508   \u2026\n\nname AC_23\n     ...\n\nend raw_codes\nend remote<\/pre>\n<p>3. Then I used irrecord\u00a0 in analyze mode to process the raw signals and extract the hex codes:<\/p>\n<pre class=\"toolbar:2 nums:false lang:default highlight:0 decode:true&gt;irrecord -a delonghi.conf &gt; lircd.conf&lt;\/pre&gt;\n&lt;p&gt;4. Then I moved that file to \/etc\/lirc\/lircd.conf and restarted lirc. Testing with&lt;\/p&gt;\n&lt;pre class=\" toolbar:2=\"\" nums:false=\"\" lang:default=\"\" highlight:0=\"\" decode:true=\"\">irsend SEND_ONCE DELONGHI FAN_LOW<\/pre>\n<p>blew my mind when it worked. Yay. Excellent progress. I used irrecord in its normal learn mode to learn my Sony remote and it just asks you to name each key before you press it. That worked great. The codes I recorded for this A\/C are available <a href=\"https:\/\/partofthething.com\/thoughts\/?attachment_id=1032\">here.<\/a><\/p>\n<figure id=\"attachment_1016\" aria-describedby=\"caption-attachment-1016\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/05\/sm_IMG_2777.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-1016\" src=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/05\/sm_IMG_2777-300x200.jpg\" alt=\"My A\/C\" width=\"300\" height=\"200\" srcset=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/05\/sm_IMG_2777-300x200.jpg 300w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/05\/sm_IMG_2777-768x512.jpg 768w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/05\/sm_IMG_2777-1024x683.jpg 1024w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/05\/sm_IMG_2777.jpg 1555w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-1016\" class=\"wp-caption-text\">My A\/C<\/figcaption><\/figure>\n<figure id=\"attachment_1013\" aria-describedby=\"caption-attachment-1013\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/05\/sm_IMG_2773.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-1013\" src=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/05\/sm_IMG_2773-300x200.jpg\" alt=\"My IR transmitter\" width=\"300\" height=\"200\" srcset=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/05\/sm_IMG_2773-300x200.jpg 300w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/05\/sm_IMG_2773-768x512.jpg 768w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/05\/sm_IMG_2773-1024x683.jpg 1024w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/05\/sm_IMG_2773.jpg 1555w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-1013\" class=\"wp-caption-text\">My IR transmitter<\/figcaption><\/figure>\n<h2>Writing a new home-assistant component for lirc<\/h2>\n<p>With what I had up to this point, I could use home-assistant as is and use the shell_command component to control things around my house. This was what was recommended <a href=\"https:\/\/github.com\/home-assistant\/home-assistant\/issues\/716\">last time a lirc component was requested<\/a>. I tried it out and it worked great:<\/p>\n<pre class=\"lang:yaml toolbar:2 nums:false decode:true\">shell_command:\n\u00a0 ac_on: irsend SEND_ONCE DELONGHI AC_25\n\u00a0 ac_off: irsend SEND_ONCE DELONGHI OFF\n\u00a0 stereo_on: irsend SEND_ONCE STR-DE205 KEY_POWER \n\u00a0 stereo_video1: irsend SEND_ONCE STR-DE205 KEY_VIDEO1\n\u00a0 stereo_off: irsend SEND_ONCE STR-DE205 KEY_POWER\n\u00a0 stereo_video2: irsend SEND_ONCE STR-DE205 KEY_VIDEO2<\/pre>\n<p>But then I got thinking that it&#8217;d be cool if I could have a IR remote control various aspects in home-assistant. My wife sometimes complains that she has to go find a phone to turn on the lights. If I had a remote laying on the table it&#8217;d be really easy to select scenes and whatnot. So I thought I&#8217;d write a lirc component.<\/p>\n<p>Conveniently, there&#8217;s a <a href=\"https:\/\/pypi.python.org\/pypi\/python-lirc\">python-lirc package<\/a> in pip that brings lirc capabilities into Python3. So making a component was just a matter of interfacing between that package and home-assistant. I whipped up a component and tested it out by having KEY_1 turn on a scene and KEY_0 turn all lights off. It works! SO AWESOME!<\/p>\n<p>I will open a pull request and try to get this feature into home-assistant for all to enjoy.<\/p>\n<figure id=\"attachment_1014\" aria-describedby=\"caption-attachment-1014\" style=\"width: 200px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/05\/sm_IMG_2775-e1463947520407.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-1014\" src=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/05\/sm_IMG_2775-e1463947520407-200x300.jpg\" alt=\"My &quot;new&quot; home remote\" width=\"200\" height=\"300\" srcset=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/05\/sm_IMG_2775-e1463947520407-200x300.jpg 200w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/05\/sm_IMG_2775-e1463947520407-768x1152.jpg 768w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/05\/sm_IMG_2775-e1463947520407-683x1024.jpg 683w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/05\/sm_IMG_2775-e1463947520407.jpg 1037w\" sizes=\"auto, (max-width: 200px) 100vw, 200px\" \/><\/a><figcaption id=\"caption-attachment-1014\" class=\"wp-caption-text\">My &#8220;new&#8221; home remote<\/figcaption><\/figure>\n<figure id=\"attachment_1017\" aria-describedby=\"caption-attachment-1017\" style=\"width: 196px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/05\/lirc-remote.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1017\" src=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/05\/lirc-remote.png\" alt=\"Lirc remote in home-assistant\" width=\"196\" height=\"112\" \/><\/a><figcaption id=\"caption-attachment-1017\" class=\"wp-caption-text\">Lirc remote in home-assistant. UPDATE: I&#8217;ve since converted to a event-handler so it doesn&#8217;t show up like this<\/figcaption><\/figure>\n<p>Now I can make rules that, you know, turn on my tv, my stereo, and put up a fireplace video like this:<\/p>\n<pre class=\"lang:yaml decode:true toolbar:2 nums:false\">lirc:\n\nautomation:\n- alias: Full fire sequence\n\u00a0 trigger:\n\u00a0\u00a0\u00a0 platform: event\n\u00a0\u00a0\u00a0 event_type: ir_command_received\n\u00a0\u00a0\u00a0 event_data:\n\u00a0\u00a0\u00a0\u00a0\u00a0 button_name: X_KEY_NETFLIX\n\u00a0 action:\n\u00a0\u00a0 service: script.fire_sequence\n\nscript:\n\u00a0 - fire_sequence:\n\u00a0\u00a0\u00a0 alias: Fire Sequence\n\u00a0\u00a0\u00a0 sequence:\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 - service: shell_command.fire\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 - delay: \n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 seconds: 1\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 - service: shell_command.tv_power\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 - delay:\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 seconds: 1\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 - service: shell_command.stereo_line_in # turns on stereo\n\n<\/pre>\n<p>So awesome. I have submitted the code as <a href=\"https:\/\/github.com\/home-assistant\/home-assistant\/pull\/2139\">PR 2139 <\/a>and maybe it will go in the next release.<\/p>\n<p>I&#8217;ve started toying with the shell_command to allow it to build the command from slider input so I can set the A\/C to any value I want. Will open another PR if <a href=\"https:\/\/community.home-assistant.io\/t\/pass-value-to-command-line\/587\/3\">this guys<\/a> doesn&#8217;t.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Or, how to control your A\/C from your phone You may have seen my earlier post about my new home automation setup. Well I just upgraded it! I just finished testing out a new infrared remote control send and receive capability and it is pure awesome. Here&#8217;s what I can do now from within home-assistant: &hellip; <a href=\"https:\/\/partofthething.com\/thoughts\/adding-a-ir-send-and-receive-capabilities-to-my-home-assistant-based-smart-home\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Adding IR send and receive capabilities to my home-assistant based smart home<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":""},"categories":[3,69,75],"tags":[],"class_list":["post-1010","post","type-post","status-publish","format-standard","hentry","category-computers","category-electronics-and-physics","category-home-automation"],"_links":{"self":[{"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/posts\/1010","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/comments?post=1010"}],"version-history":[{"count":14,"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/posts\/1010\/revisions"}],"predecessor-version":[{"id":1760,"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/posts\/1010\/revisions\/1760"}],"wp:attachment":[{"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/media?parent=1010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/categories?post=1010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/tags?post=1010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}