Beginners Guide to Node-RED in Home Assistant

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today I'm going to be talking about node-red briefly what it is how to install an entire assistant as an add-on and then we'll walk through some basic flows I can show you what automations to set up easily and what automations you can set up even without home assistant there's some things that you can do that are really cool [Music] [Applause] so you can use node-red without home assistant at all but today I'm going to show you how to run it in home assistant because I already have home assistant and it's easier and also we're going to show you how it links up with home assistant so that any entity State changes can be received in node-red and vice versa so here is home assistant we are going to go to settings go to add-ons and then this shows the add-ons that are currently installed if you go to add-on store here then it shows you a lot more options I'm going to search up here for node-red click here and then click install that's going to take a few minutes to install perhaps depending on how fast your equipment is if you install it on a Raspberry Pi for example it's going to be slower than if you've installed it on a virtual machine okay now that is installed so some things that you can do some options like ticking Watchdog is a good idea so that if it crashes it restarts auto update is personal preference really I personally don't allow things to automatically update and show inside bar is very handy so if I take this you'll see that it's appeared over here on the left hand side and then we need to start the add-on but first let's have a look at the configuration there's various things you can configure here you can look at the documentation for more information but basically there's some passwords and usernames you can use so that if you're connecting to node-red externally then you make sure that it's secure I'm going to click Start here and then we can go and have a look at the logs and see what it says okay so here's the logs and as you can see it's complaining because you haven't set up a credential secret so it's exited so we need to go and set that up so let's go back to configuration and create a secret once you've typed in your credential make sure that you press save down here so you can see here now that it's complaining about SSL certificates so let's go back to configuration again and have a look at that so here I've got SSL enabled but in this instance I actually don't have SSR so I need to uncheck this and press save if you're using a production instance and I highly recommend that you do have SSL and you have an SSL certificate on your home assistant instance right here's the logs again we can see now that it's been successful it says starting node red if I refresh this you can see it's installing various things if you look at this you can see that various things are starting including an nginx server so we'll just wait a few moments and then we'll see if we can go into node-red okay let's click over here to go into node-red and you can see that it's been installed successfully now we're going to minimize this so that we've got a bit more space to work with and then click through the little intro there gives you some idea of how to use it but let's go through it in a bit more detail the way you know dread breaks things down is that you have flows you can create new flows by pressing here for example so we've got flow one and flow 2 now and then you can isolate your different automations into different flows what you tend to do is actually have more than one Automation in a flow because otherwise you would end up with a lot of flows okay let's walk through an example so on the left hand side here you can see your nodes all of these nodes are nodes that relate to home assistant because you've installed the node-red add-on in home assistant then it means that it automatically installs these nodes for you if you've installed node-red separately without home assistant then you probably won't see these if you go over to the right hand side to the menu though and then go to manage palette this is where you can see the things that you can install so you can install different palettes which then have different nodes so for example here you can see that the node red one by default has 49 nodes which we'll see on the left hand side in a moment and you can see here that you can install lots and lots of different nodes here's the home system palette you can see that the home assistant palette uses the home assistant API via websocket that's so that entity changes are really responsive and come through to node-red really quickly if you click this you'll be able to see all of the entities that relate to home assistant that node red can use the first node we're going to grab is the events state click and drag it onto here and then double click it and now you can see there's quite a few options which might be a bit intimidating at first but it's not too bad really so server home assistant again it automatically sets that up for you because it knows that node-red is running within home assistant if that's not the case then you would need to add one yourself go to the configuration here add the URL and access token and a couple of other details let's change this server back to home assistant and now you can see that you can specify an entity here so this is going to show you all of the entities from home assistant for now I'm going to select the study speaker and you can define a state so basically what this means is is that if the state of this entity and home assistant changes then it's going to trigger an event for this node but you can also get it so that it just triggers an event when a certain State change occurs down here it shows you the output so basically when a state change occurs it will output some Json and it will do it in this format so you can see here that message dot payload will include the state of the entity the data this includes a lot of stuff which we'll look at later but things like the previous state and now the new state and then topic as well which is the entity ID down here you can also select some check boxes which can be quite useful so that state changes don't trigger an event under certain circumstances so as an example if it becomes unavailable then you might not want it to trigger an event so a scenario here might be that your Wi-Fi goes funny for a minute and so the device becomes unavailable and then goes back to the previous state you don't necessarily want to trigger an event off that it might just be that if it changes for example in this instance from say playing to not playing then you want it to trigger or vice versa so let's give it a name and press done there we go so that's the first node now we're going to scroll down here a little bit and look at some other nodes I'm now going to drag this debug node onto here once I've dragged it I can then click there and join them up and then I can name this debug node and then up here I'm going to change this from message.payload to complete message object so what this means is is that this debug node is going to Output everything that comes from this node here so we can have a look at it and see what state information it shows okay so now we've done that we can now deploy it so up in the top right you can see there's deploy and there's various options so full deploy means that all of the flows get redeployed and all of the nodes within it modified flows means that just the flows that you have modified get refreshed a modified nodes means that just the nodes that you've amended get refreshed so these can be quite useful if you've just made a few tweaks then you just want to reload the nodes versus if you've made lots of changes then you want to do a full refresh to ensure that everything is working as you expect it to so let's click this and there we go successfully deployed if you've got more flows then obviously it can take longer to deploy it now shows running here which basically means that it is monitoring the state of this study speaker so in a moment we're going to trigger a state change on this study speaker and then see what it looks like in here so I've loaded up home assistant on another Tab and now what we're going to do is we're going to go to Media text to speech Google and then in here I'm going to get it to say a message through the study speaker which should force a state change hello Mark you can play any text on any supported media player right now that's done let's go back to node-red okay here we go it shows idle as the state now here and if we go up to this up here which is the debug messages window you can see here there's been quite a few messages actually so it went from idle to buffering to playing and then back to idle so we can expand these out and have a look in more detail so under payload it shows idle that's the state and the data it shows you the details so you've got the entity ID there you've got what the previous state is and what the new state is so let's expand out this and then you can see not only has it got state but it's got all sorts of dates and time stamps that you can use which is really handy if we go to attributes it also shows you the class of the device and also the friendly name that you've specified in home system so you can use all of this data further on within your flow and in the new state that shows you what things change to as you can see here it's also got information about the volume okay let's delve into the next message so buffering and then the same information next message playing data and similar information so now we've done our first flow end to end you can see that whenever the city speaker changes state it outputs here what state it has changed from and to now let's take a look at some of the nodes on the left hand side I'm not going to go through them all but I'll go through a few of the key ones so you've got call service and this is basically to trigger a service within home assistant so if you've done automations in home assistant then you'll know what call service is and you can trigger those from node-red current state node allows you to get information about the current state of an entity in the home system you can do this at any point in time when a flow is triggered device that's when a device changes rather than an entity I don't tend to use this one very often at the moment but I have used it for the nest doorbell so that when someone rings the doorbell then it triggers a flow in node-red so events all an event stayed events all basically has all of the events that come from home assistant come through so this is quite a noisy node so you want to be careful when you're using this one this node here is the node that we used before that you can see up here pulse state is an interesting one so basically what you can do is you can have a timer set up so that every 10 seconds for example that you poll an entity and see what the state is and then trigger something to happen now trigger State this one is a bit more of an advanced version of the one we've used here basically so I'll show you that one in a minute wait until is another really handy node so basically if something happens and you want to wait until something else happens then you use this node so as an example you might want to say if motion is detected but then also wait until motion is detected in another part of the house until you actually turn a light on webhook is a more advanced node but I might do a session on this at some point in the future because it's really quite powerful the home assistant nodes in this section are a fairly recent addition within the last year or two so basically what you can actually do is create new entities in home system from node red so we've already used the debug node now and the inject node we're going to use shortly so the inject node allows you to trigger one of these flows or a section of the Flow by just pressing the button so that you can do some testing basically so let's drag this on here we go and you can see on the left hand side there's a button here so that when you include it in a flow you can click that button and it will trigger it also as well as that if you go into it you can actually set a schedule as well so that it will automatically run at a certain interval so for the next example we're going to need an entity to play with at the moment I don't have one of those set up so let's quickly go and do that and then come back so in home system we can go to settings and then go to devices and services and then this will show the Integrations so what I've got here is I've got some things on my network that have been discovered automatically so I'm just going to go and add this Shelly bulb we could give it an area I'll give it an area of bedroom because it's a bedroom lamp finish okay if we now go to developer tools and then we can have a look here we can see that we've got a new light so if I click here we can toggle the light on and off let's change the name of this bulb as well so we go up to the Cog here settings bedroom lamp front there we go we can change the icon while we're here click update and there we've got a friendly name for it now bedroom lamp front so now we've done that let's go back to node red and create another flow so now we're back in node-red we can create the new example either Below in the same flow or we can do a separate flow so let's just do it below here so in this example we're going to turn the lamp on by pressing the button in node-red so just dragging the inject node scroll back up and now what we're looking for here is the node that turns something on or off which is a cool service event so if we drag that in now connect these up now we need to double click into this cool service node go to The Domain light domain service let's do toggle entity and as you can see although we only just set this up in home assistant it's already showing in node-red so let's select bedroom lamp front and then let's give this a name as well okay so that's another simple flow let's deploy it now let's go back to home assistant press this you can see at the moment that the lamp is off let's go back to node red press this button here and you can see that it's been successful according to node-red so let's go back to home system and you can see it's been turned on it's gone back to the percentage that uh the lamp was on when we last turned it on which is five percent press it again and you can see the lamp is off so there we go there's the next simple flow in preparation for the next example we need to talk about a couple more of these nodes so let's scroll down a bit and get to the function section so there's a lot of nodes here that can really be quite useful when you get a little bit more advanced so we won't go into all of them but just a few of the key ones so the top one function that's where you can write your own JavaScript functions so if you're familiar with JavaScript then that can be really powerful but these ones are switch node and the change node you don't have to do any coding and you can achieve a lot of the things that you can achieve with the function node so the switch node for example it actually lets you choose what route to take in a flow based on an input so for example if something is on or if it's off you can get it to do different things the change node that changes what the message says so for example earlier when we looked at the study speaker and we looked at the debug output we saw that there was lots of different pieces of information or with change you can actually change some of that data or add new data to the Json message we'll talk about this a bit more when we go through the next example another one which can be handy is a delay you don't generally want to have delays that are too long for things in case things get rebooted or crash Etc but short delays of a few seconds or a few minutes can often be handy Within These flows another common one that I use is time range so you can determine whether it's a certain time of day or whether it's night time and there's lots of use cases for this one as well so we've kind of exhausted the test string now so let's hop over to my production instance of node-red and go through some more examples so here is a fairly simple example again but a better use case of something that I actually use so I have some reminders that send me a notification to my phone every month or two so I've got one for car maintenance and one for checking the batteries of things and one for checking the Hoover so to make sure it's maintained correctly uh you may argue that I should maintain the car more than every two months but that's it is what it is so if I click into here you'll see that this is called Big Time app so this is actually a node from a separate palette so you need to add this one separately you won't find this in node Red by default but ultimately if we go back into it you can see that you can set different times for things to turn on and off but you can also do it based on month year day there's all sorts of exclusions so this could be a video in itself really but apart from that if you look in here build notification you can see that what we're doing here is we're using a change node to set some information in the message so firstly we're setting the message.payload to be empty so an empty Json message and then we're setting message.payload.data to some useful information now you'll see why we do that in a minute so here you can see that I've set a title of what I want the notification to have the message itself which is what our tires need to be set to and then some additional things around how you want the notification to vibrate cancel that cancel that and then if we go to here this one is fairly straightforward then so you've got the notify domain because I've already set up that integration so that you can send notifications to my phone and then you've got the service of mobile app smartphone and that name will obviously depend also on how you've set up that device in home system so if you look down here you can see that these are the properties that you need to pass in and these correspond with what was added in the previous section here there's actually a couple of things that are not relevant anymore so for example Target that I've got here used to be used but now because I've actually got the entity pointing to my phone specifically you don't need that if you look below there's some more similar examples so this one is a reminder to check the water pressure so over time the water pressure can drop in your system and you should top it up really if you've got a Combi boiler or if you've got a separate boiler and tank so that's what I do and what I do here is is I have it a bit more useful whereby it actually detects when you're within the area of where the tank is so here every two weeks it runs 8 P.M on a Sunday and what it does is it sends me a notification to my phone but what it also does is detect whether the motion sensor has been triggered or not in the bedroom so what happens here is is that when it gets to 8pm on Sunday this will run it will then get to this node here which is a waiting till node and then it will basically wait until the state of this PIR is set to on so basically for when I get into the bedroom and it will wait for 20 hours until it times out it probably should be less than that really and then it will send a message to the Google that's in that room if you look below here you can see that there's a exercise reminder one unfortunately I'm not really strict with these anymore so you can see here that this is disconnected so basically this actually isn't running at the moment so this node will run still but it's not connected to anything so anything will happen so far all of the examples have involved home assistant to some degree but you don't have to use home assistant it makes things a lot easier especially if you've got a lot of things that are automated already but if you look here you can see that there are some things that you can do without home system so here I've got xiaomi gateways and these are configured directly in here so I've added a palette for xiaomi gateways and they connect directly to the gateways so then the gateways have got sensors attached to them so pirs contact sensors and buttons and then here what happens is whenever any activity is detected or a button is pressed this node will fire and then it will send a message what I've done here is is I've used one of these link out nodes and then you can link it to another flow so basically I've got the gateways on each floor and in the garage and then if we have a look at garage for example you can see that then when something happens from a xiaomi Gateway it checks to see if that is from the garage button or from the workbench button it then checks to see what clip type was it was it a single click was it a double click or a long press and then you get to do different things in this instance when you press the button I have got it to trigger an entity in home system however you don't need to do that a lot of these lights and plugs that I have are mqtt based and so you can set up a separate mqtt server which doesn't involve home assistant if you wish and then you could call that through here you might be wondering what this node is here well this is a bit of a hangover from old versions of node-red really where sometimes you'd get a failure on the call service if you had a long string of nodes before it was certain pieces of data in it so I probably need to do some housekeeping here so if we look below this you can see that there's something that happens when garden lights are triggered so in this instance when the guideline lights are turned on it automatically turns them off again after an hour so you can see that here after an hour it will start a timer running and then after the one hour it will turn the garden lights off again so if we go into here you can see it cools the service of turn off if we scroll a bit further down then you can see that the garage lights turn on automatically when I go into the garage I've got a PIR at either end of the garage and then it checks to see is it motion that was detected because these pirs can detect motion but also light sensitivity so yep if there's motion then it says check if the light's already on which you don't really need to do and then if it isn't on then turn the garage light on so hopefully you can see from these few examples that we've done in node-red that it's very powerful we've only just scratched the surface really if you'd like to see more videos on node-red please comment down below and let me know what you'd like them to be about well that's it for today so thanks until next time foreign [Applause] [Music]
Info
Channel: Let's Automate
Views: 37,654
Rating: undefined out of 5
Keywords: NodeRed
Id: KXcwUdDqXXo
Channel Id: undefined
Length: 27min 10sec (1630 seconds)
Published: Fri Mar 17 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.