{"id":371,"date":"2018-05-14T13:10:48","date_gmt":"2018-05-14T17:10:48","guid":{"rendered":"http:\/\/www.thesmarthomehookup.com\/test_install\/?p=371"},"modified":"2023-04-24T15:48:25","modified_gmt":"2023-04-24T19:48:25","slug":"node-red-home-assistant-how-to","status":"publish","type":"post","link":"http:\/\/www.thesmarthomehookup.com\/test_install\/node-red-home-assistant-how-to\/","title":{"rendered":"Node-RED + Home Assistant How-To"},"content":{"rendered":"<p><iframe width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/SuoSXVqjyfc\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen=\"\"><\/iframe><\/p>\n<p><strong>Integrating Node Red with Home Assistant<\/strong><\/p>\n<p>Today on the hookup we\u2019re going to look at how to integrate Home Assistant \u2013 a comprehensive smart home component platform, with Node Red \u2013 a simple and powerful automation platform.<\/p>\n<p>A fair warning before we start, this video is longer than I had anticipated.&nbsp; If this is your first time using node red I\u2019d suggest you watch from the beginning to the end.&nbsp; If you\u2019ve been using node-red for a while and would like to jump around the video here\u2019s a basic table of contents for you to scrub to the part of the video that interests you.<\/p>\n<p>I\u2019ve been coding in different languages for about 10 years now and I feel pretty confident picking up new languages on the fly, but for some reason writing automations and scripts in yaml always felt unnecessarily difficult to me.&nbsp; Not only was I confused by the mixing of syntaxes, but I was often discouraged from trying more complicated automations because I had a hard time troubleshooting them if they didn\u2019t work.<\/p>\n<p>Enter NodeRED, a platform that combines visual flow charts with simple scripting and programming to produce a fantastic automation experience.<\/p>\n<p>Today we\u2019re going to write some example flows to learn how to use each of the most useful nodes included in node red, and even some optional nodes.<\/p>\n<p>Before we start, let\u2019s talk a little bit about terminology.&nbsp; Over on the left here you\u2019ve got a bunch of these little rounded square things.&nbsp; Each of these is called a \u201cnode type\u201d, if I drag 2 of the same type out I would say that I had 2 nodes, but only a single node type.<\/p>\n<p>Next, at the top we have all of our different flows.&nbsp; I\u2019m actually not thrilled about this terminology.&nbsp; A flow in node red actually refers to everything in one of these tabs, I tend to make the mistake of calling a set of connected nodes a flow, but I believe the actual terminology for that is sequence.<\/p>\n<p>So a single rounded square is a node, a series of connected nodes is a sequence, and all of the sequences on a single tab are called a flow.&nbsp; This may seem like worthless knowledge, but it\u2019s actually important for understanding the different variable scopes in node red.<\/p>\n<p>The information sent through a single sequence is called a message object, and it can contain as many individual variables as you want.&nbsp; Each of these variables will have the prefix M S G, the default message variable sent out of a node is called msg.payload.&nbsp; A variable with the msg prefix only exists in the current sequence, meaning that if there isn\u2019t a line connecting nodes together the msg from one node cannot be processed by another node.<\/p>\n<p>A different type of variable scope is called \u201cflow\u201d, these variables can be called by all nodes on the same tab, which if you recall is called a flow.&nbsp; All the variables in this scope will the prefix \u201cflow\u201d.&nbsp; A third type of scope is called \u201cglobal\u201d and variables from the global object can be called by any node anywhere in node red.<\/p>\n<p>Okay, lets start with an easy example.&nbsp; I have an input Boolean set up in home assistant for \u201cRob Home\u201d, that\u2019s me, and \u201cLindsay Home\u201d, that\u2019s my wife.&nbsp; These input Booleans are toggled when our phones connect or disconnect from google wifi.&nbsp; In order to use these states to control the \u201chouse occupied\u201d input Boolean I have the following two sequences in node red.<\/p>\n<p>The first node type we\u2019re going to look at is the home assistant events state node.&nbsp; This node will send a message any time the selected home assistant entity changes states and the payload of that message will be the state that it switched to.&nbsp; Setting up these nodes is simple because it will auto populate the entity id for you as you start typing.<\/p>\n<p>In this automation We\u2019ve got 2 event states that correspond to the \u201cLindsay home\u201d Boolean, but each of them has a different \u201chalt if\u201d condition set.&nbsp; If halt if \u201con\u201d is selected it will only send the message on if she leaves the house, if halt if \u201coff\u201d is selected it will only trigger when she comes home.<\/p>\n<p>In the event that my wife or I come home it doesn\u2019t matter whether the other person is home, we always want to set the house occupied Boolean to on.&nbsp; We do this with a home assistant \u201ccall service\u201d node.&nbsp; These are slightly more difficult to configure because you have to use json to specify your entity ID, this is a necessary evil because using json allows you to pass additional arguments with your service data (things like brightness if you\u2019re calling the light.turn_on service, or message if you\u2019re using the notify service).&nbsp; If you don\u2019t feel comfortable writing your own json that\u2019s okay because you can go to your services developer tool in home assistant and use the dropdown menus to generate the correct json data.&nbsp; The call service node for an input Boolean lets us either turn it on, turn it off, or toggle it.&nbsp; In our case we want to specifically call turn on, not toggle.<\/p>\n<p>This is just about as simple as of a flow as you can get.<\/p>\n<p>Conversely, In the event that one of leaves the house we don\u2019t automatically want to set the house to not occupied.&nbsp; In these events we utilize a different home assistant node, the current state node.&nbsp; The current state node is set up almost exactly like the event state node, but it never triggers a sequence, it only goes in the middle of a sequence.&nbsp; By using this current state node we can check if I\u2019m still in the house when my wife leaves.&nbsp; If I\u2019m still in the house (meaning the \u201cRob Home\u201d input Boolean is on) the flow will be halted there.&nbsp; If I\u2019m not home it sends the message on to the next current state node, which checks an input Boolean called \u201chouse guests\u201d, because we don\u2019t want to be turning out the lights on our guests if my wife and I happen to both leave the house.&nbsp; If that Boolean is on, the flow stops, and if it\u2019s off it gets to continue on to trigger the call service node, this triggering the turn_off service.<\/p>\n<p>My favorite part about node red is how logical it is, lets just talk through this sequence real quick:&nbsp; Lindsay left, is rob home? If not, are there house guests? If not the house is unoccupied.&nbsp; Simple and intuitive.<\/p>\n<p>If you are ever trouble shooting a sequence and you can\u2019t figure out where things are going wrong you can attach a debug node to the sequence and see what is contained in the message object that is passing through that node.&nbsp; If you ever want to see what will happen when a node is triggered with a specific payload you can also use an inject node.&nbsp; You can choose what kind of payload you want to send from your inject node with the dropdown box.&nbsp; Once you\u2019ve set up your inject node, hit deploy and you\u2019ll be able to click this small square to trigger the message.<\/p>\n<p>Lets kick this up a notch and do an automation with multiple variables.<\/p>\n<p>In this automation I want to send an actionable ios notification to my phone and my wife\u2019s phone when a window is opened in our house.&nbsp; I want the notification to include a message stating which window was opened, a picture from our security cameras of that window, and a button that would allow us to manually trigger the siren on the house if we don\u2019t like what we see in the picture.<\/p>\n<p>Before starting this sequence we need to make sure we\u2019ve already done a few things in home assistant:<\/p>\n<ol>\n<li>Install the homeassistant iOS app<\/li>\n<li>Enable the ios component in home assistant<\/li>\n<li>Add a push action under your ios component<\/li>\n<li>go back to your home assistant iOS app and enable notifications and then tap \u201cupdate push settings\u201d<\/li>\n<\/ol>\n<p>Here\u2019s how the sequence works.&nbsp; To start, I have an event state node for each window.&nbsp; The sensors on my window send 1 of 3 message types: open, closed, or a replace battery message that includes the current voltage of the battery.&nbsp; Those 3 states are the 3 possible msg.payloads that will come out of the node, but I don\u2019t really care about the closed message, so I have a \u201chalt if\u201d condition set for the \u201cclosed\u201d payload.&nbsp; The next node type that it runs into is called a switch node, this node is like a fork in the road.&nbsp; Each fork can have different conditions associated with it, and it\u2019s possible for more than 1 of those conditions to be true causing the payload to split off in multiple directions.&nbsp; In our case I want the conditions to be msg.payload equal to open or msg.payload not equal to open. &nbsp;The reason I have it set up this way is that I won\u2019t know what the battery voltage is when it comes through, so I can\u2019t know what the exact message will be equal to.&nbsp; Another way I could have set this up is to use the contains flag set the value to \u201creplace\u201d.<\/p>\n<p>Next we are going to set the value of our notification properties.&nbsp; To do this we use a change node.&nbsp; I am not going to change the original message which is usually passed as msg.payload, instead I want to set a new variable within the message.&nbsp; I\u2019m going to call this variable msg.windowName, and I set it to the message that I want to appear on my notification, in this case \u201cFamily room window open\u201d.&nbsp; I\u2019m also going to set another variable called msg.image where I put the url of the snapshot from my cameras, I had to blur out most of this url, but I\u2019ll include a generic version of it in the github code. I\u2019m not sure if the home assistant people intended for this snapshot url to be known, but it works great for me.<\/p>\n<p>Now that we\u2019ve got all of the parts for our message we just need to put them in the right format.&nbsp; To do this I\u2019m going to use a function node. I like to cheat here and use a YAML to JSON converter, not because I can\u2019t write JSON, but why do more work than you need to?&nbsp; The actionable notification documentation is not great, so I\u2019ll post both the YAML and the JSON for a successful notification down in the description.&nbsp; You can see that for the message and attachment properties I have the variables msg.windowName and msg.image called instead of hardcoded values.&nbsp; After passing through this function node our msg.payload now contains this formatted json code.<\/p>\n<p>The final step for the actionable notification is to pass this beautifully formatted json to a call services node and the trick here is to just put a set of empty curly brackets in the data field because your msg.payload is automatically included into this field.<\/p>\n<p>There\u2019s another branch comes off this function node and checks to see if our exterior alarms are armed, and if they are it sends a load of other messages out to activate sirens and lights on the house.<\/p>\n<p>The last little thing on this automation is this other path on the first switch node.&nbsp; This sequence passes the low battery warning message in a standard non-actionable notification to my iphone only, because lets be honest, the wife isn\u2019t going to replace the batteries.<\/p>\n<p>So far we\u2019ve used the standard node red nodes: change, switch, and function, and we\u2019ve used the home assistant nodes: event state, current state, and call service.<\/p>\n<p>Before we move on any further I\u2019m going to say something about function node.&nbsp; The function node is probably the most powerful node type in node red, and I\u2019m sure a very common criticism of this video is going to be that I could have combined many of the nodes in my sequences together with a single function node.&nbsp; And it\u2019s true, but the whole reason we\u2019re using node red in the first place is to move away from having to write significant amounts of complicated code for our automations.&nbsp; The purpose of this video is to show how simple it is to use node red to do complex automations, and putting too many function nodes in is a good way to confuse home automation enthusiasts who are not also javascript programmers.&nbsp; I may do a video in the future about using javascript to do crazy stuff in node red, but this is not that video.&nbsp; Moving on!<\/p>\n<p>For our last automation we\u2019re gonna get crazy.&nbsp; Lets check out the node-red dashboard and make a nice little alarm panel that can be displayed on a tablet mounted to the wall.<\/p>\n<p>&nbsp;<\/p>\n<p>We\u2019ll start by installing the node pack for node red dashboard.&nbsp; Click on the hamburger in the upper right to open the menu and select manage pallete.&nbsp; The node red dashboard is called node (dash) red (dash) dashboard.&nbsp; Just search for it and click install.<\/p>\n<p>To see our end result we just change our url to \/ui instead of \/flow and that will bring us to our new security keypad.<\/p>\n<p>Wow that was a long video.&nbsp; Hopefully this was enough of a primer on node types with enough examples to help get you started with Node-RED.&nbsp; I\u2019ll post all of the flows I talked about in this video down in the description.&nbsp; If you want to use them as a starting point in your projects you can select import clipboard from the menu and paste in the code that I\u2019ve posted.&nbsp; Fair warning: If you import the flow before installing the necessary node types in palette manager you\u2019re going to get spammed with error messages in your debug window.<\/p>\n<p>Now for a personal note: Because this is a new channel catering to a fairly niche market, I\u2019m not a youtube partner yet which means these videos have zero monetization.&nbsp; Normally I make at least a few bucks from people using my amazon links in the description, but we didn\u2019t build anything in this video so I don\u2019t have any amazon links to post.&nbsp; If you\u2019d like to support me and you need to buy something on amazon anyways, go ahead and click the link in the description for the new echo spot.&nbsp; You don\u2019t actually have to buy the spot, anything you buy on amazon after arriving with my link will earn me a small referrer fee, which allows me to buy new products to showcase and motivates me to make more videos.&nbsp; So far I\u2019m still a few hundred in the red for this video making venture, but that\u2019s not really why I\u2019m doing it, so no big deal if you don\u2019t need anything right now.&nbsp; Anyways, if you enjoyed this video please consider subscribing and as always, thanks for watching the hook up.<\/p>\n<h3>Links<\/h3>\n<p>DrZzz Installing Node-RED Video: <a href=\"https:\/\/www.youtube.com\/watch?v=9HlQ0RUcUTE\">https:\/\/www.youtube.com\/watch?v=9HlQ0RUcUTE<\/a><br \/>\nExample Flow#1: <a href=\"https:\/\/github.com\/thehookup\/Node-RED-Examples\/blob\/master\/Example%20Flow%201.txt\">https:\/\/github.com\/thehookup\/Node-RED-Examples\/blob\/master\/Example%20Flow%201.txt<\/a><\/p>\n<p>Example Flow #2:<a href=\"https:\/\/github.com\/thehookup\/Node-RED-Examples\/blob\/master\/Example%20Flow%202.txt\"> https:\/\/github.com\/thehookup\/Node-RED-Examples\/blob\/master\/Example%20Flow%202.txt<\/a><\/p>\n<p>Camera Snapshot Generic URL: <a href=\"https:\/\/YourHassioAddress.duckdns.org:8123\/api\/camera_proxy\/camera.specificCameraName?api_password=YourPassword\">https:\/\/YourHassioAddress.duckdns.org:8123\/api\/camera_proxy\/camera.specificCameraName?api_password=YourPassword<\/a><\/p>\n<p>iOS Push Notification YAML:<a href=\"https:\/\/github.com\/thehookup\/Node-RED-Examples\/blob\/master\/actionable_iOS_push_config.yaml\"> https:\/\/github.com\/thehookup\/Node-RED-Examples\/blob\/master\/actionable_iOS_push_config.yaml<\/a><\/p>\n<p>Actionable Notification Automation YAML: <a href=\"https:\/\/github.com\/thehookup\/Node-RED-Examples\/blob\/master\/actionable_iOS_automation_example.yaml\">https:\/\/github.com\/thehookup\/Node-RED-Examples\/blob\/master\/actionable_iOS_automation_example.yaml<\/a><\/p>\n<p>Actionable Notification JSON: <a href=\"https:\/\/github.com\/thehookup\/Node-RED-Examples\/blob\/master\/actionable_iOS_json.js\">https:\/\/github.com\/thehookup\/Node-RED-Examples\/blob\/master\/actionable_iOS_json.js<\/a><\/p>\n<p>Example Flow #3: <a href=\"https:\/\/github.com\/thehookup\/Node-RED-Examples\/blob\/master\/Example%20Flow%203.txt\">https:\/\/github.com\/thehookup\/Node-RED-Examples\/blob\/master\/Example%20Flow%203.txt<\/a><\/p>\n<p>Support my channel:<br \/>\nPatreon:&nbsp;<a href=\"https:\/\/www.patreon.com\/thehookup\">https:\/\/www.patreon.com\/thehookup<\/a><\/p>\n<p>Music by www.BenSound.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Integrating Node Red with Home Assistant Today on the hookup we\u2019re going to look at how to integrate Home Assistant \u2013 a comprehensive smart home component platform, with Node Red \u2013 a simple and powerful automation platform. A fair warning before we start, this video is longer than I had anticipated.&nbsp; If this is your [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2612,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-371","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials"],"acf":[],"mb":[],"mfb_rest_fields":["title","gutenberg_elementor_mode"],"_links":{"self":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts\/371","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/comments?post=371"}],"version-history":[{"count":4,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts\/371\/revisions"}],"predecessor-version":[{"id":2367,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts\/371\/revisions\/2367"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/media\/2612"}],"wp:attachment":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/media?parent=371"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/categories?post=371"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/tags?post=371"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}