Home Assistant Node-RED Install Plus Examples

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
as you get more involved with smart homes sooner or later you'll be wanting to create rules so that you can automate everything now whether it's a personal assistant an app on a phone or a smart hub what you tend to find is the process is usually the same except that is when it comes to home assistant and that's because it comes supplied with an add-on called node-red now what that allows you to do is to create a rules in a more visual way which makes the whole process a lot easier but it's also easier to troubleshoot and test your rules as well so how do you actually install and use node-red well if that's something you interested in finding out then stick around and watch this video because that's what we'll be going over [Music] now installing node-red is really easy because this is a community add-on that gets supplied with home assistant so in order to install node-red we need to click on settings then on add-ons and then we'll click add on store and then if we scroll down where we've got home assistant community add-ons lower dan will see an option here for node-red so we'll click on that and then we'll click on the install button and off it goes and installs the software well node-red's now installed but before i just go ahead and click on that start button there's a few tweaks that i want to make first so we've already got start on boot enabled by default but i i want to enable this option for the watch dog so that way if node red would have crashed for example home assistant will try to get it back up and running again i also want to enable the option show in sidebar because that will make it easier to get access to node-red and be able to get access to my rules easier over here on the documentation tab i mean feel free to read through these because there's quite some interesting information here on the configuration but i'm just going to jump straight ahead to the configuration tab because there's two settings i want to change first one is i need to supply a credential secret here if i don't it's going to complain when i try to start it so i would suggest putting in something a lot more stronger and more complicated than what i'm using what this is for is to actually encrypt a file that stores your credentials in it now this is a one-off process it's going to create that file it's going to encrypt the file using that password so the problem is if at a later date you decide to change that well it's already created the file that's already encrypted and what you'll find is that node-red won't be able to get access to that information anymore so what i'd suggest is put this password in now while you've actually just done the installation keep it in a safe place and then that way you've got a recovery option now in my particular case i don't need to you know use this http node or the http static i'm not going to be using node-red for serving web content for example it's just basically just going to be used as my rule machine here on home assistant so i'm just going to leave those settings blank now another thing i am going to change is this option for ssl again i'm not really using it as an http server anyway but although it's got some examples for the actual things like a private key up there it won't work because the files don't exist anyway and like as you can see i haven't even got around a setting home assistant itself with proper certificates anyway so i'm just going to disable that option otherwise i'm going to have problems so what we're going to do then is click on the save button and then that saves our changes there we go back to the info tab and then we'll click on start and then that'll get node-red up and running we'll no red's now up and running and there are actually several different ways that we can get access to it by far the easiest is just to click on the node-red option over here so here in the left-hand pane we've got node-red if i click on that it'll take me straight to node-red that's basically the same as clicking on that option there open web ui so that that is really a shortcut for that i mean if i want to get access to here i've got to go to settings then i've got to go to add-ons then i've got to go to node-red and then that gives me the option to click that so as you can see by enabling that show in sidebar this is much easier another alternative though is actually if we go over to configuration uh we scroll down you can see we've got the set 2.1880 by default so at the moment my web browser is pointing to port 8123 to log into home assistant if i would actually point the web browser to the same computer but to port 1880 it'll actually connect me into the load read that way i'd still have to log in using my eha home assistant management credentials but i may as well just log into home assistant itself anyway so as i say once i click on that it just takes me straight into note red and it starts loading up various information and then gives me the option to start creating my rules basically i'm trying to keep things simple i'm going to provide a high level overview of this node read application and how you actually go about creating an actual basic rule so over here on the left-hand side we've got a home assistant toolbar now what we can do is free up some space by clicking on this option up here it's saying sidebar toggle so if i click that it takes away the text so we've only got the icons but it frees up some space now here is where we've got our work area or workspace if you like but as far as node red is concerned this is a flow so at the moment we've got the flow here and it's called low one the idea is you can have multiple flows if you want more click the plus button what you can do is you can change its name if you like to give it something more meaningful so if you double click that tab there we can change that to be say for example the kitchen and the idea then is that maybe i want to create some rules in this floor that are specific to the kitchen so all of my kitchen rules are stored in this flow and then i'll create more floors for the different areas but you can also group rules together if you like so for example i've got a floor which represents all of my lighting but entirely up to you you can go about this however you like where you like i'm doing in a quite so i would say like a hybrid approach where i've got similar rules grouped together uh more specific area rules going with that specific area flow but in this case we've just got the one floor to begin with and then down the left what we've got are the nodes and we're actually going to be using these nodes to create rules now we get quite a lot of nodes with node red itself i mean we can scroll down there's quite a lot there you can filter them out so if i type in say time for instance up here it comes back with a series of nodes that have got time in the name not necessarily related to time they've just got time in the name but it's a useful way to filter things out and try and find a specific node that you're looking for but one thing to point out is these are just generic nodes you're not going to find node in here that represents your kitchen light for example you may have configured home assistant with a kitchen light and you may be able to turn it on and off within home assistant but node-red itself doesn't know about that it doesn't have a node in here for your kitchen light instead it's got these generic nodes and then what you can then do is you can then configure those nodes to be whatever that specific device is that that node's meant to represent so to give you a basic idea of how we create rules uh here within the flow we need to start with an event that's typical for any rule engine something needs to trigger the rule to begin with now there's different events you can have i'm just going to start with this one here which is an event state so for example let's say we want to detect motion within a kitchen for instance what i'm going to do is i'm going to click on that node with my left mouse button i'm going to then drag this node into the area here of our flow then i'm going to let go and it's going to create a node in here now i've actually got to change this i've got to edit this to represent the device the state that i'm looking for so if i double click on it there's a lot of options in here we've got to pick uh to tell it about but i'm not going to do that yet because i just want to cover the basics so let's say we've configured this and what this is now doing is it's monitoring motion detection in the kitchen i also need an action in other words i need a light turning on for example if we detect motion so i can use this node up here for that so i'm going to left click on that drag it into the area and then let go and then what i'll do is something similar i've got to configure this to represent what it is i wanted to do so i would double click on this and what i can do with this for example is i can point it to the kitchen light and i can tell it to turn the kitchen light on for instance but this is still incomplete we've got two nodes what we need to do is to actually join them together to do that if we hover over the output box here on our state change node here if i click on that you can see i've now got a wire and what i need to do is join this wire to this node here so you can see that box has changed to orange if i leave go we've now got our two nodes connected together so we've now got our sequence here so the idea is if we configure these properly we'll be able to get a state change that if motion is detected in the kitchen this will trigger this rule that rule will then be passed over some you know some information will be passed over to our node here which in turn will then trigger the actual light to turn on so that gives us a very very basic rule now the one thing to point out is that this rule on itself doesn't actually do anything we've actually got to deploy the change and that's why we've got a an option up here that's gone red which says deploy so we've got to actually click on that box to deploy now the reason it's coming up and saying confirm deploy is because we haven't actually configured anything if i had configured it i wouldn't be getting this warning but in this case as i said i'll cover things in more detail afterwards but we're going to click confirm deploy in this case just so i can complete the process and what it means is that this rule is actually now deployed and it's if it was set up properly then what would happen is if somebody walked into the kitchen it would trigger a state change that the motion sensor would change from off to on this would then wake up and then send an instruction over to here which would then trigger the light to be turned on so that's a very very basic rule now you don't always have to drag nodes across from this left hand side what you can do is just select an existing node control c control v we've now got a copy i'll just click somewhere else on this on this work area here and i've now got a copy of that so that's useful when you're replicating things says you're having to reconfigure things you might have to just make a slight change for example to an existing node you've already configured and once i've got that selected i can also delete that node if i don't need it anymore i can do the same with an entire rule control c control v gives me an entire rule so this is this is really useful when you've got things like um rules for like the lights for example they're all pretty much the same the only difference is the actual device themselves they're all going to be triggered by motion turning on and then they're going to turn a light on so you've just got to change the actual device itself within the two nodes it's very easy to replicate existing rules but again i just want one rule to begin with now one thing to bear in mind is that technically nothing's really changed but the slightest change triggers this requirement to deploy the change again even if i just move a node it's going to prompt me so that's something to bear in mind now we've got a very basic rule here but what i can do is i can actually expand on that if i want let's say i want to put a condition into here let's say for example i actually want to check if it's actually dark in the room first so we've got a state um node here which checks the actual state of the device so let's say i've got a light sensor in the kitchen what i can do is i can drag this node into the actual flow now i've got choices here so again i'll have to configure that double click on it give it the details but what i can do is set this up and say well if it's going to be less than a certain lumen level within the room then we'll carry on with the rule but we've got to fit it within this rule there's different ways of doing this i can either select that connection that i've got and then delete it and then what i can do is i can then join things up that way so i've now got to join between these so that's one way we can do it the easier way really i'll just delete those links join them back up again what i'm going to do is i'm going to drag this node over the line and you can see how it's turned to dashes if i now let go as you can see it's actually joined that node in between so i've now got an event i'm then going to get a condition and then i'm going to get an action so it's only if this condition is satisfied that the rule will be carried on so that's a an easy way to extend your rules if you want but again make a change you've got to deploy the actual change now when it comes to flows you can have multiple flows so this one here represents rules for the kitchen for example but what i can do is i can create multiple floors um so i can have a flow over the kitchen a floor for the living room i can have a floor for all my lighting so i click on that plus button there this one i've been playing around with this but normally you get like a sequential order to it but what i can do is i can change this and say this one's going to be the lounge floor for example click on done so i've now got a kitchen floor and i've got a lounge floor i can copy and paste things from you know one floor to another if i want so i'll go to my lounge so i've now got a similar rule in there oh obviously they're copies but i'm not gonna do that but that's just to give you an example of how you can copy and paste between the flows but again what you want to do with these floors is entirely up to you one thing i've been doing is i'm running this hybrid mode but i'm also actually running a test flow so if i click on plus i'm going to call this one and testing for example so the idea is what i want to do is i don't want to disturb existing flaws because what i found is that you create all these rules if you actually push a change out an existing rule can actually get triggered even if you didn't actually intend it to and that's because of this default setting if i click on the drop down menu it's set here to deploy everything basically it says it deploys everything in the workspace that means everything every flow every rule will get redeployed and that's a bit of a problem i guess i say i might suddenly find some lights get turned on when i really didn't intend them to be so what i do is i create a completely separate flow so if i'm doing any testing for example let's say i'm testing a new device or i've found some better way of doing a rule for example i'll create the rule within my testing flow but what i'll do is i'll change this instead of being full i'll set it to modified laws and then click on deploy now what we've now got here is we've got our kitchen rule over here let's say we've got a lounge rule here and then we've got our testing flow here if i make some changes in here so i'm just going to drag this one into here if i click on deploy now if i click on confirm deploy i'm still getting warnings about the other flows that's basically because it's a case of they don't actually have any configurations but normally what would have happened is that this would have only deployed the change specifically for my testing flow it wouldn't have bothered with these ones because they're already deployed so that's that's a real benefit to going for that option to say modified flows only you can be more granular and go for you know modified nodes if you want to but i find this is much better because then it means i've got a more stable automated system any of my existing flows that have got rules in don't get uh you know pushed back out again the only thing it gets modified is the actual testing flow so that's that keeps things a heck of a lot more stable now that's how we go about creating rules on a basic level now another area to point out is right up here we've got an option to update home assistant nodes now normally when you install home assistant whenever you get an update you'll get notifications from a home assistant say well there's a new version of home assistant um so you can then install that and the same will happen to add-ons there's a new update for an add-on you'll get a notification to say oh there's a new update for node-red for example and then it'll update node-red but this is specific to node red itself and this is to do with the actual nodes that we've got installed these actually get updated it doesn't always involve a complete update of node red itself there can be updates pushed out for um certain nodes for example so if your nodes need to be updated you'll want to click that button up there that blue button up there and it'll actually download and update the nodes that you've actually got already installed so that's quite useful now over here we've got our info tab so this is a way of navigating around if you want it's giving us bits of information down here so this is a another way of moving around here um over here we've got a help option now what this does is gives you a lot more detail about the actual nodes that you've got now you can either work your way through these nodes individually but what you can also do you can also search for specific nodes but if you click on a node while you've got that um tab open it actually navigates to that actual node type and you can find out information about how to actually configure them the different fields in there that you can set and so on so that's that's very useful if you want to get more information about a node but one really useful tab that i find is the debug node because what you want to be able to do is to be able to test and troubleshoot now what you can actually do is attach debugs to various points within these nodes to get the outputs and the outputs from those debugs will end up here so this is a great way to find out if for example we deployed this rule but it wasn't working the light wasn't turning on for some reason we can actually tap into this rule look at the output in our debug node and find out what is the output maybe we've haven't configured the rule quite right because we're going to running checks within these rules we're going to be testing you know is the number is the output less than this number more than this number does it match this string there's all sorts of checks that takes place and if you don't get it quite right then the rule doesn't work in which case you want to be able to troubleshoot it so that's where the debug window comes in extremely useful over here we've got our menu option where we can do various things but basically that's the the overall basics of the actual of node word itself so the next thing to do is to just cover some basic examples of what we can actually set up when it comes to automation with node-red so the first thing we're going to do is to create a very basic rule which is just to detect motion if motion is detected to turn a light on so to get started we need to create an event and what we're monitoring is a state change for a motion sensor now that's what we're interested in is when that state changes from off to on because it's it's basically just a switch so what we've got is a an event state node here so we're going to drag that over to the the flow here but we need to edit this because it's just a generic uh node here we haven't told it anything about this motion sensor so that's what we need to do next so we'll double click on that we then give it a name so this is just something that's maybe uh to us so i'm gonna type in motion detect because it's it's more of a question really we're actually asking the question has motion being detected but again it's up to you what you want to call this uh for the server i'm just going to leave it as home assistant unless you've deployed node red onto a different server or you're going to try and get it to reference a completely different home assistant server altogether then you may as well just leave this as is it's picking out the first entity that it can find now one thing to point out is that the motion sensor that we've got is a device but it's got multiple sensors within it it's got a temperature sensor a light sensor and it's also got a motion sensor now those sensors are referenced as entities and that's what we're interested in we're looking for information about the actual entity itself in this case specifically the motion detection entity so it's picked out a door sensor but what we're interested in is our motion sensor so i'm going to just type in motion to filter things out then from this list i'm going to pick up motion sensor and what we're interested in is when the state is on now that's all i really need to put in there because it's an exact match i mean there's other things you can do with strings it's either not equal to it you can have less than less than or equal to more to do with numbers but in this case we're talking about strings you can pick out other things regular expressions all sorts you can choose but this is good enough uh for motion sensors at the bottom well basically by default we're actually saying we're not interested if the actual state hasn't changed we actually want to see a change in the state in our case it's either going to be off or it's going to be on so what we're interested in is when it goes from off to on that's when we're interested that's when we want our rule triggered so we're going to leave that set as is and we don't want to just keep getting notified when it's on we won't know when it's changed from off to on so we'll leave that there and then click on done we now need an actual action we need to actually turn the light on so we've got this call service node here so we're going to drag that over to our flow again we'll double click that because we need to give it some information we want to call this is up to you i'm just going to give it a name of turn light on because that's what it's going to do again it's going to be the server as is now for the domain this is a very flexible node it supports all sorts of different node and features if you will now in our case we're dealing with lights so i need to tell it that it's a light that we're dealing with so that's why i'm just typing it in build as the list out and i'll select light for the service it's then because i've picked light it's just giving fewer options when it comes to what it is we're capable of doing with this light in other words you can toggle it so you can toggle it from after on onto off in our case we want to turn it on so i'll select turn on now you've got a choice of area device or entity now when it comes to a light well the light is it doesn't have any actual sensors within it so the light is the actual device so i'm going to click on device and select what is basically my only light that's basically it but just out of curiosity you've got an optional pay which is area so what you can actually do is you can set these uh sort of service nodes up to actually turn lots of things off and within an entire area for example by picking out the actual area and that's quite useful and if you were dealing with some sort of node that had certain sensors within it or certain entities within it i should say you could pick those out and be more specific but in our case it's just a light bulb and all you want to do is turn it on so that's that's it basically but it gives you the sort of things that you've got where you can set colors you can set um brightness levels and that sort of thing that's something you'd be getting into the realms of the data section forward this is just a very basic bulb i just want to turn it on and i'm just going to for that reason click done and that sets up our our light node now because the motion sensor has been given a state here we've got a effectively we've got a condition here we're saying if the stator is on do this if the state is off do that basically so that's why we've actually got two options so we've actually got one of the top which basically saying if our criteria has been met if the state is true it'll output out this output box here if on the other hand it hasn't it'll output out this box here so in our case we do actually want it to meet that condition that motion is being changed to true so we want that top box where the statement is true so we'll take a wire from there and connect back to our light and then we'll click on deploy now just to point out i've got this set to modified flows and that's once i've set this it just stays like that it just means every time i click on deploy it'll only deploy flows that have changed so if i'd had any rules in here they wouldn't have been affected so we've now got a very basic rule so what i'm going to do is i'm just going to trigger some motion here now if i go back to my overview on home assistant the lights turned off so clearly something isn't working so the next thing to do is we need to troubleshoot this we need to find out why this rule isn't actually working although we've set up a very basic rule here where we're looking out for motion detection and the intentions to turn a light on it didn't work something went wrong somewhere so we need to troubleshoot it we need to investigate why this isn't working and fix it so there's different ways we can do this the easiest way at least when it comes to motion detection and what we're going to do is we're just going to zoom in a bit right underneath the actual node of some information so usually when things happen node-red puts some information below the node so the first thing to point out is there's nothing under this node here in other words whatever went on here didn't result in this being told to do something this was nothing uh completely empty whereas over on here we've got some information and what you can see is a little red icon then we've got off and it tells you the time the date and time when that actually occurred so straight away you might notice that's off in lowercase it's all in lowercase so what i'm going to do is i'm just going to trigger the motion it's now on in lower case so that was deliberate i deliberately put a cap at a law and just to put a bit of a spanner in the work so i could do this troubleshooting well it's another thing to point out is that even though it's you know motion is on it's still showing this red icon in other words our condition that we set up on this motion detection hasn't been satisfied and the statement that we've got in there is not true so what's actually happening is that the motion's being triggered it's not meeting our requirement and the actual node is then sending its output out here it's not sending anything out here because it's not true um it doesn't you know the motion detector uh sensor here it hasn't changed to on with a capital o it's changed on with a lowercase o so we need to fix that so i'm going to double click on that i'm going to take away the capital o replace it with a lowercase o click done and then we'll redeploy our change so detection is now showing is off so i'm just going to trigger that sensor again and there you go let's fix that problem so you can see this time we have met the condition because we do actually have a state of on our statement says is that is the actual state on lowercase or lowercase n and it is so that's why we've got it green here and also because it's now sent that information across to the actual node here we've now got information under this want to say that the light was turned on and that's also green because it was successful so if i click on the overview here and sure enough in home assistant when i was seeing a light that's turned uh actually on so i'm gonna turn that off i'm gonna go back to node red because we don't have anything in here to actually turn the light off this is just a very basic example of how to actually turn the light on but there is another way to actually troubleshoot it other than just that strategy there because what we have is what's called a debug node so if i drag that debug node over to here now we do know now what the cause of the problem is i'm just going to double click that i'm going to put it back to capital o again but i don't necessarily let's say i don't know about that what i can do is i can send some information out to this debug node here so i'm going to take a wire in case the statement is true but i'm also going to take a wire in case the statement is false because i want to know the outcome regardless and then i should be getting something out of here before i wasn't getting anything there was nothing showing up on this node at least by connecting both outputs to the uh debug node i should be getting something in a debug window so i'm going to deploy this i'm going to click on the debug window here i'm gonna just move my hand in front of that and you can see it's it's actually sending out that the actual message is on so that's what's being popped out the actual message payload so what's actually happening is that the sensor is sending node red or more specifically home assistant a payload of on lowercase o or case n so that should stand out to us as well i mean you can actually drill into this so there's more information you want i mean if you double click on this um you can at the moment it's just sending out the payload if you select the drop down menu and select complete message click done click deploy i'm just going to delete the previous contents there i'm going to do that again and you'll see this time we've got more information and if we click on the arrow to expand things out we've now got the data as well as the actual payload you can expand that out so you can get the old state was off and the new state is on get details of last change last update there's there's all sorts of good good stuff you can get out of changing that um output status statement there because by default it just gives you the payload and it doesn't necessarily tell you everything now in my case we already know what the problem was as i say we're going to get it either way so i'm just going to put this back to a lowercase and now that we know click deploy delete our debugs and actually while i'm on what i'm going to do is i'm going to delete this debug node because when you're running debugs you don't want this running all of the time now that we've found the problem now that we've fixed the problem we don't need that debug information anymore so we've deleted our debug node we'll click deploy and now what we've got is a a very basic motion detection to turn the light on the only trouble is it's going to stay on so we need to make some changes here now what we've got here is a very basic rule to detect motion and then turn a light on the only problem is we're not turning the light off now there are different ways that we can tackle this and the easiest way is to take advantage of the timer within the actual motion sensor itself because what actually goes on is that when motion is detected it's changing to a state of on now once actual motion is no longer being detected it then starts a timer and if it hasn't found any more motion it then sends a notification to in this case home assistant to say i'm no longer detecting motion so what we can do is take advantage of that building timer now rather than me creating a completely new node nodes rather than dragging a new call service across and setting that up what i want to do is i'm just going to make my life easier i'm going to copy this existing node that we've got to turn the light on and i'm just going to make some slight changes to actually turn it off so i'm going to change the name for instance and i'm going to change the service to turn off click done and then what i'm going to do is i'm going to take the output from this output box here and i'm going to patch that through to this node here and then i'm going to click on deploy so if we go back to our node here what we've got is basically a test of is the state on so in other words if motion is being detected the state is on and that would mean a true statement that's a if the condition meets the and the requirements if the statement is true then it sends an output out this box on the other hand if the statement's false in other words it's no longer detecting motion it sends the output out this box so that's pretty easy to set up we're just adding the extra node to actually turn the light off and connect it to the the other output box there so we've deployed the change i'm going to trigger that motion sensor so as you can see it's already turned the light on and then once it stops detecting motion what's going to happen then as i say it's going to start this timer rolling and then this will then get an off response and then what we'll get is like being told to turn off so as i say it was it was a lot easier really for me to just copy the existing node and just make a couple of changes rather than creating a completely new service node and having to go through that whole process itself and this even gets a lot easier when you've got multiple rules that are pretty much the same where all you're doing is essentially changing the motion sensor in a different room uh lights in a different room the whole thing gets a lot easier that way by copying and pasting but any case as you can see the statement's false and the state of the motion isn't on it's off so it's obviously center an output down this wire and it's been sent to this node here which tells it to turn the light off so the light is now being turned off now one thing to bear in mind though is this is probably not the best way it's the simplest way of doing it but it's not necessarily the best way the reason being is that we're relying on a timer whatever's being configured on this actual motion sensor now that's a setting usually within the actual device itself and if this is a battery operated device yeah that's a bit of a problem because we can make changes with within home assistant to change that parameter so let's say the actual timeout's one minute what we want is an outage of say four minutes just wait four minutes for a for no motion and then tell us and then we'll turn it off maybe one of 10 minutes or something you know it depends on the room and maybe it's a transit area or whatever we want a specific time and we want to test it as well the only problem is because that's a parameter that's set within the actual device itself and we'll configure that within home assistant easily enough but the problem is we've got to actually wait for this device to wake up specifically if it's battery operated that is because a battery operated device actually goes to sleep and it won't be taking any input like configuration changes for 4 hours 8 hours 12 24 hours whatever the setting is for its sleep time so the problem is you could then be left waiting a long period of time before the change gets applied before you can then test it again now you can actually forcibly wake the device up but that seriously eats into the battery life battery life really goes down very very quickly when you wake these things up and when they go to sleep they still operate as a motion sensor it still detects motion and so on but it's those configuration changes where you need the actual device itself to be awake so that is a bit of a problem so it's not ideal especially when you're starting out you could eat through batteries pretty quickly i mean once it's up and running it's probably fine but just something to bear in mind it's a pretty easy way to do it it's just not necessarily the best way i feel now the rule that we've got at the moment is still pretty basic the idea is if motion is detected turn the light on when motion stops being detected turn the light off now the only trouble with that is well that's all well and good if you're dealing with a room or a closet for example where it's always dark but most rooms have got windows and so during the daytime well it's light enough anyways there's no point turning the light on now there's different ways to tackle that what you could do is you could have an actual time range as a condition to say well if it's night time we'll turn the light on if it's day time we won't the only trouble i find with that approach is that if the actual weather outside's bad so you might have a thunderstorm for example or it's just dull and overcast it can still be dark during the daytime in a room in which case i find it's better to actually use a light sensor now this particular motion sensor that i'm using has a built-in light sensor so i can take an advantage of that i can find out what the light level is in the actual room that that motion sensor is in but there is a different strategy you could use you could have one light sensor that covers the entire house for example and i'll include a link in the description to show you an outdoor light sensor i've been using and the idea then is that you can have multiple rules for each individual room with simple motion sensors that just detect the motion but then you've got a light sensor covering the entire house effectively and if it's light enough outside then stands to reason it should be light enough inside as well but if you're still having problems you can get you know more sophisticated motion sensors like this one that have got an actual light sensor built in in any case in this particular example we're just going to use the light sensor that's built in so what we need to do is make a modification heat of the rule and we're going to take advantage of this current state node so i'm going to left click on that drag it into the floor then i'm going to drag it between those two nodes that the dashes appear on the line that i'm going to let go so now we've got essentially we've got a condition here so i need to double click on this and i need to point it to the actual light sensor but first i need to give it a name so i like to make my rules easier to understand so i know what it is that we're actually doing here this comes in useful you know if you come back to the rules at a later date and you can't remember what it is you did just by putting in that question i know exactly what it is we're trying to find out i'm trying to find out is it actually dark the server can stay as is because home assistant and node-red are actually running on the same computer the entity id is the actual light sensor or as it's referred to an aluminum sensor actually so i'm just filtering the actual list of entities out and here we've got our test room aluminum sensor so i'll select that now the sensor is actually feeding back a certain value a level that it detects it's reporting as the light level and it's going to vary it it really does i mean you can buy the same two devices off you know off a vendor and find out they'll give you different values but that's just something to bear in mind you'll have to find out what the actual level of light is for that specific sensor at a specific time when it's dark enough to justify it but in this case i don't necessarily know what that is at the moment so we're just going to make up a value and then we're going to find out so at the moment by default it's set up to expect a string value that we want to check but we actually want to check an actual numerical value so i'm going to click on the drop down menu here and set it to less than because there's a certain tolerance once the light level falls below a certain level then to me it's going to be dark so that's why i'm going for less than it automatically switches over to numerical format here and i'm just going to put in a number of 100 as an example but we're going to change this afterwards so we're going to click on done and then we're going to click on deploy so essentially what we've now got here is a bit more sophisticated rule which says if motion is detected it then sends the output to this node here this one's then making a check if the light level is below 100 then we send the output over to this node and what this node does is turn the light on so the threshold that i've got at the moment is if the light level falls below 100 as far as this concern this sensor is concerned it's dark enough to justify turning the light on now what i want to do is go over to the overview window here which is our dashboard at the moment the actual light sensor that i've got in this motion sensor here is reporting a valuable 110 so the idea is what i would do is wait until it's dark enough in the room to justify turning the light on and then finding out what level the actual light sensor is reporting that's my tolerance and just set it to something slightly higher so in this case it's reporting 110 for example so what i could do is say well if this is going to be below 111 i mean it it's a case of i'm just doing this for the sake of a test you'll have to find out what exactly your threshold is because this particular room it's light enough at the moment but i want to actually demonstrate the actual light getting turned on so i'll click on deploy so now what should happen is if i trigger the motion the light level that's being reported at the moment is 110 but the check on this is is it less than 111 which it is so it should result in the light being turned on so i'm just going to change the motion sensor there so it's triggered the light because we are reporting a level below 111. now as before it's still going to go through the process where if motion stops being detected then the motion sensor itself's gonna tell home assistant i'm not detecting any more motion and then it's gonna send a an output out here to the actual node here which turns the light off but what we've achieved now is by using this current state node is now we can actually check to see if it's actually dark in the room so that makes the rule a lot better it means that you're not going to walk into a room all the time and the light's just going to come on all the time it's now only going to come on if it's dark enough now one problem that i have with this particular rule is that i'm relying on the actual motion sensor more specifically a setting within the motion sensor it decides how long to wait before turning the light off now if i want to change that actual timer i've got to update the settings within the motion sensor now when it comes to battery-operated smart devices they go to sleep uh they might be going you know to sleep for 4 hours 8 hours 12 hours whatever it is and you're not going to get those changes taking effect until the device wakes back up again now what i could do is actually force it to wake up but that can drain the battery quite a bit so what i prefer to do is to actually have my own timer in which case what i like to do is to add an additional delay on top of whatever it is the actual motion detection and sensor is using so i'm just going to drag this node over to here because i'm going to add in another node which is a delay node so i'm going to drag that in drag it over our line there so now by default what's going to happen is we're going to actually delay the output by 5 seconds before we actually instruct the light to be turned off now what you set this to is entirely up to you it's going to vary depending on the room um if it's a transit area maybe it's a minute two minutes or so on you'll just have to find out what's best for you now in this example i'm just going to select uh we'll leave it at five minutes for the sake of the test we'll click on done we'll click on deploy and then what will happen now is that when motion gets detected a light will get turned on when motion stops being detected the output will get gets sent to our timer here and it's going to start this timer and then when that timer runs out then we actually get the light turned off and the beauty of this is the fact that i i've actually got control of this i don't have to make any further changes to the actual motion sensor i don't have to wake it up or anything all i've got to do is just change the setting here and redeploy the actual change um you know within node red itself so i'm just going to trigger that motion so off it goes and it's it's turned a light back on again and what we should then find is that once the actual motion stops being detected which i think it's about a minute um that this is set to at the moment the motion sensor will stop so it's timer will stop it'll then pass the output over to the actual delay timer that we've got here and then this is going to then start running its own timer on top of that that timer that we had there so there we go so you can see it's it's now just literally started the actual countdown process so this is going to wait for five minutes before it turns the light off now in a test that's not really practical is it but as i say the benefit really is the fact that i've now got control over how long i've wait so i'm not going to wait four or five minutes that's going to take too long so i'll just pause the video there well i ended up rerunning that test because there was a bit of an interruption but essentially the result is the same just different times involved so what happened was motion was detected and as far as the light sensor was concerned it was dark in which case we then ended up with a light being turned on so at 1757 the light was turned on motion wasn't being detected anymore so the motion sensor timed out after about a minute and so as you can see 1758 we'd get enough message sent to home assistant that then results in a five minute timer and then once that times out hence why we've got a zero showing there now the light then gets sent an instruction to be turned off so that's why we've got a six minute gap between here so 1757 light gets turned on 1803 in other words three minutes about six the light gets turned off so this sort of rules good enough for sort of transit areas the idea is someone's going to walk into a room like a hallway but they're not going to stay there that long in which case they're going to walk into the room and more or less you know straight away they'll just walk straight back out again in which case it's fine to just have a timer begin and then it'll just turn the light off the only catch is other rooms this sort of thing isn't practical because once that timer actually starts it's going to time out and the light's going to get turned off but either way there's still a useful way of being able to use that delay node now another useful node that we get by default and we can take advantage of to improve this rule is called a stop timer now what that allows us to do is to actually stop the timer from running so in this particular case what we've got is just a basic delay timer the idea is when motion stops being detected we'll have a five minute timer begin and when the timer runs out we turn the light off the only trouble is let's say somebody walks into a room the light gets turned on then whatever reason motion stops being detected maybe they sat down for instance if within that five minute window they get back up well it doesn't really don't do anything basically because this this time it's still counting down and it's still going to result in the actual light being turned off but with a stop timer if we send it a stop message the timer stops and that would keep the light actually turned on so i'm just going to filter these nodes and i'm going to select the stop timer i'm going to drag it between the actual motion sensor and the delay time just to make my life a bit easier taking with the delay timer and i'm going to connect these two together it doesn't really matter what's what we're using here we've got at the top we've got an original payload and an additional payload may as well just stick to the original one because we're not really sending this any information it's just a basic node anywhere where we're going to tell the light to turn off no matter what information we send this node it's still going to result in the light being turned off i'm going to change this over to minutes instead of seconds now what i'm also going to do is i'm going to take this output here and i'm going to join it to here then i'm going to change the settings of this node now we don't actually need anything other than to send this a stop message so i'm going to delete the message the data part and i'm going to change the payload message to a string which contains the word stop then click done so the idea now is that if motion is detected and it's dark turn the light on if motion stops being detected we start a five minute timer then we turn the light off now if within that five minute timer um starting motion gets detected again this time what's going to happen is the messenger will get sent to the node here where we're checking the light level now the thing is the light got turned on meaning we no longer meet that criteria the threshold that we'd set for the room being dark is now being breached effectively because it's now lighter in the room in which case we don't satisfy uh the rule that we've got in here where we're seeing is that is you know is the um the light level less than 111 it's it's above that in which case we're gonna get an output out here and because we've specifically got the word stop in our payload that's gonna result in a timer actually stopping so that's a useful note to put in the only thing to point out is that i'm having to change a test environment here because this won't work the way i had it set up before it was convenient having a motion sensor right next to me because i could trigger it and play with it and make made life easier as part of the video the only trouble is because that's got the light sensor in it i need that in the same room as the actual light otherwise this won't work i mean i'm surrounded here by quite a lot of recording lights and what i need to do is to get that light sensor to actually detect the change in the light level initially it's going to be dark in which case it's going to satisfy the first criteria to turn the light on but i need this light then to be detected the light level to then go up which then results in additional motion resulting in the output going out here because the light level is now higher than the threshold so one thing i do have to do because of that i need to go back and check our light level so at the moment it's showing zero because it's in total darkness the light uh the curtains are closed see there's just no light in that room at all if i go back to node red i'm going to change my threshold because originally it was set at 111 i need to change this to something much much lower so i'm just going to set it for five that's should be enough so when this light gets turned on we should end up with the light level being above five in which case next time around when we you know do some motion within that room it doesn't you know that the actual light level is above five in which case it doesn't satisfy that requirement that we've got so what i need to do now is i need to go off in this other room to get the motion triggered and then we'll start this whole process going well i've now triggered motion so it's showing the timer of 7 28 so the light is now turned on go back to the um dashboard here you can see the lights turned on and light level is now showing 191 because the lights um obviously making that room a lot brighter than it was before if you come back to node red it should have by now timed out and it should have started there so when i've got a bit of an output down here saying that the timer is actually running now if i don't do anything this time is actually going to run out and it's going to result in the actual light getting turned off so what i'm now going to actually do is i'm going to go back to that room it'll trigger the motion again and it should result in that timer actually stopping well sure enough i went back to the room triggered the motion so it's showing 7.29 now you can see we didn't pass the check which was is it dark in that room uh the illuminance level is 187 which is higher than the five so it sent a message down to our timer here to stop i just saw it started again running i'll just i didn't quite catch it in time so i'm just going to go back again re-trigger that motion sensor yeah so as you can see the timer in is actually showing us stopped because by the time i explained this part in this part the actual timer on here and it reset in which case the time had started all over again so that's the benefit of this is that if motion is detected and it's dark it turns the light on as long as somebody's still in the room and still moving around as long as you know you don't have a period longer than this timer any further motion is going to result in keeping that light turned on and then eventually when motion stops because typically somebody's actually left the room you're going to end up with this timer then runs and then turns the light off so that to me is a much more sophisticated way a better way of using that delay node i mean the delano is very very useful uh definitely useful for other sort of rules that are run but i find this one even better to be honest because unless i actually wanted to stop it's just going to act as a basic timer anyway so i tend to use this a lot more than the actual delay timer but definitely a useful note to know about now one node that comes by default with node-red which i find extremely useful for testing is the inject node so we're going to go to here where it says filter nodes i'm just going to start typing then i'm going to drag the actual inject node itself over into the flow and then i'm actually going to take the output and i'm going to connect it into our node here where we're testing to see if the actual room is dark enough i'm going to click on deploy because basically what what i can now do is rather than actually waiting for an event to take place so that could be a timed event for instance in this case it's a motion event rather than actually waiting for that event to take place i can actually send information to this node in other words tap into this part of the rule and bypass the actual event and then i can actually trigger the rule without needing that event to take place so it's extremely useful especially when you've got more complicated rules where you don't want a wait around for a certain time to occur or in this case you don't have to keep going into the room to actually trigger motion and so on so now that that node's actually connected up here essentially when i click this button it's just going to send information to this node here and then that will start the rule the reason that would work is because this particular node here all it does it just checks to see if motion's taking place if motion is detected it sends some information over to this node which in turn it does its own check to see if the room's dark enough it checks to see what the light level is and if it's like below a certain threshold we then get information sent over to this node to turn the light on if on your hand it's above a certain threshold information gets sent down to this node to stop the timer so it doesn't really matter what information actually gets sent to this node the result is going to be the same all it's going to do is as soon as it receives information is check is the light um below this threshold that we've sent so in this case it doesn't matter what information i'm sending to it so when i click that button you see there it's basically gone and done its check it's saying yes the room is dark essentially and it's turned the light on so if i go back to my overview dashboard here you can see the lights now turned on so that's definitely a useful way to be able to test things if your rules aren't actually working for some strange reason rather than having to actually wait for an event to take place to be able to run your troubleshooting and find out where it's broken use this timestamp um injection node as it is because that's where it's set to by default now if we go into here what it's essentially doing is it's just sending us an actual timestamp across and again that the information itself doesn't really matter but this is extremely useful r5 for testing now this next node that we get by default is extremely useful but it doesn't actually do anything so i'm going to go to where it says filter nodes and i'm just going to start type out because what i'm interested in is this comment node i'm going to drag that into my flow because i like to keep a comment about every rule because when i create a flow chances are i'm going to have a lot of rules in here i mean i could go to the actual tab for the flow and i can keep information here within the description but i find it's easy to keep an actual comment about all of the actual individual rules so each rule i create will get a comment of some sort so what i can do is double click in there and then i can start typing information so for example if motion is detected and it's dark turn on light and then i can just put some more information in there i've still got to click the deploy button i mean technically we haven't changed anything as far as the rule is concerned but any change within this flow will trigger that um deploy button up there to change its state but the reason i do that is just because i've worked in it for a long long time and documentation is really essential because chances are you'll do something like this you'll create a rule you'll come back at a later date and you'll be wondering how it works or maybe it's a case of something stopped working and you you need to understand what it is you were trying to actually achieve in the first place to be able to work out why it's no longer working or maybe it's a case if you want to make a slight adjustment and again you still need to understand how does this actually work what does it actually do and that's where that common node comes in extremely useful now it's common to set up rules that are going to take place at a certain time of the day whether it's a specific time or sunrise sunset for example we need to know that's actually going to trigger at that certain time over the day in order to start a rule so for example maybe we want to actually turn the outside lights on at around sunset and we want to turn them off when we get the sun rise so there is a node that's actually built in which is very very flexible it's called big timer so i've got filter nodes i'm just going to start typing and then we're going to drag this big timer node into the flow double click on that i'm just going to call this one sunset slash sunrise but you can call it whatever you like now there's actually two timers here because bigtimer is basically just an on off switch that's all it is and there's a lot of flexibility in here now in this case what we're interested in doing is to actually turn the lights on when we get to sunset so i'm going to select from the drop down menu rather than a specific time i'm going to select sunset and then for the off time we'll be selecting sunrise we've got a second timer there which we're not interested in we're just interested in those two specific times of the day and then we've got offsets for these as well so for example we could actually have lights turn on maybe 10 minutes before we get the sunset we could have them turn off 10 minutes after sunrise so it's very flexible now the sake of this example i need to keep these specific um because i need to actually demonstrate it actually working now blowing down when you're dealing with sunrise and sunset you actually have to tell us where you are basically in the world it needs the latitude and the longitude settings one way to get these is from google maps that's a good place to look so for example just for giggles we're going to go to this world of astoria in new york it happens to be closed at the moment but if you select an actual location on the map and then right click it it actually comes up with the actual coordinates here so if i right click them again i'll click the measure and say with the left mouse button this time what i can do is i can actually take a copy of those coordinates so i'm going to copy these and that's going to be my latitude my longitude so you have to get these right for wherever it is you've actually got home assistance set up otherwise this isn't going to work you'll you'll find your lights turning on at a completely wrong time so these have to be accurate so we've got our information uh exactly where we are in the world now it's a bit confusing i must admit where it's referencing the on and the off message do with mqtt this has got nothing to do with that but i do actually need to put in some information here for what the actual messages are so in our case we want on and off we'll do the same the actual text now you can change the timeout if you like but what i'm more interested in i mean it is very very flexible this thing i mean you've got um calendar options here very specific days uh that you can set this up for but we're just running a basic timer for sunrise and sun sunset so i'll keep it simple now i don't necessarily need the output repeating all of the time there's no point constantly sending a message out to say turn the light on turn the light on turn the light on there's no need for that so i'm going to disable this option to repeat the the output i'm going to do what the output at startup in other words if if home assistant gets restarted for example i do want to know what the state is at the time so for things like power cuts and so on uh he would want the lights turning off again uh for instance if we've ruled over into a time where like in this case we're past sunrise in which case we want lights turning off but for whatever reason home assistant you know fell over at some point and it never got that message out but i'm going to click on done and that's set so big timer now it does get a bit more complicated understanding and what i'm going to do is i'm actually going to introduce an actual switch in here because i need to there's only basically there's that one output at the top there's multiple outputs but the one we're interested is the one at the top and it's i need to be very specific as to which message we actually send to actually turn the light on and off so if we go down to this option here which is switch i'm gonna grab that and i'm gonna connect the connection from big timer over to the switch that we've got and this is just going to be an on off switch and then what we're going to have is one one output's going to be on and i know that output's going to be off and then that gives me basically a switch where we've got an on and off so when we get a message on it goes out that port when we get a message of off it goes out backward now i must admit i can i can make this a whole lot easier but i'm not going to get in like in the details of messing around with message payloads but for the sake of this what we're going to do is i'm just going to take these two nodes that we've already just defined here because this is to me this is a separate rule i mean i could have taken the output and connected up to there if i want but i want to keep it separate it's a completely separate rule as far as i'm concerned so we join these two together so the idea is that when big timer is on it'll send a message and then this one will then send a message out the out early output port which is set for on and if it gets a message of off it'll send it out output and that output port so turns the light off the only catch is as you can see there because we just started and we just deployed the rule big timer is now set to off because it's obviously past sunrise in new york at the minute so it sent a message out the output port here to um to turn the light off and that was often in the first place but that's something something to bear in mind it's another reason why i was talking earlier about this idea of having actual separate flows and having this set to deploy only you know flows that have actually changed because otherwise you'll be getting these sort of settings happening which may not necessarily be what you want but in this case i've got a bit of a problem here and that i've got a timer and it's currently off and i don't want to wait until you know sunset to actually find out if this is actually going to work or not so what i'm going to do is i'm going to i'm actually gonna copy the actual inject node that i've got because i can actually override this if you look there there's a little a box there where it says optional override so i'm gonna double click on that node and i'm actually going to change this to be a string of on so i'll call that one on i'll connect that to there we'll call this one off and we'll set this to a string which sends off or click done and then we'll connect that to there because like i said i don't want to really actually wait for sunrise and sunset i want to test this out myself so what should now happen is if i click this one it'll send an on message to big timer and big timer will turn on in which case we should get our light turning on whereas when i click this one it should send enough setting to big timer it should then result in light being turned off so i'm going to click on that one and there we go so if we go over to our overview page our light is now turned on come back to node red now what's actually going to happen is this is actually going to time out at some point if i don't do anything it's going to turn off anyway because we're still in sunrise this is a this is essentially kind of like a timer clever timer that's running all the time i've just overridden it temporarily and it's going to time out and then turn itself off because it's it's past sunrise in new york it's actually daytime but what i can do is i can actually force it to turn off by clicking this option so that's turned off since it's now sent a message over to the off light so i'll go to my overview and see the lights now turned off so i've gone back to node red so obviously i wouldn't leave those in permanently this is just all part of the testing having these inject nodes but the idea is that what will happen now is going forward is that when we actually get to sunset big timer will turn on in which case it sends an on message to our on off switch that we've got here that in turn then sends a message out to turn the light on then obviously when we get to sunrise it'll be sending an off message to the switch and the switch will then send that out to that node there to turn the light off so it's it's very flexible i must admit i mean there's there's just a lot of things you can do in here and as i said the only thing i'll point out is the fact that i just don't want that continuous message being sent out all the time um when it comes to all of these smart home devices that you've got you're going to have a lot of them it doesn't really pay to have a device constantly you know repeating itself saying i'm on i'm on i'm on it's just going to drown the network as far as i'm concerned so i only want it to turn on in this case it's sunset and turn off at sunrise just keep it simple the only the only alternative or other option i would say is that if home assistant were to restart then obviously that would then um send a new message out when home assistant starts from scratch but other than that um that's once you get your head around it it's a bit it's a bit confused i must admit this idea of the um you on message and the off message because it's it actually kind of gives you the impression it's all to do with mq18 and it's not but yeah pretty easy to set up once you know how uh but that's one way to set up your sunrise and sunset options now by default node-red does actually come supplied with a lot of nodes but chances are in your travels on the internet you might actually come across people who have been setting up automation rules and you want to actually use those yourself but you find that the node um that they're actually using doesn't get mentioned in here you can't find it and that's because you've actually got to install the node and there are a lot of nodes that you can actually download and install so to install node we want to go up to the menu option up here different ways we can go and go in through settings but we can just select here manage palette and that'll take us to the same page and then here what we've got is this is a list of all the nodes that are already installed but if you click on the install tab there's so many of them that it's not actually even showing you one but if you know what the actual node is you can just type in the name so for example easy time is a quite a useful one i find for time scheduling so i'm just going to type in easy time and that's the node that i'm interested in so i'm going to click on install it mentions here about the actual documentation um how might need a restart but i'm just going to click on install now and it'll install the actual node itself it doesn't take that long it just downloads the actual software then it installs it i'll click on close if not go back to our filter nodes just type in easy there's my easy timer so i can now start using this timer it's it's pretty much just the same as kind of big time it just doesn't have all those fancy details of like calendar events and so on so you can still do sunrise sun sets so if you want to keep things simple you can just use this is an alternative but as you can see it's pretty easy to install additional nodes as long as you know what the node is you should be able to find it just by searching for it well thanks for making it to the end of this video i really do hope you found it useful if so then do click the like button and share as that will help get the video out to more people who might find it useful as well if you've got any comments or suggestions please post those in the comments section below and if you're new to the channel and you'd like to see more content like this then yes do subscribe just remember to set the bell icon to actually send your notifications when new content gets released although i also post to twitter as well as facebook if you'd like to help channel and support it you can actually make contributions through paypal and buy me a coffee i've also got links to patreon and there's also the join membership option for youtube itself patreon and youtube members do have the option to actually benefit from early access as well but above all many thanks for watching this video i'll see you in the next one [Music] you
Info
Channel: Tech Tutorials - David McKone
Views: 54,385
Rating: undefined out of 5
Keywords: home assistant node red, home assistant node red examples, home assistant node red install, home assistant node red motion sensor, home assistant node red automation, home assistant node red entity, node-red home assistant, node-red install, node red tutorial, node red home assistant, node red home assistant tutorial, node red home assistant install, node red home assistant configuration, node red home assistant examples, node red, node-red tutorial, home automation
Id: QLAe4H72Aq8
Channel Id: undefined
Length: 74min 31sec (4471 seconds)
Published: Sat Jun 04 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.