Home » Blog » Mastering Scripts in Home Assistant: A Comprehensive Guide

Mastering Scripts in Home Assistant: A Comprehensive Guide

Welcome to my comprehensive guide on mastering scripts in Home Assistant! Are you tired of repetitive tasks in your smart home? With Home Assistant, you can take control and automate your devices with ease using scripts. Let’s do 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!

Ed

Click here to watch the video

Introduction

Some of you asked me why you should use scripts in Home Assistant because you think you can do everything with automations. Well, scripts are a powerful tool that allows you to combine multiple actions into a single command. Imagine turning off all your lights, locking your doors, and adjusting the thermostat with just a tap. But I hear you thinking… I can do this with automations too…

Yes, that’s true, but what if you have multiple automations and they all contain the same task, like sending a message to your phone, resetting some helper values, or turning on specific lights and switches at the same time? Let’s say, you want to send a message to your phone when an automation gets triggered. For instance, you get a message as soon as mail has been delivered to your letterbox.

By the way, I made a video about that scenario a while ago, so if you want to make that, check out this video. Anyway, you want to send a message to your phone when a certain event occurs. You can do that in an automation and that will definitely work. Now, let’s say you have 20 automations that send messages to your phone. If you get a new phone, you have to go through all these automations to let them point to your new phone. If you send a message using a script, you can call that single script in all your 20 automations and you only have to point that one script to your new phone.

This is just a simple example, but I hope you get the idea that a script can be a great solution if you have repetitive tasks that you want to perform in multiple automations. The best part is that you can send parameters to scripts so that your scripts can use these parameters in their actions.

I will show you some examples from simple to more advanced on how you can use scripts in Home Assistant. Let’s start with the simplest use case: “Create a script that sends a fixed message to your phone”.

Script 1: Send a fixed message to your phone

  • Go to Home Assistant and go to Settings.
  • Go to Automations & Scenes.
  • Click the Scripts Tab.
  • Click Add Script.
  • Select Create New Script.
  • Give your Script a Name, for instance, Send Message To Phone.
  • Click Add Action.
  • Select Call Service.
  • Select the notify service for your phone. You need to have the Home Assistant Companion App installed on your phone to be able to use this service.
  • Enter a message.
  • Enter a title.
  • The message and title will be fixed in this use case. In the next use case, we will make these configurable.
  • Click Save Script.
  • Test your script by going to the three dots in the upper right corner and selecting “Run Script”, The message and title should appear on your phone now.

Congratulations, you’ve created your first script. This is quite a simple script and maybe not so applicable for everyday use. You can call this script using an automation or a button on your dashboard. It might come in handy if you want to send a fixed message to your phone like “Beware, the windows are still open” when you leave your house. So, for every message we need to create a separate script and that’s, well, a bit inconvenient. It would be a lot more useful if we could create a script that accepts the title and message as parameters. This way we can send several different messages using only one script.

Script 2: Send a custom message to your phone

So, our next use case is: “Create a script that sends a custom message to your phone.” Let me show you how you can set this up.

Create the script
  • Open the script that we’ve just created.
  • Click on the three dots in the upper right corner.
  • and select Add fields.
  • A Fields section is now added to the script.
  • Enter a name for the first field. In our case, that’s “title”.
  • Enter a description. This description is visible in the automation that calls this script.
  • Toggle the required field so that the user has to enter a title before he can save the automation that calls this script.
  • Click Add Field.
  • Enter a name for the second field. In our case, that’s “message”.
  • Enter a description for the message field.
  • and, toggle the required field again.
  • We are now going to point to these fields in the action of this script.
  • Scroll down to the Sequence section.
  • Click on the three dots next to the notify service that we’ve created before.
  • Select Edit in YAML.
  • Replace the message with the following code:
message: "{{message}}"
  • replace the title with the following code:
title: "{{title}}"
  • click Save Script.
Create the automation that calls the script

The script is now created. To call this script, we need to create an automation that sends the values for the title and message to this script. You can trigger this automation using various states, like doors that are still open in your house, lights that are still on, the washing machine that is ready, etcetera. If you want to know how to do this, you can watch this video for example. For this tutorial, I’ve created a toggle helper called dummy. Make sure you’ve created a toggle helper with the name “Dummy” before you create the automation. As soon as I switch that helper, the automation gets triggered. But, you can use your own triggers instead.

Let’s set up the automation.

  • Go to Settings -> Automations and Scenes.
  • Click Create Automation.
  • Select Create New Automation.
  • First, define your trigger.
  • Click Add Trigger.
  • Select State.
  • In my case, I select the dummy helper that I’ve created for this tutorial.
  • And, in the “to” field I select “On”.
  • Now go to the Actions section.
  • Click “Add Action”.
  • Select Call Service.
  • In the service field, select the service script.notify_send_message_to_phone. This is the script that we’ve just created.
  • You’ll see the two fields that we’ve defined in the script.
  • Enter a title and a message.
  • Click Save and give your automation a meaningful name.

