5 MUST HAVE SCRIPTS for Home Assistant (Coding Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
every home assistant user needs to master scripts they can massively save you time and simplify your automations i'm not only going to give you the code in this video but i'm also going to show you step by step of how to actually adapt it for your own needs stick to the end of the video because i'm going to give you a cool yaml coding tutorial i'm going to show you how you can fade lights and how you can build this script yourself this video is possible thanks to the awesome subscribers that i've actually voted for this video in the recent survey that i made on my channel to participate in the next survey you just need to subscribe to the channel now let's roll the info [Music] a script is a series of actions all the actions that you can do on an automation you can recreate them in a script if you go to your configuration and click on scripts you can see the list of all of your scripts you can add a script through the ui you can also go to scripts.yaml and code it directly from there the first script i'm going to show you today is the notify all the reason why you actually need this script is that every time you want to notify a device and you want to notify a second device or a third device you need to do that and it's a repetitive action in each of your automations what you can do is you can save time and use just one script let me show you how you can build it now i'm going to go into the yaml mode because it could be a lot easier for you to see and that's what you're going to be copying and pasting so copy all of the yaml code paste it into your scripts.yaml and let's change a few things under the sequence there are three things are happening in this block of code you can see them from line 10 to line 13 to 14 to 17 and 18 to 21. so i have three devices that i'm notifying and each device will be notified with the sign title and the same message title and message is defined here as fields so you can think them as like variables now if you have more than three devices do the following highlight from line 18 to line 21 copy paste and over here change your device to whatever your device is here you can have all of your devices in one place so if you have the renaming device you can just go in here and if you need to add a device or remove a device you can just do it here instead of doing it in all of your automations broadcast all so at the moment here i'm picking up a message and i'm sending this message to the kitchen speaker if i wanted to add more so what i could do is is i could call this tts carl and this corsair and i just need to replace this with the drum speaker any other speaker and now we have two speakers in the house playing back the same message the first script i'm going to show you today is alarm broadcast in here i'm using a repeat while repeat while is similar to the concept of a loop in programming to repeat is a cool way to say to a home assistant keep doing the same action but until some sort of thing happen in this example what i'm saying is while my alarm panel is in triggered mode then keep on repeating this message and then the moment that the alarm panel is no longer in trigger mode or any other condition that you want to put in then this will stop to actually get this done you go to the action type click repeat you'll have various options like count while and until i'm doing while for example here something while the state of the alarm panel is triggered and i've also got an escape loop so i've added a template repeat.index less than 50. well this means that this will only work only 50 times and you don't really want the alarm going on for hours and hours and i know you know your neighbors and what i'm doing is i'm using my previous script that i showed you in the video the broadcast all and i'm passing on a message and the message could be anything you want so all of our speakers will play the same message and they'll do it with a delay of 10 seconds so at the end of one message let's go to delay and then it goes again and it goes again until the conditions are met the fourth script today is flashing lights now you can flash lights for a lot of reasons let's say you want a flashlight again in case of an emergency so combined to the actual broadcast message we also want a flashlight we're using a similar concept same repeat and the same while with the alarm panel as triggered what i'm doing is specifically is i'm turning a switch on because i have a smart bulb tied to a switch so i turn the switch on first and then i turn on all of the colored lights to red so that will give us a nice emergency signal brightness 255 and then after that i toggle the light so toggling the light will obviously turn it off and then it will turn it back on again and it will keep looping up until the wire conditions are met like before fifth must-have script that you need in your home assistant is party mode we all love to party come on right you know i know we haven't been having many parties last year but this year you know we got to have many more and i actually had a special occasion personally we had a gender reveal party at my house and that sort of sparked this script and i'll show you how i created it because of the theme of the party i'm actually going through blue to pink and that's what i'm deciding to do but you can do this in any way you want the smart thing that i used here is an input boolean the input boolean is on then the script will keep on running if i turn the input boolean off and i can do that through a physical button if i have an automation set up like that or i could do it from my mobile phone in home assistant and i just turn off the boolean once i've turned off the boolean then the script will stop working this actual script over here is pretty straightforward what it does is it just picks some light and it just changes some rgb colors to actually add rgb colors this took me a little bit of a while to figure out how to do it so top tip is to use these little dashes with the free rgb colors so don't do something like 255 color comma 109 and that's not going to work what you sort of need is some sort of format like this i'm having the delay set at one minute so it's not sort of in your face and people don't notice is flashing constantly but you could change this to anything you wanted now as promised we're gonna go into that deep dive and build that fading light script before we get on with it take a second to smash that like button and let me know in the comments scription down below which one of your favorite scripts and which one you're going to be using immediately go to your scripts.young you can either use visual studio code like i am or you can use file editor i always recommend visual studio code because it's going to be a lot easier all right so let's start now you can obviously reference the code in the blog and you can just basically copy and paste it the purpose of this cutting tutorial now is to actually show you how you can build it and how it actually works and how you go about building your own script without actually blindly looking at someone else's code let's just let's just go through this and try to do this together so pull up your editor and let's go now first thing we need to do is give it a name okay so you can call it fade lights that will be uh fine so you can see every single um script that i have starts with the script name if you have two words then put an underscore and then just put colon and you can go and we can start so it's giving us a an issue it's expecting an object which is fine because we haven't actually completed it all now we can put an alias in and the alias is going to be a description so we say fading lights now it's immediately prompting us we're missing property sequence which is fantastic but first we're going to specify a few variables so i'm going to create some fields and these fields are what we can actually pass the uh script so like the inputs of the script so each field needs a name so i'm calling this light and it actually as expects a description here we can put an example now this could be really useful and i'll show you the developer tools how we can pull the text from the example so you know when you do fill example in developer tools and you call the script and this is where this could become useful we're just going to do something like this so this is a name of a lamp that i always use in my tutorials so i'm happy with this now look at the indentation so you can see that description and example are properties of light and light is property of fields and alias and fields are probably of fade lights so you can see we're still missing the property sequence which we haven't done anything yet of that so so we'll get to that now now here's where people get confused where do you put sequence you put sequence here do you put it here loop it in here now as you can see the error messages of line 423 are faded lights so you really need to think of this needs to be a property of fade lights sequence isn't a property of the alias or fields so if you put sequence in you'll see incorrect type expected object which is fine because we haven't filled it in yet now if i try to indent this like this you can see the error message coming up missing property so we know we've indented it in the right position and we've got it like this and within the sequence i'm going to actually declare a few variables so i'm going to do in i'm going to double tap so we've got double space or a tab dash variables and here i'm going to set some variables i'm going to do double tap again light underscore entity and in the light entity i'm going to actually do something a bit cool with some templating here i'm saying light dot so every light we know starts with a light dot okay so we have light dot and then we need to read the input from the field called light and it's slightly confusing you know but we're gonna get to it so with gingka we open two curly brackets and then we just do light cool so now this light will be populated for example with imac lamp in example with any light that you pass to the script so you can use one script to basically fade any light this next step we're going to be setting our input number two two five five so our input number is what we're using to help us store the brightness value now the input number something that you're going to need to be creating before you doing the script yourself there could be other ways of doing this without input numbers um and let me know in the comment section down below if you've got a better script than what i'm building over here now here we're going to be using a service the service that we're going to be using is input number so input underscore number set value and we need an entity id which is going to be our input number called brightness level and now we need to pass some data to the value so i'm going to do data and then again i'm going to go and indent so you can see it automatically does it for me and the actual value so how much what we're going to be sending the input number two is 255 which is maximum brightness which is what we're doing over here so we are starting with our light at maximum brightness and then we're going to fade it down to zero with the script the next block of code is the repeat so this could be something that we've seen in our other scripts but we're just going to go through any more detail let's define the while so the while is the condition that we're looking for another while dash condition so i'm going to be looking for a numeric state and i actually have some pre-filled it in for me a little bit of a shortcut so i can clean it up so the numeric state gives me entity id which is going to be the input number brightness level so what i'm saying is is that the level of the brightness has to be above zero so a line above changes to zero okay so we have so we will be repeating a sequence of action which you haven't defined yet i'm going to be repeating it while the numeric state of the brightness level the property numeric state of the brightness cell is above zero okay so it will obviously work the first time because the first time it's two five five all right so we just said it two five five so this condition will always work um at least in the first instance now where are we putting sequence it's the same question as before remember when we did it earlier so sequence the property of repeat so as while is the property of repeat sequence is probably a repeat and here is where we need to plus sequence and you can see the error message sort of went away now it's telling me that we're missing an object because we're not doing anything in our sequence okay so we have service i'm going to do light dot turn on so here we are turning on the light because we're passing the decreasing amount of brightness so we can set up target which is going to be our entity id now because this is dynamic we the entity will be the light entity we defined earlier so we can put that in the curly brackets and we can do something like this and we can put the quotes around it cool so we've got our entity id light entity now we need to add the brightness so data is going to be a property of target and on the data we have brightness and over here for example if you put it you would put like a value like 255 right some sort of hard-coded value but i'm going to show you how we can do something a little bit more dynamic okay so here we what we're saying is is to use the state of the brightness level and convert that to an integer value so at the beginning it will be 255 okay so we the first point of the of the sort of script will turn the light on to its maximum brightness and then we're going to start decreasing it before we decrease anything we need add a delay so you can do something like this delay and here you can set like hours minutes uh as you wish so i've second this to one second so the it will it basically will take 255 seconds to fade the light you can set this as you wish so we've got the delay at the end of the sequence we need to obviously reduce the amount of you know reduce the level of brightness in the input number so the next time it runs it will it will pick it up and we do that by doing input service and we're going to call it input number decrement entity id is going to be the same so it's going to be input number brightness level so first time it runs let's get set to 255 and then the second time it's going to be 254 253 and it's going to continue just give this a go let me know if this works for you and if you're struggling please put a comment down in the description below share your yaml now that i've saved it and actually got to go and reload my script go to configuration scroll down server controls check your configuration reload scripts you go to developer tools go to services now search for scripts find the fading lights whatever name you've called it you have this fill example data button so this will give you an example uh like changes to a light that you have in your system obviously and now if i call the service the light will come on and it will gradually go down so to demonstrate that actually so you can see in the brightness right here two three four it's just going down every second so we know that the script is working now i'm going to give you some homework before the next video are you able to modify your script and change it to actually increment lights so say example a wake up script instead of a going to bed sleep if you can actually get that done let me know please share your code in the description down below if you enjoyed this home system pro tip then watch this other video over here you're going to find 12 more home assistant tips to become a pro this is geo see you next time ciao
Info
Channel: Smart Home Makers
Views: 34,125
Rating: undefined out of 5
Keywords: home assistant, smart home, smart home makers, automation, home automation
Id: PhdAqc__8CA
Channel Id: undefined
Length: 16min 23sec (983 seconds)
Published: Mon Nov 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.