Today I’m going to focus on how you can create your own blueprints to easily create automations and scripts in Home Assistant. Let’s dive into this!
⭐⭐⭐ NOTE: ⭐⭐⭐
This article accompanies a YouTube video. I wrote it for people who would rather read than watch a video. In order to keep doing this, I would like to ask you to also check out the video, leave a comment under the video, give the video a thumbs up and subscribe to my YouTube channel. This means that the video is offered more often to new visitors so that they also stay informed of the latest Home Assistant tutorials.
Thank you for your support!
One of the powerful possibilities of Home Assistant is that you can create blueprints. Blueprints are YAML files that function as a template that can be used to generate an automation or script. This can come in very handy as soon as you want to create multiple automations that have the same functionality, but use different devices or entities. Next to that, you can share blueprints with others so that they can easily create automations and scripts using your blueprints. I am going to explain in this Home Assistant tutorial how you can create your own blueprints and how you can use them after you’ve created them.
Blueprint location in Home Assistant
So, how does this work? When you install Home Assistant, there are two blueprints installed already. You will see them as soon as you go to Settings-> Automations & Scenes and when you click create automation. Here you can select to create a new automation or create an automation based on a blueprint. You will see the two sample blueprints in this list.
But, there is another way to do this and that is by going to the Blueprints tab. Here you see all the blueprints that are available in your system. You see that blueprints for automations and scripts are available. When you click on the three dots next to a blueprint, you can create an automation or a script based on this blueprint. If your blueprint contains a source URL, you can share this blueprint with other people using the Share blueprint option. I will focus on creating a blueprint for an automation in this tutorial, but it works exactly the same for a script.
So, I created some blueprints myself for the Ulanzi Desktop clock. If you click on Create automation for the Awtric Create Notification blueprint, you see a lot of options that you can choose from. There are a lot of different selectors that you can choose like a dropdown, a color selector, and a toggle. The complete list of selectors can be found on this page.
For this tutorial I am going to create a blueprint for the following use case:
“Turn lights on and off on motion when the lux value is below a certain threshold”.
Start with creating an automation
You can start writing a blueprint from scratch, but it’s way easier to first create an automation and generate your Blueprint based on this automation. I created this automation for this tutorial:
Define the triggers
I start with two triggers. The first one is a state trigger that checks if motion is detected from Clear to Detected. If you look at the YAML of this trigger, you see that it checks from “off” to “on” which is the same as checking from Clear to Detected. I named the trigger ID for this trigger “on”. If you don’t know what trigger IDs are, then please watch this video where I explain how trigger IDs work.
The second trigger checks if motion is stopped for 10 seconds and I named the trigger ID “off”.
Define the actions
I defined an If/Then action in the actions section. Within the IF action, I check if the trigger with Trigger ID “on” is triggered and I check if the lux value of my motion sensor is below a certain value so that this action will only be called when it’s dark enough for me.
Within Then I turn on a certain light in a specific color using the light_turn_on service. You do not have to apply a color to the light if you don’t want to, but I want to show you in this tutorial how you can work with an RGB selector in the blueprint.
Within Else, I just turn off the light. You might ask yourself why I choose to use the services light.turn_on and light.turn_off and why I don’t just choose to turn on a device. Well, if you use the light.turn_on and light.turn_off services, you have two advantages:
- The first advantage is that you can point to an entity_id instead of a device_id. If you ever have to replace your lightbulb, that new lightbulb will have a new device_id, and in that case, you have to go through all your automations to select the new lightbulb device. If you use the entity ID, you just have to assign that entity_id to the new lightbulb and all your automations will just keep working.
- The second advantage is that you get a lot more options that you can set for your lightbulb, like brightness, color, color temperature, etcetera.
So, using the call service action is most of the time a better choice than using the Device action.
Copy the YAML of the Automation
Now that the automation is created, we can copy the YAML of this automation to use it as the source for our blueprint. For this, go to the three dots in the upper right corner and click on Edit in YAML. Select the YAML code using control or command A and copy the code to your clipboard using control or command C. We will use this code in the blueprint that we are going to create in a minute.
To edit blueprints, you need a text editor like studio code server or file editor.
How to install Studio Code Server in Home Assistant
- You can install Studio Code Server by going to Settings->Add-ons.
- Click on Add-on store.
- Search for Studio Code Server and install studio code server.
- Make sure that Show in sidebar is selected and start the add-on.
Now, let’s click on the Studio Code Server link to open the editor. You see all the files on your Home Assistant server here.
We are going to create a new blueprint in the Blueprints folder. There are two folders within the blueprint folder. The automation folder contains all the blueprints for automations. The script folder contains all the blueprints for the scripts. Let’s open the automation folder. I like to create a folder for my own blueprints, so I created a folder smarthomejunkie. Let’s select this folder and create a new file in this folder by clicking on the new file icon. I’ll give this file the name toggle_light_on_motion_and_lux.yaml.
Now paste the automation code that we copied to our clipboard using control or command-V.
How to convert Automation code to a blueprint
We have to convert this automation code to a blueprint. Therefore we start with the line
Then, we will give our blueprint a name and description. The name will be shown in the list of blueprints when we create an automation. This blueprint is an automation blueprint, so we will give it the domain: automation. I am the author of this blueprint, so I will add myself as the author of the blueprint. These were the general settings of the blueprint. You can also add a source_url where the blueprint can be found on GitHub if you like, but it’s not mandatory.
Defining the inputs
Now the fun starts, because we are going to define the fields that the user can choose in our blueprint. We want the user to choose the following devices and settings:
- The Light Entity
- The light color
- The Motion Sensor
- The Lux Sensor
- The maximum lux value within which the light may switch on.
- And the Wait Time before the light turns off after no motion is detected.
For each of these inputs, we are going to add an entry to the blueprint. We start with the line
Creating the inputs
Then we we are defining the light entity. We name the light entity input: light_target. The light entity has a name and we want to let the user select a light entity, so we define a selector of type entity. To make sure that the user only can select light entities, we add a domain filter of type light. You can add multiple domains here if you like.
Next, we want to select a light color. So, we add a new input with the identifier rgb_color. Its name is Lightbulb Color. And, we want the user to select a color, so we are defining a color_rgb selector here. The default value will be white, which is 255, 255, 255.
The motion sensor input looks a lot like the light entry that we defined earlier. The difference is that we filter on the domain binary sensor and the device class motion.
The lux sensor input looks a lot like the motion sensor input. Here we filter on the domain sensor and the device_class illuminance. The lux value is a value input. You don’t really need to use a selector for such a value. I set the default to 50.
And the wait_time input uses the number selector with the mode slider which will make sure that a slider is shown between the minimum and maximum value. The unit of measurement is seconds and I set the default to 120 seconds.
The mode of the automation is single. This can also be restart, queued, or parallel, but single is fine for me. There are multiple roads to Rome on how to create this type of automation.
Rebuild the triggers
Now we are going to rebuild the triggers of our automation so that the blueprint knows what to do with it. All we have to do is replace the entities and values that we use in our triggers with the input fields that we defined above. So, for the first trigger, we replace the binary sensor with !input motion_entity.
For the second trigger, we replace the binary sensor with !input motion_entity as well. And, we replace the “seconds” value with our !input wait_time.
We do not have conditions, so we can leave that code as is and hop over to the actions. In the actions code, we will replace all our entities and values with the input fields that we defined above too. Within the If condition, we replace the entity_id for the lux sensor with !input lux_entity. And, we replace the value 400 with !input lux_value.
Within the Then part, we replace the color code with !input rgb_color, and the target entity id with !input light_target.
And finally, in the Else part, we replace the entity_id with !input light_target as well.
This way, our blueprint is not pointing to hardcoded entities and values anymore, but will make use of the entities and values that the user selects when he or she creates an automation using this blueprint.
Now, let’s save this blueprint and activate it in Home Assistant. The next step isn’t really needed if there are no automations created with the blueprint yet, but it’s good practice to do this every time after you changed your blueprint to make sure that all automations that were created with this blueprint are synced to the new blueprint. For this, go to the Developer Tools, and click on the Automations link in the YAML tab.
How to use a Blueprint in Home Assistant to create an automation
Now, let’s test if the blueprint works! Go to Settings->Automations & Scenes and click on the Blueprints tab. If everything went well, you will see your new blueprint in this list. If your blueprint contains an error, the error will be shown here as well. I must say, that not all errors are shown here all of the time, so sometimes you might have to debug your blueprint if it does not work.
Click on the three dots next to the blueprint and select “Create Automation”. Now, fill in all the fields and save the automation. Your automation should now work and your light will turn on and off based on motion when the lux value is below the value that you’ve entered.
This is a simple example of how a blueprint works, but you can do much more using variables and selectors. You can type the code of this blueprint for free from the screen, or buy me a coffee and get this code in return. See the links to the code, the blueprint possibilities, and selectors in the description below this video.
If my work saves you time, please consider becoming one of my patrons, just like these awesome people did. You play a vital role in making it possible for me to create these Home Assistant tutorials. And please, give this video a thumbs up and subscribe to my channel to help the channel grow.
I’ll see you soon in my next video. Bye Bye!
Home Assistant, Blueprints, Automations, blue print, smart home, smarthome