Homework for you! Can you write a loop with Jinja templating that gives a comma separated string with all lights that are on. Can you display their entity id? For bonus points could you include their brightness and colour attribute!
AMAZING, great explanation of templating and its syntax. Specifically explaining the difference between the formatting, and the actual (how do I say this correctly) functions of the code? It suddenly makes a lot more sense now thank you!
Honest to God templates in Home Assistant was so confusing to me. I’ve been using Home Assistant now for over two years and have never been able to use the templates because I just didn’t understand it. I really appreciate you taking the time to explain it I can see how this is gonna be extremely useful. I’m still not a fan of the syntax but now I see a way of how to use this feature. Coincidentally this is exactly what I want to do, I have a few smart things buttons And I want to be alerted when the battery level falls below a certain percent. This is so helpful. Thank you.
Thanks! I've been wanting to write an automation that notifies me when any one of my many ZigBee devices' battery starts running low. Nothing more annoying than realising your device has not been doing its job for a few days, and is offline🤦🏼♂️ This is just the start I needed to make it happen.
First of all, just wanted to say thank you for the great tutorial. I have been looking for a tutorial to explain templating for a while and yours is the first that I can say that I really understand. Second, here is what I have come up with for my lights. I don't really change the color of my lights very often, but I am concerned with the brightness of them. Also, some of my lights are on switches, mostly shelly controllers, so to include them in the results I needed to do something a little different. Obviously, there is no brightness value for them. Here is what I have come up with to first list the lights that are dimmable, and second list the lights that are on a switch. {% set lights_on = namespace(sensors=[]) %} {% for state in states.light%} {% if state.state == 'on' %} {% set lights_on.sensors = lights_on.sensors + [state.name ~ ' ON' ~ ' Brightness: ' ~ int((state.attributes.brightness / 255) * 100) ~ '%' ] %} {% endif %} {% endfor%} {% for state in states.switch%} {% if state.state == 'on' %} {% if state.name.endswith('Light')%} {% set lights_on.sensors = lights_on.sensors + [state.name] %} {% endif %} {% endif %} {% endfor%} {{lights_on.sensors|join(' ')}} And the Results: 3D Printer LED ON Brightness: 61% Joe's Nightstand ON Brightness: 80% Kitchen Sink Light ON Brightness: 27% Monkey Lamp ON Brightness: 100% Dining Light ON Hallway Light ON Master Light ON
Really like this line of code: {% if state.name.endswith('Light')%} what happens if the entity ends with light instead of Light. Can you deal with the case ? Good work
This was excellent. Very approachable. one thins - you somewhat skipped over the explanation of what the `namespace(sensors)` code does. What is a namespace?
Namespace is a concept coming from programming basically a collection of names that the interpretar would understand. This isn’t something that can be explained that quickly !
@@SmartHomeMakers thanks for replying. However, I am still confused on how it removes unknowns. I wasn't able to find examples of this online either! Could you please explain more how piping to int(-1) removes unknowns ? Isn't int(-1) casting -1 to int (like in c/c++) ? Or does it subtract 1 from the piped data ? I'm confused!
@@algre977 When casting into an 'int' fails, it by default returns a value of 0, and in this case 0 is actually a valid potential value. The first argument to 'int' is what its default value will be (e.g. if the cast fails, what default value would you like to pass on), so all the 'int(-1)' does is set that default value if casting the int fails to -1, allowing the remaining logic to behave as wanted.
Great video and very helpful to someone like me who has used HA for several years and not really delved into Jinja at all. My only comment would be if you could explain a bit more when the code is onscreen. For example when the int(-1) is introduced, I had no idea what it meant, same with the ~ . Just be great to hear you say int(-1) means ..... and the ~ is for the following.
Another great video 👍. This series is helping me a lot as a noob to home assistant as I'm sure it's helping others. Is there any chance of doing a video on how to integrate a device like a cover/blind? Or is your api video the same thing?
Nice tutorial. I'm glad I used Jinja a lot when working with Django and Flask. I can imagine people find Jinja a bit confusing if they have never programmed before. Btw ive only worked with templates in combination with automations, but is it also possible to just have a card showing the output of some Jinja code?
I created an automation which simply use options to select the charging current as follows according to the voltage of my inverter battery which depends on solar power output in my off-grid system; The car charging starts if battery voltage is above 54.9v, and the car charging current is then set by Home Assistant to 6a. Above 55v to below 55.5v current is set to 10a Above 55.5v to below 56v current is set to 15a Above 56v to below 56.5v current is set to 20a Above 56.5v current is set to 25a When I ran the automation, it started and changed the charging current from 6a to 20a, but it was stuck there even when the battery voltage had changed. So, my question is, please, is it possible to create an automation in the user interface to check the battery voltage and set the corresponding charge current in a loop which should run until the battery voltage falls below the starting trigger which is 54.9v? Although I have just been using Home Assistant for about a week, using conditions to trigger actions is tricky. I used OPTIONS and I also used IF...ELSE and none seems to have worked. Could my problem be because I used device instead numeric to set my trigger as I have seen some people do? Any guide, or suggestions from you will be greatly appreciated.
{% for state in states.light %} {% if state.state == "on" %} {{ state.entity_id }} = {{ state.state }} {% endif %} {% endfor %} {% for switches in states.switch %} {% if switches.state == "on" %} {{ switches.entity_id }} = {{ switches.state }} {% endif %} {% endfor %} Not exactly but almost there, not separated by commas
Great work so far! In the 2nd for loop would use switch instead of switches as when you iterate you have 1 switch at a time. This is a little bit of programming best practices :)
@@kodeypatterson8973 not sure if it is the way you did the copy and paste but would be great to indent the code {{}} section needs to be to spaces in !
@@SmartHomeMakers Kinda depends on how your group attributes are defined. I have the groups setup by Hue so the code below works for me. Could otherwise check and exclude any entity which has the attributes lights in them. {% set output = namespace(sensors=[]) %} {% for state in states.light | selectattr('attributes.hue_type', '!=', 'room') %} {% if state.state == 'on' %} {% set output.sensors = output.sensors + [state.name ~ ' (' ~ state.state ~ '), rgb-colour: ' ~ state.attributes.rgb_color] %} {% endif %} {% endfor %} {{ output.sensors | join('; ') }}
Homework for you! Can you write a loop with Jinja templating that gives a comma separated string with all lights that are on. Can you display their entity id? For bonus points could you include their brightness and colour attribute!
Thanks so much for the tutorial it really helps. Please keep making these kind of videos.
Thanks Jakob! Are you going to give the light script a shot?:)
@@SmartHomeMakers oh yeah definitely. Can I ask you something in private I have s small problem with my lights?
@@jakobholzner Feel free to send me an email, but can't promise I have the time to help but will try my best :)
Thanks for explaining templates so well. I always wanted to learn about templating but it looked so hard.
Thanks Adi!
@@SmartHomeMakers thank you. keep on the good work.
AMAZING, great explanation of templating and its syntax. Specifically explaining the difference between the formatting, and the actual (how do I say this correctly) functions of the code?
It suddenly makes a lot more sense now thank you!
Glad you enjoyed it! Checkout my full masterclass on UA-cam
Honest to God templates in Home Assistant was so confusing to me. I’ve been using Home Assistant now for over two years and have never been able to use the templates because I just didn’t understand it. I really appreciate you taking the time to explain it I can see how this is gonna be extremely useful. I’m still not a fan of the syntax but now I see a way of how to use this feature. Coincidentally this is exactly what I want to do, I have a few smart things buttons And I want to be alerted when the battery level falls below a certain percent. This is so helpful. Thank you.
Thanks Mark! I’m thinking to organise a templating full masterclass on UA-cam so subscribe so you don’t miss it!
Danke!
Appreciate a lot the support :)
Thanks
Hey Peter! Thanks for the donation much appreciated:)
Thanks! I've been wanting to write an automation that notifies me when any one of my many ZigBee devices' battery starts running low. Nothing more annoying than realising your device has not been doing its job for a few days, and is offline🤦🏼♂️ This is just the start I needed to make it happen.
Awesome Jaco, I would have liked to include in this video adding the sensor to a dashboard. So that you can spot it easy!
First of all, just wanted to say thank you for the great tutorial. I have been looking for a tutorial to explain templating for a while and yours is the first that I can say that I really understand. Second, here is what I have come up with for my lights. I don't really change the color of my lights very often, but I am concerned with the brightness of them. Also, some of my lights are on switches, mostly shelly controllers, so to include them in the results I needed to do something a little different. Obviously, there is no brightness value for them. Here is what I have come up with to first list the lights that are dimmable, and second list the lights that are on a switch.
{% set lights_on = namespace(sensors=[]) %}
{% for state in states.light%}
{% if state.state == 'on' %}
{% set lights_on.sensors = lights_on.sensors + [state.name ~ ' ON' ~ ' Brightness: ' ~ int((state.attributes.brightness / 255) * 100) ~ '%' ] %}
{% endif %}
{% endfor%}
{% for state in states.switch%}
{% if state.state == 'on' %}
{% if state.name.endswith('Light')%}
{% set lights_on.sensors = lights_on.sensors + [state.name] %}
{% endif %}
{% endif %}
{% endfor%}
{{lights_on.sensors|join('
')}}
And the Results:
3D Printer LED ON Brightness: 61%
Joe's Nightstand ON Brightness: 80%
Kitchen Sink Light ON Brightness: 27%
Monkey Lamp ON Brightness: 100%
Dining Light ON
Hallway Light ON
Master Light ON
Really like this line of code: {% if state.name.endswith('Light')%} what happens if the entity ends with light instead of Light. Can you deal with the case ? Good work
Another great video! Keep on making please!
Thanks Marco!
Thanks for sharing. Great tutorial.
Thanks Ron !!!
This was excellent. Very approachable. one thins - you somewhat skipped over the explanation of what the `namespace(sensors)` code does. What is a namespace?
Namespace is a concept coming from programming basically a collection of names that the interpretar would understand. This isn’t something that can be explained that quickly !
Well presented, thanks very much. That's super helpful.
Thanks Ryan :)
Thank you for making this guide 👍🏼👍🏼
Hi Zubir! Happy to help!
What does the (-1) next to int do ?
It removes the unknowns, the devices that are not working or available for any reason
@@SmartHomeMakers thanks for replying. However, I am still confused on how it removes unknowns. I wasn't able to find examples of this online either! Could you please explain more how piping to int(-1) removes unknowns ? Isn't int(-1) casting -1 to int (like in c/c++) ? Or does it subtract 1 from the piped data ? I'm confused!
@@algre977 When casting into an 'int' fails, it by default returns a value of 0, and in this case 0 is actually a valid potential value. The first argument to 'int' is what its default value will be (e.g. if the cast fails, what default value would you like to pass on), so all the 'int(-1)' does is set that default value if casting the int fails to -1, allowing the remaining logic to behave as wanted.
@@organon69 thank you very much!
@@organon69 Thanks Glen for this very clear explanation! much better than mine
Great video and very helpful to someone like me who has used HA for several years and not really delved into Jinja at all. My only comment would be if you could explain a bit more when the code is onscreen. For example when the int(-1) is introduced, I had no idea what it meant, same with the ~ . Just be great to hear you say int(-1) means ..... and the ~ is for the following.
Great point! Another use in the community explained the -1 much better than I did!
Another great video 👍. This series is helping me a lot as a noob to home assistant as I'm sure it's helping
others.
Is there any chance of doing a video on how to integrate a device like a cover/blind? Or is your api video the same thing?
Hi James there is a video on my channel about a blind cover!
Perfect, thanks 👍
@@jimmywoody6962 no worries!
Nice tutorial. I'm glad I used Jinja a lot when working with Django and Flask. I can imagine people find Jinja a bit confusing if they have never programmed before.
Btw ive only worked with templates in combination with automations, but is it also possible to just have a card showing the output of some Jinja code?
Hi Danny! Yes you can create a template sensor in condiguration.yaml!
Goooood stuff!
Thanks Nuno! Are going to try the homework?:)
Thanks dude
Thanks Simon!
I created an automation which simply use options to select the charging current as follows according to the voltage of my inverter battery which depends on solar power output in my off-grid system;
The car charging starts if battery voltage is above 54.9v, and the car charging current is then set by Home Assistant to 6a.
Above 55v to below 55.5v current is set to 10a
Above 55.5v to below 56v current is set to 15a
Above 56v to below 56.5v current is set to 20a
Above 56.5v current is set to 25a
When I ran the automation, it started and changed the charging current from 6a to 20a, but it was stuck there even when the battery voltage had changed.
So, my question is, please, is it possible to create an automation in the user interface to check the battery voltage and set the corresponding charge current in a loop which should run until the battery voltage falls below the starting trigger which is 54.9v?
Although I have just been using Home Assistant for about a week, using conditions to trigger actions is tricky. I used OPTIONS and I also used IF...ELSE and none seems to have worked.
Could my problem be because I used device instead numeric to set my trigger as I have seen some people do?
Any guide, or suggestions from you will be greatly appreciated.
You can ask chat GPT to get a baseline code and the with trail and error get it to do what you need
Top!
{% for state in states.light %}
{% if state.state == "on" %}
{{ state.entity_id }} = {{ state.state }}
{% endif %}
{% endfor %}
{% for switches in states.switch %}
{% if switches.state == "on" %}
{{ switches.entity_id }} = {{ switches.state }}
{% endif %}
{% endfor %}
Not exactly but almost there, not separated by commas
Great work so far! In the 2nd for loop would use switch instead of switches as when you iterate you have 1 switch at a time. This is a little bit of programming best practices :)
@@SmartHomeMakers Thank you! I was hoping for a critique. Any other tips? Could I reduce the amount of lines here?
@@kodeypatterson8973 not sure if it is the way you did the copy and paste but would be great to indent the code {{}} section needs to be to spaces in !
Ppl can't troubleshoot why the lights won't turn on when they flick a binary switch in HA but to write a template ;)))
Hey Vlad! So much to learn on HA!
Here's my homework ...
# Template
{% set output = namespace(sensors=[]) %}
{% for state in states.light %}
{% if state.state == 'on' %}
{% set output.sensors = output.sensors + [state.name ~ ' (' ~ state.state ~ '), rgb-colour: ' ~ state.attributes.rgb_color] %}
{% endif %}
{% endfor %}
{{ output.sensors | join(';
') }}
# Results
Dining Table (on), rgb-colour: (255, 192, 141);
Hallway Front (on), rgb-colour: (202, 196, 255);
Hallway Rear (on), rgb-colour: (202, 196, 255)
This is my result: Coloured Lights (on), rgb-colour: (255, 179, 115);
First Floor Lights (on), rgb-colour: (255, 179, 115);
Imac Lamp (on), rgb-colour: (255, 167, 89);
Inspiration Ceiling (on), rgb-colour: (255, 192, 141);
Inspiration Room Lights (on), rgb-colour: (255, 179, 115)
Well done! Can you exclude from this list lights that are in groups? Have a think about it!
@@SmartHomeMakers Kinda depends on how your group attributes are defined. I have the groups setup by Hue so the code below works for me. Could otherwise check and exclude any entity which has the attributes lights in them.
{% set output = namespace(sensors=[]) %}
{% for state in states.light | selectattr('attributes.hue_type', '!=', 'room') %}
{% if state.state == 'on' %}
{% set output.sensors = output.sensors + [state.name ~ ' (' ~ state.state ~ '), rgb-colour: ' ~ state.attributes.rgb_color] %}
{% endif %}
{% endfor %}
{{ output.sensors | join(';
') }}