Mastering Home Assistant Templates: A Beginner's Guide

2024 ж. 14 Мам.
43 692 Рет қаралды

Home Assistant Templates 101
Let's walk through everything you need to know to get started making templates. I explain the foundational blocks of templates, walk you through how to transform your home assistant data using math, and wrap it up giving to a look at some different ways you can use templates.
If you feel overwhelmed with #homeassistant templates, dont worry. After this video you will have everything you need to get started.
Code Examples:
Example Sensors:
github.com/thejeffreystone/ho...
Jinja Examples from Temaplte Panel:
gist.github.com/thejeffreysto...
Today Is / Month Is:
github.com/thejeffreystone/ho...
Jinja Resources:
Crash course for Beginners:
ultraconfig.com.au/blog/jinja...
Full Jinja Documentation (Use Search when needing syntax for functions):
jinja.palletsprojects.com/en/...
Video Chapters:
00:00 - Home Assistant Templates 101 - Intro
01:10 - Home Assistant Template Updates as of 2023.4
01:44 - What is Jinja?
03:07 - Template Basics: What do those Curly Braces mean?
06:57 - Home Assistant Entities in Templates
11:44 - Transform Home Assistant Entities using Templates
17:22 - Advance Templates - A Quick Look
23:00 - More to Come
Who am I?
My Name is Jeff. I am an DIY-er and Automator of boring things. And, of course, a huge Home Assistant fan. Oh, I also live in a house that tweets.
Follow Me:
Github - github.com/thejeffreystone
Twitter - / thejeffreystone / / slackerlabs
On the Web - slacker-labs.com/
Discord - / discord
And if you like this video you could always fuel my automation habit -
Grab SlackerLabs Merch: slackerlabsmerch.square.site
One Time Donation:
Paypal: www.paypal.com/paypalme/Slack...
Cash App: $slackerlabs
Buy me a coffee: www.buymeacoffee.com/icE6DeBut
.
Or use my Amazon affiliate link which provides a small commission on things you buy without additional cost to you - www.amazon.com/s/ref=as_li_ss...
Video Gear:
Canon m50: amzn.to/3ftldPH
Desk Mic: amzn.to/3wkum40
Camera Mic: amzn.to/3tXk1cp
Newer 660: amzn.to/3fwwejd
Dazzne D50: amzn.to/3oGBK5y
Audio Interface: amzn.to/3u07chK
Gimble: amzn.to/3ouKAoe
Music - Epidemic Sound: www.epidemicsound.com/referra...
Video Assets - LenoFx: i.refs.cc/CJXU6Da9?smile_ref=e...

