Correct templates and if then else notation in automation?

After hours search, read and test, I’m still not understand, what is the correct or best practice notation to use a simple if then else pattern in automation. New in HA now for only some days. Perhaps, someone can help me?

I really only want to have a simple if then else with a helper number. I read that “simple” if then else is not provided in automation, because the usage of templates is more powerful but needed for this.

I only want to understand the concept. And what is needed to write how and where. So the content is not important here. It should only work without errors to set a value back and forth.

action:
  - service: input_number.set_value
    data_template:
      entity_id: input_number.test
      value: >
        {% if ((states('input_number.test') | int) < 1) %}
          1
        {% else %}
          0
        {% endif %}

This is working for now. But I saw different notations, etc. So:

  • Some sources put entity_id: on service level and not below data_template level. Why and when to use which “position”?

  • On change, the editor changes > after value to |. Important? Why? Do I have to use > in general?

  • Some examples are using >- after value and then {-, -}, etc. Why? What is the best way?

  • Is everything allowed or only working by change?
    if ((states(‘input_number.test’) | int) < 1)
    if states(‘input_number.test’) | int < 1
    if states(‘input_number.test’)|int<1
    so what about necessary and needed or not needed spaces and (=

  • And is the above way somehow simpler possible and/or I did not find the simple ifthenelse until now.

Thanks in advance. :slight_smile:

It really doesn’t matter at all. You can put it under data: or at the same level as service:.

the ‘>’ indicates a multi-line template. So does the ‘|’. the difference is that > replaces newlines with spaces and | keeps the newlines.

You will typically always use >

the - removes whitespace. Most of the time it makes no difference. I almost never use them. But it probably doesn’t hurt to use them either.

you can put spaces in. I do it because it’s easier to read.

as far as () go, you will have to play around with the templates to know where they are needed. just remember that filters always act on the last thing before them. so at times you may need to force a grouping before you apply a filter

so these two templates aren’t the same:

{{ 2.05 * 5.77 | int }}
{{ (2.05 * 5.77) | int }}

just remember your order of operations.

you could simplify it in this case but sometimes it’s easier to read if you write it like you have it now.

But you could do it like:

value: "{{ 1 if states('input_number.test') | int < 1  else 0 }}"
1 Like

Wow. Thanks a lot for answering every question. Very valuable for me. I really have to get used to the syntax. But as there are a lot of examples and with such help and description as you have provided, Im sure, this will work.

Thanks again! :slight_smile:

Out of curiosity, what is the purpose of this template?

I’m asking because the conversion it performs is peculiar.

  • Values greater than or equal to 1 are reported as 0.
  • Values less than 1 are reported as 1.

What is the input_number’s range and step-size?

Only to get into the sytax how to use if_then_else, input_number, services, templates, …

And here with such simple idea to change the value from 1 to 0 back and forth eyery time the action runs - when nothing else is touching the value. :wink:

I see, so it’s a contrived example, purely for experimentation.

I believe finity touched upon it but you don’t need to use data_template: any more, if you are using one of the latest releases of Home Assistant. Just data: is fine. In fact, data_template: been deprecated and if do use it, you will see warnings in the log.

1 Like

Thank you for this input. Wasn’t able to find such notation in the examples in the forum. Clear, if this is new. And on the other hand good. :wink: Otherwise there were even more questions and confusion from me.

But now I found it here. Will go through it as well.

Check the release notes for 0.113 because it also introduced enhancements to scripts, automations, etc.

1 Like