Create your OWN Template Sensor with Home Assistant Coding Tutorial
Ғылым және технология
★FREE Home Assistant Course★: courses.leonardosmarthomemakers.com/collections
Use Home Assistant Templating with an If Statement to create a new sensor based on information about energy monitoring.
★My Gear★
kit.co/smarthomemakers/getting-started-with-home-assistant
kit.co/smarthomemakers/how-to-build-a-smart-home
kit.co/smarthomemakers/content-creation-kit
★★Connect with me ★★
☕Coffee☕ www.buymeacoffee.com/natureboss
🌐Web🌐 leonardosmarthomemakers.com/blog/
📘Facebook📘 leonardosmarthomemakers
🐦Twitter🐦 SmartHomeMaker2
#smarthomemakers #homeassistant #smartthome
★★ Contents ★★
00:00 - Before Coding
01:34 - Templating #1
06:43 - If, Else If
08:55 - TOP TIP
10:00 - Sensor in Configuration
12:18 - Demo
DISCLAIMER: This KZread video description contain affiliate links, which means that if you click on one of the product links, I’ll receive a small commission. This allows us to continue to make videos like this.
Пікірлер: 139
Once again, thanks for a very helpful video. Your content selection and quality is constantly growing and I enjoy your channel 👍
@SmartHomeMakers
2 жыл бұрын
Thanks Jaco!!
Thank you so much for making this tutorial. So far the most straight forward and clean way of doing it.
@SmartHomeMakers
7 ай бұрын
Amazing appreciate it!
This video will help me a lot with templating. Always had struggles with it, but now it is more clear for me. Many thanks 🙂
@SmartHomeMakers
2 жыл бұрын
Thanks Robin! Glad you enjoyed it!
Excellent video. Clear and precise presentation!
@SmartHomeMakers
7 ай бұрын
Glad you enjoyed it!
Sir, you have opened my mind to new ideas! I've seen the dryer power monitor before and it's on my list of to-dos, but you have me thinking about it differently. Thank you! I feel better prepared to get started.
@SmartHomeMakers
2 жыл бұрын
Robert, my pleasure! Have a happy new year !
Thank you so much! Great for presenting custom ESPHome devices better!
@SmartHomeMakers
10 ай бұрын
Thanks!!
Thank you so much. Just two weeks into Home Assistant and struggling with 'custom sensors.' This was the perfect introduction!
@SmartHomeMakers
2 жыл бұрын
Glad it helped Tym!
Brilliant, so simple to follow. Thank you!
@SmartHomeMakers
3 ай бұрын
Thanks Stuart!
Excellent Tutorial. I'm an experienced coder but a noob to Home Assistant so I needed some help to get over the first hurdles in a new interface and language. This was exactly what I needed. I'll be watching your whole series when I have time. Thanks.
@SmartHomeMakers
11 ай бұрын
Thanks Damien! In which languages do you code in?
Well done. That's clarified a whole bunch of things. Thank you.
@SmartHomeMakers
2 жыл бұрын
Thanks Ash! Which video would you like to see next?
@mindracing
2 жыл бұрын
@@SmartHomeMakers More on templating would be good
wooooww just what I was searching. thank you very much!!!
@SmartHomeMakers
2 жыл бұрын
Thanks Jorge!!
This is what I was looking for. Thank you so much.
@SmartHomeMakers
Жыл бұрын
Thanks David for watching!
Very well explained and very useful tip 👍🏼👍🏼thanks
@SmartHomeMakers
2 жыл бұрын
Thanks GioBen! Much appreciated your comment !
+1 for the tutorial. I have no experience with ginja so this is great
@SmartHomeMakers
2 жыл бұрын
Glad it was helpful!
Thank you for making this video. I'm a programmer by day, but a lot of the explanations around custom sensors seemed as clear as mud until I found your video. This breaks it down and explains it very clearly. You have saved me a lot of future headache, thank you so much for the simple and concise explanation!
@SmartHomeMakers
2 жыл бұрын
Thanks Thad ! Appreciate you watching the video :)
@thad3229
2 жыл бұрын
@@SmartHomeMakers thanks for making them
Excellent! I had no idea what templating was! I actually had made one months ago I think, made a switch and a contact sensor into a cover(garage door). But you explanation of those crazy brackets was super helpful, last time I did any programming was in the early 80’s and it was the Basic language, so I’m pretty much less than zero on programming knowledge. Thanks a bunch!
@SmartHomeMakers
2 жыл бұрын
The basics of coding might not have changed much :) are you learning any new language now ?
@waynenocton
2 жыл бұрын
@@SmartHomeMakers no I’m not, except the Home Assistant and Tasmota stuff.
Great video. Thank you
@SmartHomeMakers
2 жыл бұрын
Thanks Geoff!
1000 times Thank you!! Being new to this is overwhelming but you helped greatly!! I made a simple little sensor to show whether I'm home or not... it's a start to bigger and better things :)
@SmartHomeMakers
2 жыл бұрын
Hi John! Please share your code with us :)
@TheCowboysdude
2 жыл бұрын
@@SmartHomeMakers Not sure how to or if I even can format code here but here it is.... VERY simple LOL I made that sensor that's called john_presence and put it in my sensor dir [which is included in my config.yaml] - platform: template sensors: john_presence: friendly_name: John presence value_template: >- {% if is_state('device_tracker.sm_f926u1', 'not_home') %} John's Out {% else %} John's In {% endif %}
@SmartHomeMakers
2 жыл бұрын
@@TheCowboysdude you can also achieve this without templating by linking your device tracker to a person in HA! Regardless good work!
@TheCowboysdude
2 жыл бұрын
@@SmartHomeMakers did not know that!!! Thank you!! I will try that too. What I was really trying to do was show a picture and have it change when I wasn't home.....but that's been a bit over my head :)
This is a very useful video... great job
@SmartHomeMakers
2 жыл бұрын
Thanks Robert appreciate the constant support !
Thanks for the tutorial, really nicely explained. I wish there was entities ID autocomplete in this editor, just like in VS Code. Would make playing around it so much easier.
@SmartHomeMakers
2 жыл бұрын
Kuba, you are right it would be a nice addition!
Very nice explanation.
@SmartHomeMakers
2 жыл бұрын
Thanks Jose!
Nice bite size & useful intro to templates.
@SmartHomeMakers
2 жыл бұрын
Thanks Etienne!
Great video
@SmartHomeMakers
2 жыл бұрын
Hi Parzival! Glad you enjoyed it
Enjoying your video's! Very helpful! HOWEVER, I missed the part where the "sensor.tumble_dryer_power" entity came from. Did you define it as part of the Shelly1PM? I'm currently only using esp's.
Nice! Thanks.
@SmartHomeMakers
2 жыл бұрын
Thanks Ron, I hope you have a great 2022
@ronm6585
2 жыл бұрын
@@SmartHomeMakers Thank you and Happy New Year!
plenty of examples presented in a simple way 😁👍
@SmartHomeMakers
2 жыл бұрын
Glad to hear that!
@SmartHomeMakers
2 жыл бұрын
Just made a free templating course here on YT!
Thanks for the video. I got it to work with a Sonoff S31PM by using {% if state_attr('switch.sonoff_s31pm', 'power') == 0 %} in place of what you were using.
@SmartHomeMakers
2 жыл бұрын
Is that because the sonoff switch has power as an attribute of the device and not its own entity
@vegaswayne247
2 жыл бұрын
@@SmartHomeMakers Yes, it is an attribute not its own entity. I'm using the HACS integration to get it to work in Home Assistant.
Are you kidding? My wife would love a tumble dryer as a present. Definitely do that.
@SmartHomeMakers
2 жыл бұрын
Why not:)
@TheUnofficialMaker
2 жыл бұрын
You must be the guy that bought yourself a new iPhone, gave your son an iPad, your daughter an iPod and your wife an iRon! lol
Very clear and much obliged. Only struggle here is to make the outcome of the if/else a trigger for an automation. I.e. if status dryer changes from standby to drying: send me a message or whatever action needed.
@SmartHomeMakers
Жыл бұрын
Hi Celine, you can use the from to in a trigger to look at the status!
Thanks for all the work and time you have given us. I was not able to use the term state. When I went looking on other sites I found a different way of creating sensor as follows - platform: template sensors: quickpot: friendly_name: 'fastcook' value_template: > {% if states("sensor.dishwasher_energy_current") | float == 0 %} OFF {% elif states("sensor.dishwasher_energy_current") | float
thanks for the tutorial! i'm getting this error, any idea what is going wrong here? 'float' got invalid input 'unknown' when rendering template '{% if states("sensor.dryer_power")|float == 0 %} Off {% elif states("sensor.dryer_power")|float
Love your coding videos. Keep up the good work. Could you maybe do a sensor for daily power consumption?
@SmartHomeMakers
2 жыл бұрын
Hi Oystein! Yes you could, want would you use it for? Like an automation to turn off the device once it reached a certain power level? Not sure how practical on a family basis that could be :)
@Shaq2k
2 жыл бұрын
@@SmartHomeMakers Nice. I simply want to see daily consumption of my smart plugs etc. As they are out of the box, they only show current consumption and total consumption as far as I can tell. Even in the energy dashboard. Sure, the individually monitored nodes show the daily consumption, but not when I click on one of them; then they show just the total consumption. Which in reality tells nothing at all
Setting up an automation for our dryer is the one thing I've been unable to do so far. I live in the U.S. and we have the funky plugs for our dryers, so using a normal power monitor smart plug won't work. I've heard of Shelly before, do you have a link to the one you used? I know Shelly offers some products here in the US, so hopefully I can get one that will work.
Thanks Gio, a great video on templates! Much needed as I expand my use cases in HAS. By chance do you have a video that returns a specific entity that triggered an automation? For example I have 5 smoke sensors and I want my notification to return the specific sensor that triggered the automation. Thank you!
@SmartHomeMakers
Жыл бұрын
Thanks Louis, let me know what you think about my other templating videos
I get a strange error on startup saying -sensor is an invalid option for -template
the sensor name will convert to xxxx_xxxx from name in template automatically right ?
Thank you for this, I have searched for any syntax documentation for creating templates and have had to rely on looking at other users code for clues, can you point at such documentation? For example I first thought to use a case statement rather than if but can’t find if one is available for use! I also found that I could just update templates rather than restarting HA, saved a ton of time with my many errors and restarts. Thanks for what you are doing to help us all!
@SmartHomeMakers
2 жыл бұрын
Hi Rick! There is a jinja2 templating documentation from the developer tool haven’t got a link on me :)
@rickz6006
2 жыл бұрын
@@SmartHomeMakers Maybe my biggest issue is that I never know if I am coding Yaml, Python or Jinja. None of which I have used before. Thanks!
@SmartHomeMakers
2 жыл бұрын
@@rickz6006 python is the programming language that HA is built upon, yaml mainly is seen as a configuration language as is jinja
@simonburke6681
2 жыл бұрын
I find the same, it's hard to figure out the prefix etc. It would be nice if there was a homeassistant examples cheat sheet trying to relate to the jinja documents is hard
You have just solved me another problem …. just one question left: how do you describe a value between two values let’s say „if sensor value is>= 40 but
@SmartHomeMakers
2 жыл бұрын
Hi NR I forgot if I answered this question already ?
Danke!
@SmartHomeMakers
2 жыл бұрын
Welcome!
@n.r.2258
2 жыл бұрын
@@SmartHomeMakers But I have one question left … what is the command if you want a range for the sensor …. ie between 40 and 50 … In you’re sample, you just covered 1 value i.e. one value …. how about between 2 values ?
@SmartHomeMakers
2 жыл бұрын
@@n.r.2258 have you tried >= 40 and
@n.r.2258
2 жыл бұрын
@@SmartHomeMakers {% if states ('sensor.wasser_sensor') | float >= 40 and
@SmartHomeMakers
2 жыл бұрын
you need to repeat the states portion again states ('sensor.wasser_sensor') | float
Thanks for the information.. I like the sound of the keyboard.. Can you tell me what kind it is?
@SmartHomeMakers
2 жыл бұрын
Hi Akram, just an apple keyboard!
Hello and congratulations for the home assistant videos, I need help on the dryer status template. How can I insert a delay that if it is under 6 w for 10 seconds is it stanbay?
@SmartHomeMakers
Жыл бұрын
Would that be a condition in an automation?
@ARGONAUTA34
Жыл бұрын
@@SmartHomeMakers Hello,no in this sensor, put a 10 second delay before the dryer gives me standby - sensor: - name: stati asciugatrice state: > {% if states("sensor.tz3000_gjnozsaz_ts011f_active_power")|float == 0 %} off {% elif states("sensor.tz3000_gjnozsaz_ts011f_active_power")|float
I am trying to evaluate the last notification on my phone whether it contains a list of words and if NOT contain any of the specified words then it should update the sensor. What is the best way to write this code?
@SmartHomeMakers
2 жыл бұрын
Hi Alex, nice use case, not sure home assistant is your answer for this!
Studio Code editor throws immediate error template: - sensor: incorrect type expected array state: Any ideas? thx
11:41 Line 391 'Template' disappears ? I followed the instructions and no errors but my sensor does not appear afterward.
@SmartHomeMakers
Жыл бұрын
Hello! did you check the logs?
Thanks for the video. I followed everything you did but after restarting I do not get a new sensor.tumble_dryer. Any idea why?
@SmartHomeMakers
2 жыл бұрын
Hi Chris, did you get any error messages? Do you want to share your YAML?
@cchris3
2 жыл бұрын
@@SmartHomeMakers Got this error; The following integrations and platforms could not be set up: template Please check your config and logs. Logger: homeassistant.config Source: config.py:464 First occurred: 1:27:53 PM (1 occurrences) Last logged: 1:27:53 PM Invalid config for [template]: expected dictionary for dictionary value @ data['sensors']. Got [OrderedDict([('name', 'Washing Machine'), ('state', '{% if states("sensor.washer_power")|float
@SmartHomeMakers
2 жыл бұрын
@@cchris3 can’t spot anything wrong! Is template aligned to the left no spaces ?
@cchris3
2 жыл бұрын
@@SmartHomeMakers Yea, left aligned with no spaces. "Check Config" says valid and restart happens without issues. But just dont see the new sensor. Thanks.
Can home assistant be used to send IR or RS232 command to control tv, audio receivers etc…?
@SmartHomeMakers
2 жыл бұрын
With something like a broad link you can!
@TheUnofficialMaker
2 жыл бұрын
@@SmartHomeMakers can you make a video on it!
How do we know when drying finishes to get notifications?
@SmartHomeMakers
Жыл бұрын
When the power consumption drops to nearly 0 for a certain amount of time, depending on your model!
You create the code in templates but then you don't use that when you're typing the code into the config yaml so I am confused. I have got my code in the template but what do i do with that? I cannot see why you do that first as you just create some different code in the config yaml
@SmartHomeMakers
Жыл бұрын
Do you mean in the developer tools?
Thanks for the great video! I'm using Developer Tools, Template But, I'm stuck here is my code: ``` (% if states("number.humidifier_target_level") == 55 %) off (% else %) on (% endif %) ``` I am getting this error: This template does not listen for any events and will not update automatically. I have checked the States and it shows 55. Any help would be greatly appreciated! TIA
@SmartHomeMakers
Жыл бұрын
Can you try to use states("number.humidifier_target_level") ? What do you get?
@russgulley7193
Жыл бұрын
@@SmartHomeMakers Thanks, that is the same as I'm using. Same error.
I followed this video to a T. My template works within the generator but every line of code throws back an error. As soon as i write the word template: i cant seem to correct it at all.
@SmartHomeMakers
2 жыл бұрын
Can you share your code Bobby?
it will work with sonoff ewelink?
@SmartHomeMakers
2 жыл бұрын
Have you got it integrated in HA?
Can you assign a unique id to the template sensor?
@SmartHomeMakers
2 жыл бұрын
What are you trying to achieve Damuson?
@Rich33527
2 жыл бұрын
@@SmartHomeMakers Thanks for the reply. I honestly cant remember the exact circumstances on what I was trying to do. This was when I was first learning Home Assistant. I was trying to do something but was getting an error since there was no unique ID. I obviously found a workaround and most likely a better way than I was thinking. I just cant remember what...
Can this be used to make a virtual motion or contact sensor in HA?
@SmartHomeMakers
2 жыл бұрын
Hi Ryan, what is your use case for a virtual motion sensor?
@ryantrainor2
2 жыл бұрын
Hi, Thanks for replying. I’m trying to find away to trigger an Alexa routine with ahome assistant automation.
@SmartHomeMakers
2 жыл бұрын
@@ryantrainor2 I think you will find this article a great read: robpickering.com/integrate-iotty-wall-switches-with-home-assistant/
@ryantrainor2
2 жыл бұрын
Thank you I’ll check it out much appreciated!
@SmartHomeMakers
2 жыл бұрын
@@ryantrainor2 Have a great day/evening!
HEy whenever i do the check i get a config invalid for Integration error: name - Integration 'name' not found. Integration error: platform - Integration 'platform' not found. Integration error: host - Integration 'host' not found. i have no errors on my code and it is exactly the same as yours other than the name ( i am running mine for the washer right now)
@thepyropc_
2 жыл бұрын
and if i remove what i just added it says the config is fine so its something in this it doesn't like. I'm out of ideas of what could be wrong lol
@thepyropc_
2 жыл бұрын
figured it out by adding it it made another part in my yaml freak out
@SmartHomeMakers
2 жыл бұрын
Hi Patrick! I’m glad you figured it out how did you do it ?
@thepyropc_
2 жыл бұрын
@@SmartHomeMakers Sadly had to rebuild my whole home assistant install (backups wouldn't restore for some reason_ so just had to do it all again....... but this time went way faster lol
@thepyropc_
2 жыл бұрын
@@SmartHomeMakers #washer# template: - sensor: - name: Washer Status state: > {% if states("sensor.washer_energy_power")| float == 0 %} Idle {% elif states("sensor.washer_energy_power")| float {% if states("sensor.dryer_energy_power")| float == 0 %} Idle {% elif states("sensor.dryer_energy_power")| float
Thanks for the video. BTW you have a spelling mistake in your splash screen. Cobing instead of Coding :D
@SmartHomeMakers
2 жыл бұрын
Thanks Brent!
Man, i use HA since 2018 and some of my automation using template by copy cat, i just understand the logic of the code with this tutorial. How poor my knowledge. Thanks so much
Imej itu terlalu menyinggung perasaan