Пікірлер
  • What parts of home assistant templates are giving you the most trouble?

    @SlackerLabs@SlackerLabs Жыл бұрын
    • What is giving me trouble is how can I use templating to send a custom notification message?

      @eugsmiley@eugsmiley Жыл бұрын
    • My problem was understanding how to use them, but I think this video got me started.

      @mrxmry3264@mrxmry3264 Жыл бұрын
    • Time calculations. working with datetime helpers, timers. working out percentage of time remaining. determining difference betweeen two input_Datetime helpers etc

      @ByStand3R@ByStand3R Жыл бұрын
    • @@ByStand3R this. Dealing with templates (or automations) that are predicated on prior events e.g. "if the door sensor was activated less than 30 seconds ago".

      @SBinVancouver@SBinVancouver Жыл бұрын
    • ​@@eugsmiley That's a pain but a really important use case. I still have to go back and reference code for that. Notifications can get really complicated with icons, actions, urls, persistence, priority, retries, sounds,............... Don't worry though! You don't need to include all those things! 😅

      @jmr@jmr Жыл бұрын
  • Dark mode may be good when you are on the box but in this video light mode would've been better I think. The change in colour helped but the original colours were difficult.

    @vicb2389@vicb2389 Жыл бұрын
  • Jeff, you are an awesome educator! I have struggled with templates for ages spending so much time getting my head around things in the template development panel testing and retesting until I understood why things didn't or by some fluke worked. In this one video you broke through 3 years of my lack of understanding of some very simple basic format rules eg: {{ }} {% %} and | int and also the >. I have to also admit that your Github pages are awesome examples. I take my hat off to your presentation and education magic. Thankyou.

    @user-gn2ll7ds9s@user-gn2ll7ds9s Жыл бұрын
  • Super straight forward guide! Thanks Jeff!

    @lawjamesr@lawjamesr Жыл бұрын
  • This is the jinja video I always wanted when starting out. Great work!

    @singaporepastor@singaporepastor Жыл бұрын
  • Thanks for the simple guide to templates. Although I am already using them, I copied most of the code without fully taking in what they did. You have clarified the usage in an easily understandable way. I am looking forward to the next video!

    @IanTakats@IanTakats Жыл бұрын
  • Dude you're a legend, I only needed 10 mins of your video to make complete sense of a template sensor I was struggling with!

    @chimeranzl9147@chimeranzl91472 ай бұрын
  • Thanks so much Jeff - this is the most helpful Templates video I have seen. I have managed to cobble together 1 on my own, but did not fully understand why it worked when the 22 non-working attempts preceding the success failed. This explains it for me. Well done!

    @fredarmstrong6418@fredarmstrong6418 Жыл бұрын
  • Hello, You've just shown me just how powerful and interesting these templates can be! Many thanks!

    @philippe5394@philippe53942 ай бұрын
  • Thank you! Been looking for a primer like this. I really hope you continue on with follow on vid's on this.

    @Joseph-ko2kl@Joseph-ko2kl Жыл бұрын
  • Great Video! It could be improved by using a different color scheme for your console (maybe that's not even possible). It was really hard to read.

    @samuraiintellectual@samuraiintellectual Жыл бұрын
    • That’s good feedback.

      @SlackerLabs@SlackerLabs Жыл бұрын
  • Thanks! This video was awesome and helped clear a few things up for me. And I love your solution for Day of Week / Month of year - excellent implementation.

    @Dorff_Meister@Dorff_Meister Жыл бұрын
  • Thank you for creating such an insightful and engaging video, I really enjoyed and learned a lot from it

    @atabatabaee@atabatabaee5 ай бұрын
  • This is the best video I've ever seen on templating! It really unlocks the potential of HA, and it's always where I struggle the most in my projects. Well done!

    @makeitworktech@makeitworktech Жыл бұрын
    • Thank you sir!

      @SlackerLabs@SlackerLabs Жыл бұрын
  • Excellent job. More Jinja videos please. A whole series (4-5) would be great

    @OPB682@OPB682 Жыл бұрын
    • Right on! I had planned just one other. Then I got scripting and realized I have more to cover. So I suspect it will be a few more. And I am going to try to make them shorter. Focus on a specific thing with each one.

      @SlackerLabs@SlackerLabs Жыл бұрын
  • Jeff, your videos and GitHub repo have given me so many ideas on things I can do in HomeAssistant. Keep up the great work! BTW, everyone loves my “Automate the boring stuff” t-shirt

    @My427Cobra@My427Cobra Жыл бұрын
    • Nice!

      @SlackerLabs@SlackerLabs Жыл бұрын
  • I created a notification for when each member of my household is home based on some stuff you did before, and it was a pain to try to get it to be generic enough to not repeat a lot of code. I was able to convert it over to a macro easily and it makes things smoother. As a programmer, I hate duplicating stuff, even yaml. Can't wait to see your video on macros for more ideas to automate my home!

    @JamesRitchieWolfmanWhtwlf@JamesRitchieWolfmanWhtwlf Жыл бұрын
  • This is Amazing start for templates, always scared to look at temples, but not anymore 😮

    @narshimagoudpalakurla9651@narshimagoudpalakurla9651 Жыл бұрын
  • another great video. I am looking forward to the next video with the advance templates in it. would love it to work more with the TTS side and your scripts macros. still unable to get my head around your old videos. its like the initial step or a middle step or section of knowledge is missing for me and I struggle to continue

    @ByStand3R@ByStand3R Жыл бұрын
  • Thank you, I think I need to hunt through your other YAML videos so I've a better grasp of the fundamentals... Then I'll research getting the mean value of a very spiky "uv" sensor that I might be able to use when it's darker rather than using sunset. 🙂

    @mastweiler22@mastweiler229 ай бұрын
  • I really appreciate the effort to make this tutorial and the effort it took to figure out where to start to help those of us who have no formal education in coding. I have used cut/paste then trial/error coding for HA for years, now I already have way more understanding of what I am doing and how to do it.

    @rivjr007@rivjr007 Жыл бұрын
  • useful I was shying away from templates now i think I can try atelast. Need more of these type

    @bat3332@bat3332 Жыл бұрын
  • Let's get that next video on this topic!!! Great info! One way I use templates a lot is in automations. I'll create an automation and then need another one very similar. I duplicate the automation and edit the yaml of the copy. Sometimes it's also easier to write logic for an automation in yaml. Then I select template as a condition in the automation and slap the yaml in there. I did that last night because I needed a true or false based on a bunch of math. Also .. as mentioned.. notifications! HA still doesn't have a good gui to create dynamic notifications.

    @jmr@jmr Жыл бұрын
  • Clear explanation. The added yaml code and the sequel are important reasons to continue following this series of videos. Thank you very much. I have learned a lot and hope for more. Tip: the yaml code on the video is difficult to read. Better contrast letters and background color will help.😀

    @edwaaij4752@edwaaij47524 ай бұрын
  • Wonderful video, thank you!

    @KaiKai-uz4tf@KaiKai-uz4tf Жыл бұрын
  • very useful, would like to see a sequel

    @gerardschepers3744@gerardschepers37443 ай бұрын
    • I got 3-4 videos in this series. Should be a playlist of them.

      @SlackerLabs@SlackerLabs3 ай бұрын
  • Jeff - Love it... Looking forward to the next video... I am looking to build templates that bridge my Accurite sensors (rain in particular) to my Home Assistant dashboard... Really linking your SDR video on sensors to tracking things like rain on a day, week, month basis...

    @bill_automated1226@bill_automated122611 ай бұрын
    • I'm slowly getting back to it. Bronchitis has made my voice pretty rough so I just got to the point where I think it wont sound too bad.

      @SlackerLabs@SlackerLabs11 ай бұрын
  • Great tutorial

    @matthewfurlong5974@matthewfurlong597410 ай бұрын
  • Very good at instructing. Thanks. I found this very helpful to get started on templating. BTW, you are wearing a Dallas Stars hat, are you in Dallas area? I am thanks again.

    @brettautrey4117@brettautrey4117Ай бұрын
  • Such a Teacher ! Thanks ! I definitely subscribe to your KZhead channel ! 👍

    @knelletv@knelletv2 ай бұрын
    • Thank you! I see paces I need to improve, but I'm glad even with my fumbles it helps!

      @SlackerLabs@SlackerLabsАй бұрын
  • Good information, Jeff. May I make a small suggestion: don't use the dark theme for showing code, since red lettering almost disappears on an black background.

    @steveelves3499@steveelves349915 күн бұрын
  • Thank you Jeff!!!!!

    @MarkGhali@MarkGhali Жыл бұрын
    • I hope it helps and doesn't make things more confusing. I debated just walking though different types of templates. So if I missed something let me know.

      @SlackerLabs@SlackerLabs Жыл бұрын
    • @@SlackerLabs This is a great start and is helpful! Thank you

      @MarkGhali@MarkGhali Жыл бұрын
  • Great video. Hope the advanced template video will follow soon. Question:is there a way to include template in lovelace entity cards eg to change the icon color?

    @johnnynobels@johnnynobels Жыл бұрын
    • I’m not sure on the color change. I know you can change icons if you are using something like the markdown card. But I’m Not sure on color.

      @SlackerLabs@SlackerLabs Жыл бұрын
  • I started using Chatgpt to write templates for automations in home assistant. It has been a really big improvement for a lot of my automations, since I don't know how to write them.

    @afcragg@afcragg Жыл бұрын
    • I haven't played with ChatGPT yet...at least not in terms of smart home. But given that Python and jinja2 is pretty widely used I suspect it might be pretty good at it.

      @SlackerLabs@SlackerLabs11 ай бұрын
  • I always avoided templates because jinja isn’t my favorite but your advanced templates definitely gave me some awesome ideas I may try out.🙌🏾 Question: does hone assistant allow you to create sensors dynamically? Let’s say …as part of an automation?

    @technithusiast@technithusiast Жыл бұрын
    • well, I am not sure it does, in the way I think you are asking. But a pattern I used a lot is I create a MQTT sensor that is simply watching a MQTT topic, and then I have an automation that does a lot of this jinja stuff and simply published the result to the MQTT topic. It doesn't have a way to actually create the sensor. But I do some stuff like have a sensor that the value is lets say the record high temp for my house in a year, and each night an automation kicks off and does some jinja math to figure out if the day's high temp is greater than the current record, and if it is updates the record high sensor.

      @SlackerLabs@SlackerLabs Жыл бұрын
  • I've learned a lot by going through your config and this clears things even more. I have a question though, how often do you have to go back to maintain these due to upgrades? For example, sensors can have a device class, units, etc.

    @PatrickBulteel@PatrickBulteel Жыл бұрын
    • For most of them it’s a set it and forget it. But I did find some as I was working on gathering examples for my next video that were pointed to entities that didn’t exist anymore. So I definitely need to come up with a way to notify me on those. But honestly most I don’t have anything to do once they are built. Only when source entities change.

      @SlackerLabs@SlackerLabs Жыл бұрын
  • Thanks, ❤ the greater than sign.

    @clairerovic@clairerovic Жыл бұрын
  • Thanks

    @tiranhay@tiranhay7 ай бұрын
  • Thanks!!! I've been wanting to learn about templating and now I know!! How does the "energy" part of the light work? Is this part of a particular light you have or is this a new feature in HA that I haven't run across?

    @KentSmith@KentSmith Жыл бұрын
    • Yea, it's an attribute as part of that light bulb. I think it's a Sylvania zigbee bulb.

      @SlackerLabs@SlackerLabs Жыл бұрын
    • @@SlackerLabs you forgot the unit (kWh).

      @mrxmry3264@mrxmry3264 Жыл бұрын
  • I was watching your new video so had to go back to this one first. Great introduction but for someone new to HASS the actual process of going from the template editor to an actual file (I see you use sensor.yaml but have no idea of the creation process and how to ensure HASS even knows it exists) and how HASS starts using that template file. For example, I on the YAML tab you had the Sensor reload option, I do not. I realize its because I don't have a sensor.yaml file. Is it as simple as creating the file and then HASS knows it exists or do I need to do some kind of include in the configuration.yaml file?

    @russellmm@russellmm11 ай бұрын
    • You can really do it one of three ways, and it comes down to how you want to lay out your configuration files. You could just add it to your configuration.yaml file. Where your just drop a sensor: heading and then start adding it under that. Or you could fancier with your organization. I threw together a gist that shows the different ways to set them up. But yeah you will have to manually add a line so that home assistant includes it. gist.github.com/thejeffreystone/3f11aaf75fd95175b674f98e030507af

      @SlackerLabs@SlackerLabs11 ай бұрын
    • @@SlackerLabs thanks!

      @russellmm@russellmm11 ай бұрын
  • i'm really hoping you have something in the advanced video regarding the alexa media player stuff... cuz i've been trying forever to create sensors for a bluetooth device connected (attribute "connected_bluetooth") to each one.... but apparently there's no way i can find to disclude the 'null' value which exists when nothing is connected...

    @vetsus3518@vetsus3518 Жыл бұрын
  • "repeating of course" nice one ;-)

    @moontowerdrums@moontowerdrums11 ай бұрын
  • Hmm... I've been looking for a way to display wind chill.

    @mastweiler22@mastweiler22 Жыл бұрын
  • @ 10:25 you mention saving the template. How is this done? I don't see a save button, nor do I see a "template entities" line, (under the "YAML" tab)

    @ppi57@ppi574 ай бұрын
    • Yeah, I can't remember what got me here, but the beginner's guide is missing some things. The YAML I am referring to is the YAML configuration files. And the save ability is based on whatever tool you use to edit those YAML files.

      @SlackerLabs@SlackerLabs4 ай бұрын
  • Has something changed in HA as the example you used to show a TypeError when converting F to C now works and returns a number in the Template editor?

    @RahulParmar1978@RahulParmar1978 Жыл бұрын
    • In my current system nope. It still gives me a type error if I try to subtract 32 from my temp sensors without the | int

      @SlackerLabs@SlackerLabs Жыл бұрын
  • Great tutorial, but isn't this the "legacy" way of coding templates? Should I be using the modern template format going forward and start converting my legacy templates to the new format?

    @major_west@major_westАй бұрын
    • Yea. I would use the more modern one if nothing more than it's organized better. I talk about the more modern format in later videos in that series. But given the amount of tutorials out there and how stuff on the internet lasts for ever knowing the legacy format will be good too.

      @SlackerLabs@SlackerLabsАй бұрын
  • You've set-up a nice video teaching templates. The problem is you are teaching the traditional style templates that are no longer supported. The new standard was installed a couple of years ago and HA Docs say that you should not use the old standard, and should use the new format. The good news is that the new standard makes a lot more sense logically. Instead of templates inside of a bunch of other integrations, it has it's own integration. Your next video should be showing everyone how to convert from the old format to the new format. Then one of the templates you wrote here would look like this: template: sensor: name: basement_temp_celsius unique_id: randomchrshere state: > {{ ((states('sensor.basement_air_device_temperature') | int - 32) / 1.8) | round }} unit_of_measurement: "°C" icon: "mdi:thermometer attributes: friendly_name: Basement Temp C Availability is there as well if you want it. There is a much richer option set and a much more logical location of the info. Please look at the HA Template docs for more details, YT will not let me post links here.

    @sirgoodenough65@sirgoodenough65 Жыл бұрын
    • Dont worry. There is a method to my madness.

      @SlackerLabs@SlackerLabs11 ай бұрын
  • So "Template Entities" is not an option under "YAML configuration reloading" and if I reload "ALL YAML CONFIGURATION" my template still doesn't show up in the states tab?

    @bradnoyes7955@bradnoyes795510 ай бұрын
    • same for me

      @jdb6284@jdb62848 ай бұрын
  • You seem to make a big jump from writing a script to storing the script. You suddenly say that you can open sensor.yaml but where is this stored? I am using docker on a NAS and can see the file system but sensor.yaml does not exist. Can I created it and if so where in the filesystem should I do this?

    @StephenEllwood@StephenEllwood10 ай бұрын
    • Yep. You are right. Holy crap. 🤦‍♂️ Yes you can just create it. Then you will need to tell home assistant that you are going to use it. So in your configuration.yaml file you will need a line like: sensor: !include sensor.yaml

      @SlackerLabs@SlackerLabs10 ай бұрын
    • @@SlackerLabs So I did this and created a sensor but it does not show up. Code is: - platform: template sensors: gas_usage_total_kwh: friendly_name: "Total Gas Used kWh" value_template: "{{( states('sensor.gas_meter_gas_consumption') | int/1000*11.36267) | round(2)}}" but I see nothing. How can I tell if it is there?

      @StephenEllwood@StephenEllwood10 ай бұрын
  • Hi, do you know why for weekday it is with () => {{ now().weekday() }} and for month without () => {{ now().month }} ? Thanks for your videos.

    @onetick3724@onetick3724 Жыл бұрын
    • Well, I am not a computer science guy, so I may get this technically wrong. But it is a python thing. So weekday() is a function from the date time python library that returns the numeric value of the week day based on a date. So Monday = 0 and Sunday = 7. But the date time library also lets us access each element of the date time using that dot notation. So now().month just returns the month from whatever date time is passed to it. now().day would return the day of the month of the current time , so if now() = 2023-04-29 12:15:45 the now().day would get 29. I always considered it a short hand method for splitting up a timestamp. So I didn't have to do some crazy stuff if I just wanted to get part of it for a condition check. now().month, now().day, now().year, now().hour, now().minute, and now().second all work to get a single part.

      @SlackerLabs@SlackerLabs Жыл бұрын
  • I’m not quite sure what happened to the chapters in the video, but it seems that those didn’t quite come through in the video player’s UI?

    @handcoding@handcoding Жыл бұрын
    • It looks like Jeff only did them as text in the video description. Video Chapters: 00:00 - Home Assistant Templates 101 - Intro 01:10 - Home Assistant Template Updates as of 2023.4 01:49 - What is Jinja? 03:07 - Template Basics: What do those Curly Braces mean? 06:57 - Home Assistant Entities in Templates 11:44 - Transform Home Assistant Entities using Templates 17:22 - Advance Templates - A Quick Look 23:00 - More to Come

      @eugsmiley@eugsmiley Жыл бұрын
    • Oh. Nice. Copy paste for the win

      @SlackerLabs@SlackerLabs Жыл бұрын
    • @@SlackerLabs Ah, right on!

      @handcoding@handcoding Жыл бұрын
  • Liked - and implemented - your counter stats idea but didn’t like the fact those were not numeric sensors. Today you showed a way to change this in your latest video: add an empty unit (unit_of_measurement: “”)

    @bennopuetz206@bennopuetz206 Жыл бұрын
    • What are you doing with the sensors that you need them to be numeric? I am only using mine in the dashboard, but curious what other use cases for that data might be.

      @SlackerLabs@SlackerLabs11 ай бұрын
  • Thanks, very useful, but I wish you wouldn't use dark theme for such demos. When viewing the video not full screen then it is very difficult to read the dark red text on a black background. Even in full screen it is often not totally clear, which is not helped by video compression.

    @djjaysmith@djjaysmith7 ай бұрын
    • Yeah, I updated it later based on feedback like this. I love feedback like this, so if you notice any other areas where it could be made easier to understand let me know!

      @SlackerLabs@SlackerLabs4 ай бұрын
  • 21:30 the month of July is missing. Why?

    @quasimodo1427@quasimodo1427 Жыл бұрын
    • Typo from when I was adding it back in....didn't even miss it evidently...

      @SlackerLabs@SlackerLabs Жыл бұрын
    • @@SlackerLabs love your vids!!!

      @quasimodo1427@quasimodo1427 Жыл бұрын
  • For those interested, here is the template coding to display a clickable Google Maps link of a device_tracker object in a Markdown Tile {% set fLat = state_attr('device_tracker.jf_cell', 'latitude')|float %} {% set iLatEnt = (fLat|int) %} {% set iLatMEnt = (((fLat - iLatEnt)|float * 60)|int) %} {% set fLatS = '{:04}'.format('%.1f'|format(((((fLat - iLatEnt)|float * 60) - iLatMEnt)|float) * 60)|float) %} {% set fLon = state_attr('device_tracker.jf_cell', 'longitude')|float %} {% set fLon2 = 0 - fLon %} {% set iLonEnt = (fLon2|int) %} {% set iLonMEnt = (((fLon2 - iLonEnt)|float * 60)|int) %} {% set fLonS = '{:04}'.format('%.1f'|format(((((fLon2 - iLonEnt)|float * 60) - iLonMEnt)|float) * 60)|float) %} {% set iLonMEnt = '{:02}'.format(iLonMEnt) %} Position : [Google Maps](www.google.ca/maps/place/{{iLatEnt}}%C2%B0{{iLatMEnt}}'{{fLatS}}%22N+{{iLonEnt}}%C2%B0{{iLonMEnt}}'{{fLonS}}%22W/@{{fLat}},{{fLon}},18z) ({{state_attr('device_tracker.jf_cell', 'latitude')}}, {{state_attr('device_tracker.jf_cell', 'longitude')}})

    @jean-francoiscyr8515@jean-francoiscyr8515 Жыл бұрын
  • So the order of operations for pipe is before math. Who's have guessed it. I bet all my calculating templates are wrong

    @uSlackr@uSlackr Жыл бұрын
    • Well it simply breaks takes the value before it and passes it directly to the function after. So if you need to do a bunch of math prior to passing the value to a function you will Need to wrap everything before it in ()

      @SlackerLabs@SlackerLabs Жыл бұрын
  • can i get an email for you jeff. i am from the bahamas and need a direct contact from you for a step by step guide

    @user-te1go5ll3m@user-te1go5ll3mАй бұрын
  • For getting the weekday or things like that I've used now().strftime('%B'). There's a list of all the % arguments out there, but that's how I conveniently desplay time and date on my dashboard is a '%A, %B %d %Y' which gets me Weekday, Month Day Year

    @nicholasplentovich8685@nicholasplentovich8685 Жыл бұрын
    • Yeah and for anyone reading this, you could totally use that in that template sensor instead of the craziness I did if you want to create a sensor to use everywhere so you dont have to remember all the string from time function arguments..

      @SlackerLabs@SlackerLabs Жыл бұрын
KZhead