Home » Blog » How to create Action Template Sensors in Home Assistant

How to create Action Template Sensors in Home Assistant

Hi! I am really excited about the brand new Action parameter that you can use in Trigger templates in Home Assistant. Now you can create an Action Template Sensor and this brings endless new possibilities to templates in Home Assistant. Let me show you how you can implement it yourself!

⭐⭐⭐ NOTE: ⭐⭐⭐

This article accompanies a YouTube video. I wrote it for people who would rather read than watch a video. To keep doing this, I would like to ask you to 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!


Click here to watch the video

Introduction of what an Action Template Sensor is

Home Assistant is developing more and more to be more user-friendly for not-so-technical users. I think this is a good thing so that Home Assistant will increasingly be used by a wider audience. But, every now and then, the Home Assistant team decides to add functionality that aims at more advanced users. They just did that in version 2023.9 of Home Assistant and this one is HUGE! You can now use an action parameter in a trigger template to create a custom sensor.

So, what does that mean? Basically, you can use actions that you are used to using in scripts and automations in a template sensor now too. This means that you can call a service in a template, store the resulting value of that service in a variable, and use that value as the sensor value. Or, use that value to calculate a new sensor value.

Using the Weather Forecast service in a Template in Home Assistant

For example, Home Assistant introduced a new service weather.get_forecast to retrieve the forecast for the next couple of hours. In the past, the forecast was an attribute of the weather entity, but this attribute will disappear in the future. Now, in my case, I used that attribute for one of my heaviest automations to water my garden automatically based on weather conditions, temperatures, and precipitation. If you want to know how I’ve set that up, then please check this video where I explain how you can create your own full automatic garden watering system! I also changed the download code for that video so that it’s compatible with Home Assistant 2023.9 and up.

I cannot use the forecast attribute anymore in the future, but luckily I can use the action parameter now! You can check the results of the weather.get_forecast service in the developer tools by going to the developer tools, clicking the Services tab, and selecting the weather.get_forecast service in the service field. Now select your weather service. In my case that is OpenweatherMap. Then select the Forecast Type. Openweathermap doesn’t seem to have a Daily forecast type, so I select Hourly here. Now click Call Service.

As a result, you will get a list with the forecast for every three hours. It contains various values for the condition, precipitation, temperature, etcetera.

I want to know what the total rain forecast for the next 24 hours is, so I am going to use the precipitation and the precipitation_probability in the example of this video. Each forecast takes 3 hours, so I will have to calculate the sum of the next 8 forecast entries because 3 times 8 is 24.

Home Assistant also introduced the Template Helper in 2023.9, but you cannot use this Helper to implement a Trigger Template yet. So, we have to create some YAML in a separate file. To edit a file in HJome Assistant, you can install either the File Editor or the Studio Code Server Add-On.

How to install Studio Code Server

To install Studio Code Server,

  • go to Settings, Add-Ons, and click Add-On Store.
  • Search for Studio Code Server.
  • Click Studio Code Server.
  • Click Install.
  • Turn on the Show in Sidebar toggle and click Start.
  • Now open Studio Code Server by clicking on the Studio Code Server link in the left menu.

You now see all the files on your Home Assistant Server here. I put all my templates in one file, so I created a file which is called templates.yaml. You can create a new file by clicking on the “new file” icon and adding a templates.yaml file. After that, you need to point your configuration.yaml to this templates.yaml file. You can do this by opening your configuration.yaml file and adding the following line:

templates: !include templates.yaml

Now reboot Home Assistant by going to the developer tools. Click on the YAML tab and click on Select Configuration to check if you didn’t make any mistakes. If you get a green message, you can click on Restart to restart Home Assistant.

Adding the code for the Trigger Action Template Sensor

Go back to Studio Code Server after Home Assistant is restarted.

  • Open your templates.yaml file
  • and add the following code.

You can type this code from the screen, or, you can download this code for the price of a coffee using the download link in the description below this video. This will save you time and frustration plus you sponsor me so that I can keep creating these videos for you.

What does the code for the Action Template Sensor do?

So, what does this code do?

I first define a trigger template. Within this trigger template, I will eventually define a sensor that holds the value of the total expected rain forecast. Within the trigger, I am going to define when this template should trigger, just like how automations work. In my case, I want to trigger this template every hour.

Now, when the template triggers, I want an action to take place. This works the same as how it works in automations too. The action is a service called weather.get_forecast. The target is my OpenWeatherMap entity which I also used in the developer tools a couple of minutes ago. In data, I am going to say that I want to get the hourly values. And now, the important part starts. I will store the outcome of this service call in a variable that I call my_forecast. So, the list that you saw in the developer tools before is now stored in the variable my_forecast.

Then, I am going to define the sensor.

  • I start with defining the name. This name will also be the name of the sensor in Home Assistant.
  • Next, I defined a unique_id which makes it possible to edit some things like the area through the front-end of Home Assistant. You can create your own unique_id by clicking the right mouse button and clicking Generate UUID at Cursor.
  • I also defined an icon here and set the unit of measurement to millimeters.
  • Now, we are going to define the state. I want to show the total rainfall for the next 24 hours and incorporate the chance of rainfall in it by using the precipitation and precipitation probability values.
  • I start by defining a namespace. This namespace will hold the final value for the sensor.
  • Within the namespace, I define the total precipitation variable and initially set it to 0.
  • Now, we want to loop through the forecast blocks of three hours, so I define a for loop that loops 8 times through the dayparts that each take 3 hours.
  • Then I retrieve the precipitation for the daypart in the list that I stored in the my_forecast variable.
  • Next, I retrieve the precipitation probability for the same daypart and divide that by 100.
  • If the probability is higher than zero, I multiply the precipitation by the probability to get a sort of the best value for the actual precipitation. I know, this might not always be accurate, but I have used this for over three years now and it turned out to be the best result that I could get.
  • And after that, I add the calculated value to the total precipitation.
  • When all the dayparts are processed in the for loop, I return the total precipitation and round it to 2 decimals.
  • Save the templates.yaml file using control or command-s or using the menu of Studio Code Server.

So, I created a new template sensor that returns the total expected precipitation for the next 24 hours.

Testing the Action Template Sensor

Let’s activate this sensor.

  • Go to the developer tools and click on the YAML tab.
  • Scroll down and click on Template Entities. If everything goes well, you will see a green checkmark.
  • Now go to the States tab.
  • Search for the sensor.sprinkler_total_expected_precipitation sensor and see what value it returns. Be aware that the template is set to trigger every hour, so it will take up to a maximum of 1 hour before the sensor will contain a value.

As you can see, this new functionality is really powerful! I used the weather forecast service as an example for this video, but I hope you can see all the new possibilities for other services as well. Let me know in the comments what cool ideas you have now you’ve seen this video. I might create a video about your idea too!

Like I said before, you can type the code from the screen which is absolutely free, or you can save some time and frustration by downloading the code for the price of a coffee. This also helps me to continue my work to create tutorials for you.

Thanks for watching this Home Assistant Tutorial. If my videos help you and save you time, please consider becoming one of my patrons just like these wonderful people did. You’ll find links to Patreon, Ko-Fi, and how to join my channel as a member in the description below this video. And, as always, please do not forget to give this video a thumbs up and subscribe to my channel if you haven’t already!

I’ll see you soon in my next video! Bye Bye!

home assistant, template, yaml, automations, smart home, sensors, develop


Back to all articles