Let’s test the automation and script by flipping the Dummy helper.

  • Go to Settings -> Devices and Services.
  • Click the Helpers tab.
  • I search for the dummy helper that I’ve created before.
  • Click on the Dummy helper and turn it on.

The notification will now appear on your phone and the title and message that you’ve entered in the automation will show.

Now we know how we can utilize fields in scripts. This helps us to create flexible scripts that can be used in various automations or other scripts.

I need your help!

You will be doing me a huge favor if you subscribe to my channel if you haven’t already. And, you help me a lot if you also give this video a thumbs up and leave a comment. This way, YouTube will present this video to new people and that will make the channel grow! In the description of the video, you will also find information about how you can sponsor me so that I can continue to make these tutorials for you.

Thank you!

Working with response variables in scripts in Home Assistant.

There is one more thing we can do and that is use response variables in scripts. This is a more advanced feature of scripts. Scripts can be used to retrieve data from sensors and manipulate this data using template code. The result of this template code can be used in an automation that calls this script. Let me show you how you can do this with the following example. The use case is: Send a custom weather condition to my phone. It’s just a simple use case, but I hope you will get an idea of how to use response variables. Let’s first create the script that gets the weather condition and manipulates it.

Create the weather condition script
  • Go to Settings > Automations & Scenes.
  • Click the Scripts tab.
  • Click Add Script.
  • Select Create New Script.
  • Give your script a meaningful name.
  • Click Add Action.
  • We only want this script to calculate something, so we are going to add a stop action. This means that this script will stop immediately when it is called. But, we will make sure this script will also calculate our custom weather condition.
  • Click Add Action and select Stop.
  • Enter a reason for stopping. I will just enter “End” here.
  • Now, enter the name of the response variable that you want to use. This can be any name. It’s up to you what the variable name will be. For our purpose, I will name it weather underscore condition.
  • Now we are going to add the YAML template code that will generate our custom weather condition.
  • Click on the three dots in the upper right corner.
  • And, select Edit in Yaml.
  • We are going to add a variable.
  • Start with adding a line called variables:
  • Then enter your variable name. This should be the exact same name as the name that you used in the response_variable field. In our case, this is weather_condition followed by a piping symbol.
  • Next, we are going to create our template that will generate the custom weather condition.
  • First I create a variable temperature that stores the temperature that is provided by the openweathermap integration.
  • Then I am checking this temperature in a couple of if statements. Based on the temperature I fill the variable “conditions” with a value like “Cold”, “Chilly”, “Comfortable”, or “Warm”.
  • This is just basic templating. If you’d like to know more about how to create templates, then please watch this step-by-step tutorial where I explain how you can create your own templates. You can find the link in the description of the video.
  • Finally, we are going to return the generated value in this line. So, our response variable weather underscore condition gets filled with an index called “value” that contains the custom weather condition that is stored in the variable “condition”. The index name “value” is important to remember, because we are going to use that in the automation that we are going to create next.
Create the automation

So, we created a script that generates a custom weather condition and we still have the script that sends a custom message to our phone. We are going to use these two scripts in one automation now to send the custom weather condition to our phone.

  • Open the automation that we’ve created in the previous use case. This was the automation called Notify: Send a custom message to phone.
  • Click Add Action.
  • Select our script Weather: get custom weather condition.
  • Now enter a name for the response variable that you will get from the script. This can be any name. You can use the same name as the one that you’ve used in the script, but I am going to use the name “Result” here. What happens here is that the response variable from the script that contains our custom weather condition will be stored in the response variable with the name “Result” in my automation.
  • Now move this action above the Notify action by clicking on the three dots in the action section and clicking on Re-order.
  • Drag and drop this action above the Notify action.
  • Now open the notify action.
  • Change the title to a meaningful title. For instance Weather Condition.
  • Change the message to a meaningful message that also contains the response variable “value”.
  • So, I start with the text “The weather is” and then I am going to show the custom weather condition by pointing to the response variable “result”. Within the “result” response variable I am pointing to the index called “value”. Remember? We created the template in the custom weather condition script and called the index “value”. This is where we use that index name “value” again. It will be populated with the outcome of our template.
  • Click Save.
  • Go back to your Dummy helper and turn the helper on. The message on your phone will now show the custom weather condition that you’ve created in your script using a template.

I can imagine that this last example may make you dizzy, but I still hope that you see how powerful this can be if you want to start using scripts in Home Assistant. This is just a simple example, but there are so many ideas to use this. I’m curious to see what ideas you can come up with. Let me know your ideas in the comments.

Conclusion of using scripts in Home Assistant

So, why waste time on manual tasks? Let Home Assistant’s scripts do the work for you. Start automating and taking control of your smart home today!

Thanks for watching, and if my videos are valuable to you, please consider sponsoring me just like these amazing people do. You can really help keep this channel alive! Thank you. And don’t forget to give this video a thumbs up and subscribe to my channel for more tips and tricks on mastering Home Assistant.

I will see you soon in my next video!

Bye bye!


,

Back to all articles