{"id":365,"date":"2018-05-30T13:03:17","date_gmt":"2018-05-30T17:03:17","guid":{"rendered":"http:\/\/www.thesmarthomehookup.com\/test_install\/?p=365"},"modified":"2023-04-24T15:48:05","modified_gmt":"2023-04-24T19:48:05","slug":"mastering-node-red-custom-alexa-commands-node-red-dashboard","status":"publish","type":"post","link":"http:\/\/www.thesmarthomehookup.com\/test_install\/mastering-node-red-custom-alexa-commands-node-red-dashboard\/","title":{"rendered":"Mastering Node-RED: Custom Alexa Commands + Node-RED Dashboard"},"content":{"rendered":"<p><iframe width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/4QfoXdcfqKI\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen=\"\"><\/iframe><\/p>\n<p><strong>Mastering Node Red Part 2<\/strong><\/p>\n<p>Today on the hookup we\u2019re going to learn how to use Node-RED to control anything in our smart home with Amazon echo, and set up custom user interfaces using the Node-RED dashboard.<\/p>\n<p>This is part 2 of my Mastering Node RED series, if you\u2019re just starting with node red I highly recommend you watch part 1 of this series first, click ***point*** that link to go there now.&nbsp; We\u2019re going to be using a bunch of custom nodes in this video that are not included in the standard node-RED package.&nbsp; I\u2019ve included all of the names of these packages in the description below.&nbsp; To install them click on the hamburger icon in the upper right corner and select manage palette, then click on the install tab and input the names of the packages into the search field.<\/p>\n<p>Lets start with amazon echo.&nbsp; If you\u2019re using home assistant you already have a few ways to incorporate amazon alexa commands into your home automation including emulated hue and home assistant cloud.&nbsp; So why should we even bother looking at amazon echo in node red? 1. Simplicity:&nbsp; Never before has it been so easy to set up a new alexa command 2. Customizability: having it in node red means that the payload of your commands is infinitely customizable. And 3: Locality: Using the alexa local node causes your raspberry pi to act as the hub for each of these devices and removes any reliance on cloud servers.&nbsp; A major theme that you will see in my videos is that I really don\u2019t like cloud services, and although alexa itself is a cloud service, not using a skill provided by a third party greatly decreases the security concerns in the alexa platform\u2026 other than amazon itself having all your data.<\/p>\n<p>&nbsp;<\/p>\n<p>Because the&nbsp; Alexa Local node is actually just another emulated hue device that means it not only responds to on\/off commands, but also to brightness messages, This means you can say things like \u201cturn on the kitchen light\u201d but also \u201cset the blinds to 50%\u201d or \u201cset that charger to 3\u201d.&nbsp; Lets walk through the setting up my outdoor shades.&nbsp; I\u2019m going to make an alexa local node and call it \u201cTest Shades\u201d, and I\u2019m going to pipe it into a debug node, just so we can get an idea of what\u2019s coming out of this message. I\u2019ll hit deploy and and jump into the alexa app on my phone, after going to smart home \u201cadd device\u201d my new test shade will be discovered quickly.&nbsp; Lets test it out \u201calexa turn the test shade to 40%\u201d, you\u2019ll notice that my debug node didn\u2019t really tell me much, we\u2019re going to need to click on the debug node and select \u201coutput complete message object\u201d.&nbsp; Now lets try that again \u201calexa turn on the test shade\u201d.&nbsp; Now you can see I\u2019ve got WAY more information in there.&nbsp; Here\u2019s what we see as part of this message object:<\/p>\n<p>Msg.bri is 40 because I told it to set to 40%<\/p>\n<p>Msg. on_off command: is false because I didn\u2019t specifically tell it to turn on or off<\/p>\n<p>Msg.payload: is on, because 40% is of course at least partially on<\/p>\n<p>Change direction: 0 (if you have any idea what this means or how to make it equal 1, go ahead and post down in the comments)<\/p>\n<p>Bri_normallized: .4 this is just the brightness out of 1<\/p>\n<p>On: \u201ctrue\u201d because 40% is not 0%<\/p>\n<p>Device name, light ID, and port aren\u2019t super important for this automation because they won\u2019t change based on the command I give to alexa.<\/p>\n<p>When I programmed these shades I noted it takes almost exactly 13 full rotations of the bar to roll the shades up and down, so when I programmed the different stop points on the shade I just made them 0-13.&nbsp; This is all well and good when you\u2019re writing custom MQTT messages, but how do we translate this into a brightness percentage out of 100?&nbsp; The answer is the range node.&nbsp; If you\u2019ve ever used the map function in Arduino this node works the same way.&nbsp; We tell it the range of the message we\u2019re receiving (in this case 0-100 in the msg.bri variable) and the range of the message we\u2019d like to have, which for my shades happens to be 0-13.&nbsp; The result of this flow will be to have the shade turn to a specific % based on the command that I give it\u2026 but there\u2019s a problem\u2026 This system remembers the previous brightness when the on\/off commands are issued, but if I say to turn the shade on, I actually want it to turn on to 100%.&nbsp; To do this I\u2019m going to use a switch node.&nbsp; In this node I\u2019m going to check the msg.on_off_command variable.&nbsp; If it\u2019s true, I want to set the shades to 13, if it\u2019s off I want to set the value to zero.&nbsp; I\u2019m using function nodes here because I need to have correctly formatted json to send to the call services node.&nbsp; Lets do a really quick crash course on writing JSON.<\/p>\n<p>I\u2019m gonna hop over to the services tab under developer tools to write my json. We\u2019re trying to send the input number set value service, lets generate as much stuff as we can with the dropdown menus.&nbsp; I stupidly have my couch shade called input_slider1, I should fix that, but it\u2019s not going to happen right now.&nbsp; Now you can see down below that we can send a value property in this json object as well, but how?&nbsp; It\u2019s super easy.&nbsp; Each parameter is separated by a comma and each entry needs quotation marks, so to add the value parameter we\u2019ll just add comma, quotes, value, end quote, colon, quote, some value, end quote.&nbsp; Now lets copy it into our function node, and we\u2019re going to have a couple of if statements.&nbsp; If msg.payload == \u201con\u201d meaning I issued the on command, then I want to set the value to 13, and if msg.payload == \u201coff\u201d I want to set the value to 13.&nbsp; Easy enough.<\/p>\n<p>Now we\u2019re going to use this same json that we wrote to set the shades to custom heights.&nbsp;&nbsp; To do this we\u2019ll just paste in our json from before but we\u2019re going to replace the \u201csome value\u201d part with our msg.bri value that we\u2019ve reduced to a 0-13 range.&nbsp; Notice that since msg.bri is a variable we don\u2019t want to have it in quotation marks.<\/p>\n<p>Now our flow checks to see if I specifically used the \u201cturn on\u201d or \u201cturn off\u201d command and sets to all the way down if I did, and to a specific percentage value if I didn\u2019t.&nbsp; This is just one example for what you can do with alexa and node red, but the sky\u2019s the limit .<\/p>\n<p>&nbsp;<\/p>\n<p>Speaking of things with limitless possibilities, lets talk about node-red dashboard.&nbsp; I know what you\u2019re gonna say: you already have a dashboard in home assistant with all kinds of cool options like floorplan and HA dashboard, why do you need another dashboard?&nbsp; Let me convince you.&nbsp; In this example we\u2019re going to make a slick alarm panel ui, without writing any code at all.<\/p>\n<p>Lets start with some dashboard basics.&nbsp; The UI has different pages called tabs, and each tab has different modules called groups.&nbsp; In the panel on the right where your debug tab usually lives you\u2019ll notice that you have a new tab called \u201cdashboard\u201d.&nbsp; In this tab you can customize different attributes of your UI like color and size.&nbsp; This is also where we\u2019re going to add new screens to our UI by clicking the add tab button.&nbsp; For our example I\u2019m going to add a tab called \u201calarm panel: and a group called \u201ckeypad\u201d.&nbsp; I\u2019m going to make this group 6 units wide because I want to have a 3 button wide keypad and I want each button to be 2&#215;2.<\/p>\n<p>Now that I\u2019ve got a group I just need to populate it.&nbsp; To do that I\u2019m going to add 12 buttons corresponding to 0-9, asterisk, and clear.&nbsp; For each of these buttons except clear I\u2019m going to have the payload be exactly the same as the label on the button, but it certainly doesn\u2019t have to be.<\/p>\n<p>Once I\u2019ve got all the buttons set up and arranged in the order I\u2019d like them to be I can preview my alarm panel by going to my normal node-red url and replacing everything after the base url with \/ui.&nbsp; You can move your buttons around as needed until you are happy with the way your ui looks.<\/p>\n<p>Once you\u2019ve got your ui looking nice, we need to program it.&nbsp; We\u2019re going to program it a simple way first, and then we\u2019re going to get a little fancy.<\/p>\n<p>Let\u2019s start by making sure that our user is putting in the right code.&nbsp; To do this we\u2019re going to need to join our consecutive msg.payloads together in a join node.&nbsp; I want a 4 digit pin, so I\u2019m going to set up a few options in here pertaining to that.&nbsp; I want to send the joined message after 4 parts, or 20 seconds, whichever happens first.&nbsp; The 20 second part basically just makes the current code entry time out after 20 seconds.&nbsp; The other part you\u2019ll notice here is that there is a default option that says that if the node receives a payload of message.complete with a value of \u201ctrue\u201d it will automatically send the message on, even without 4 parts joining or 20 seconds passing.&nbsp; This allows us to set up a \u201cclear\u201d button using a change node that allows the user to start over if they enter an incorrect pin.<\/p>\n<p>We obviously only want the keypad to respond if the correct message is sent, so we\u2019re going to send this message to a switch node that looks for the correct pin.&nbsp; For our example I\u2019m going to use one eight seven four which is my current number of subscribers at the time of writing this.&nbsp; After this node, the easiest thing to do would be to toggle the alarm on or off using a call services node and the input_boolean.toggle service.&nbsp; But that wouldn\u2019t really be that useful because we probably need some time to leave the house after turning the alarm on.<\/p>\n<p>So instead of that call service node we\u2019re going to check the check the status of the exterior alarms.&nbsp; This node will return either \u201con\u201d or \u201coff\u201d and we\u2019ll send that payload into a switch node.<\/p>\n<p>You might have noticed I skipped a node there, we\u2019re going to circle back around to that at the end.<\/p>\n<p>If the current status node returns on, this sequence it disarms the alarm using the input_boolean.turn_off service and then outputs the message \u201calarm disarmed\u201d in text to speech using the text to speech node in node-red dashboard.&nbsp; This text to speech node outputs whatever is in the msg.payload of the message object which could be a string of text or an mp3 audio file.<\/p>\n<p>If the alarm was off it starts the process of arming.&nbsp; To start this process I set 3 variables with a single change node.&nbsp; I set msg.payload to \u201cArming in 45 seconds\u201d so the text to speech node will say it, I set msg.keypad to \u201carming\u201d which will be displayed on the keypad, and I set the flow wide variable \u201carming status\u201d to arming.<\/p>\n<p>Next the message is passed into a delay node, which holds the entire message object for 45 seconds before passing it on.&nbsp; After 45 seconds it passes into a switch node that checks to make sure the flow wide variable \u201carming status\u201d is still equal to arming, and if it is, it activates a call service node that turns the exterior alarms on. &nbsp;The other branch sets that flow wide variable \u201carming status\u201d back to \u201cnot arming\u201d.<\/p>\n<p>If you\u2019re following along at home you might be really confused as to why I\u2019ve made this arming status variable.<\/p>\n<p>Lets say you set the alarm and it starts arming, and then you decide you don\u2019t want it arm you would have to wait 45 seconds for it to arm and then turn it off, which is totally unacceptable to me.&nbsp; In order to fix this we\u2019ve got this \u201carming status\u201d variable.<\/p>\n<p>The first switch variable that we skipped over checks that flow wide arming status variable.&nbsp; It doesn\u2019t matter where in the flow a flow wide variable is set, we can always read it.&nbsp; If we punch in our pin while the alarm is in the process of arming our message gets sent down this other path where it sets \u201carming status\u201d to \u201cnot arming\u201d which then prevents the alarm from being set after the delay node.<\/p>\n<p>The other problem with our delayed arming is that our status window is going to get out of sync.&nbsp; For this we use a trigger node.&nbsp; A trigger node sends a payload immediately, then sends a second payload after a specified amount of time.&nbsp; The first payload I send will be \u201ccancelled\u201d to give the user some feedback that the pin was entered successfully, and then after 3 seconds it will set the status to \u201coff\u201d which is the current state of the alarm.&nbsp; Because my alarm status window is set to display msg.keypad and not msg.payload I have to have another change node here to shift msg.payload into msg.keypad.<\/p>\n<p>The last thing to consider when using flow wide and global variables is that since they are used to recall things that have already happened any restart of node red will cause them to not have any value associated with them.&nbsp; This causes a problem where a reboot of node-RED leads to flow.arming status variable not existing, which would prevent anything from passing through this switch node.&nbsp; We can solve this in two ways:&nbsp; We can either create a new fork in our switch where flow.armingStatus is null (which means that the variable hasn\u2019t been given a value yet), or we can give it a default value any time node-red starts up.&nbsp; To do this, we will use an inject node, inject nodes are mostly used for debugging, but by clicking this \u201cinject once after X seconds\u201d check box you can have them run once whenever node red restarts ensuring that your flow and global variables will have default values.<\/p>\n<p>Now all that\u2019s left is to check out our new UI.&nbsp; To access it you point your browser to your normal node-red URL, but instead of the \u201cpound flow\u201d part, you replace that with ui.&nbsp; You can access this url from a tablet mounted on a wall to make a pretty slick home control hub and alarm panel.&nbsp; Lets see it in action.<\/p>\n<p>If you didn\u2019t know this, I\u2019m a high school teacher and we\u2019re in the first week of summer!&nbsp; What this means for you is I\u2019ve got a lot more time to finish up all of the videos that I\u2019ve been working on.&nbsp; Make sure you hit that subscribe button because in between my normal Wednesday videos I may be releasing additional tutorials.&nbsp; Coming up this week:&nbsp; Automating remote backups for both hassio AND node-RED.&nbsp; I\u2019ve got a few user submitted sequences to include in my last Node-RED how to video, go ahead and post a comment if you\u2019ve got anything else that you absolutely want to see included.&nbsp; And as always, thanks for watching the hookup.<\/p>\n<h3>Links<\/h3>\n<p>Node-RED Palettes:<br \/>\nnode-red-contrib-alexa-local<br \/>\nnode-red-dashboard<\/p>\n<p>Node-RED Flows:<br \/>\n<a href=\"https:\/\/github.com\/thehookup\/Node-RED-Examples\/blob\/master\/Alexa_Local.json\">https:\/\/github.com\/thehookup\/Node-RED-Examples\/blob\/master\/Alexa_Local.json<\/a><br \/>\n<a href=\"https:\/\/github.com\/thehookup\/Node-RED-Examples\/blob\/master\/security_panel.json\">https:\/\/github.com\/thehookup\/Node-RED-Examples\/blob\/master\/security_panel.json<\/a><\/p>\n<p>Get started with Hassio:<br \/>\n<a href=\"https:\/\/amzn.to\/2KOpeNN\">https:\/\/amzn.to\/2KOpeNN<\/a><br \/>\n<a href=\"https:\/\/amzn.to\/2kiLKTN\">https:\/\/amzn.to\/2kiLKTN<\/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>Twitter: @TheHookUp1<br \/>\nMusic By: www.BenSound.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Mastering Node Red Part 2 Today on the hookup we\u2019re going to learn how to use Node-RED to control anything in our smart home with Amazon echo, and set up custom user interfaces using the Node-RED dashboard. This is part 2 of my Mastering Node RED series, if you\u2019re just starting with node red I [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2610,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-365","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\/365","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=365"}],"version-history":[{"count":4,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts\/365\/revisions"}],"predecessor-version":[{"id":2365,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts\/365\/revisions\/2365"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/media\/2610"}],"wp:attachment":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/media?parent=365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/categories?post=365"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/tags?post=365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}