START Leveraging Loops NOW - Home Assistant Coding Tutorial (Templating)

Поділитися
Вставка
  • Опубліковано 18 гру 2024

КОМЕНТАРІ • 61

  • @SmartHomeMakers
    @SmartHomeMakers  3 роки тому +2

    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!

  • @jakobholzner
    @jakobholzner 3 роки тому +4

    Thanks so much for the tutorial it really helps. Please keep making these kind of videos.

    • @SmartHomeMakers
      @SmartHomeMakers  3 роки тому

      Thanks Jakob! Are you going to give the light script a shot?:)

    • @jakobholzner
      @jakobholzner 2 роки тому

      @@SmartHomeMakers oh yeah definitely. Can I ask you something in private I have s small problem with my lights?

    • @SmartHomeMakers
      @SmartHomeMakers  2 роки тому

      @@jakobholzner Feel free to send me an email, but can't promise I have the time to help but will try my best :)

  • @adifoto6362
    @adifoto6362 2 роки тому +1

    Thanks for explaining templates so well. I always wanted to learn about templating but it looked so hard.

  • @someoneelse6641
    @someoneelse6641 Рік тому

    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!

    • @SmartHomeMakers
      @SmartHomeMakers  Рік тому

      Glad you enjoyed it! Checkout my full masterclass on UA-cam

  • @jig1056
    @jig1056 2 роки тому +1

    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.

    • @SmartHomeMakers
      @SmartHomeMakers  2 роки тому

      Thanks Mark! I’m thinking to organise a templating full masterclass on UA-cam so subscribe so you don’t miss it!

  • @n.r.2258
    @n.r.2258 2 роки тому

    Danke!

  • @peter24x7h5
    @peter24x7h5 3 роки тому

    Thanks

    • @SmartHomeMakers
      @SmartHomeMakers  3 роки тому

      Hey Peter! Thanks for the donation much appreciated:)

  • @javandermerwe
    @javandermerwe 2 роки тому +1

    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.

    • @SmartHomeMakers
      @SmartHomeMakers  2 роки тому +1

      Awesome Jaco, I would have liked to include in this video adding the sensor to a dashboard. So that you can spot it easy!

  • @jholt0130
    @jholt0130 3 роки тому +1

    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

    • @SmartHomeMakers
      @SmartHomeMakers  2 роки тому

      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

  • @marcohamacher2231
    @marcohamacher2231 3 роки тому

    Another great video! Keep on making please!

  • @ronm6585
    @ronm6585 3 роки тому

    Thanks for sharing. Great tutorial.

  • @uSlackr
    @uSlackr 2 роки тому

    This was excellent. Very approachable. one thins - you somewhat skipped over the explanation of what the `namespace(sensors)` code does. What is a namespace?

    • @SmartHomeMakers
      @SmartHomeMakers  2 роки тому

      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 !

  • @ryanandrewbaker
    @ryanandrewbaker 3 роки тому

    Well presented, thanks very much. That's super helpful.

  • @ZubirJamal
    @ZubirJamal 3 роки тому

    Thank you for making this guide 👍🏼👍🏼

  • @algre977
    @algre977 3 роки тому +2

    What does the (-1) next to int do ?

    • @SmartHomeMakers
      @SmartHomeMakers  3 роки тому

      It removes the unknowns, the devices that are not working or available for any reason

    • @algre977
      @algre977 3 роки тому +1

      @@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!

    • @organon69
      @organon69 3 роки тому +5

      @@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.

    • @algre977
      @algre977 3 роки тому

      @@organon69 thank you very much!

    • @SmartHomeMakers
      @SmartHomeMakers  2 роки тому

      @@organon69 Thanks Glen for this very clear explanation! much better than mine

  • @StratplayerUK
    @StratplayerUK 2 роки тому

    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.

    • @SmartHomeMakers
      @SmartHomeMakers  2 роки тому +1

      Great point! Another use in the community explained the -1 much better than I did!

  • @jimmywoody6962
    @jimmywoody6962 2 роки тому

    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?

  • @DannyDM00
    @DannyDM00 2 роки тому

    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?

    • @SmartHomeMakers
      @SmartHomeMakers  2 роки тому

      Hi Danny! Yes you can create a template sensor in condiguration.yaml!

  • @NunoSoares22
    @NunoSoares22 3 роки тому

    Goooood stuff!

  • @simonburke6681
    @simonburke6681 3 роки тому

    Thanks dude

  • @larryumolo4532
    @larryumolo4532 Рік тому

    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.

    • @SmartHomeMakers
      @SmartHomeMakers  Рік тому

      You can ask chat GPT to get a baseline code and the with trail and error get it to do what you need

  • @NunoSoares22
    @NunoSoares22 3 роки тому

    Top!

  • @kodeypatterson8973
    @kodeypatterson8973 3 роки тому +1

    {% 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

    • @SmartHomeMakers
      @SmartHomeMakers  3 роки тому +1

      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
      @kodeypatterson8973 2 роки тому

      @@SmartHomeMakers Thank you! I was hoping for a critique. Any other tips? Could I reduce the amount of lines here?

    • @SmartHomeMakers
      @SmartHomeMakers  2 роки тому +1

      @@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 !

  • @TheRealAnthony_real
    @TheRealAnthony_real 2 роки тому

    Ppl can't troubleshoot why the lights won't turn on when they flick a binary switch in HA but to write a template ;)))

  • @peter24x7h5
    @peter24x7h5 3 роки тому +4

    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)

    • @SmartHomeMakers
      @SmartHomeMakers  3 роки тому

      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)

    • @SmartHomeMakers
      @SmartHomeMakers  3 роки тому

      Well done! Can you exclude from this list lights that are in groups? Have a think about it!

    • @WouterSieverink
      @WouterSieverink 2 роки тому

      @@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(';
      ') }}