This seems to work, but is it valid?

I was looking thru my code to clean up some of the and / or conditions with the new shortcut from Thomas, and I found this one I was using.
Don’t know how or where I came up with it, but it appears to be working. I look at the samples in the Docs and every list item appears to start with ‘condition:’. Is that word not needed if the condition is a template?

Like I said, this appears to work, but is that intended. Looking for a Masters Opinion.

This is what I was using:

condition:
  condition: or
  conditions:
    - "{{ trigger.to_state.state != '' }}"
    - "{{ trigger.to_state.state != 'off' }}"

This is what I’m changing it to:

or:
  condition:
    - "{{ trigger.to_state.state != '' }}"
    - "{{ trigger.to_state.state != 'off' }}"

I’m worried that it’s not actually doing what I think it is.

First, I am the LAST person on the forums that should be advising on YAML code. I am 99% wrong most of the time.
That said, try changing ‘off’ to ‘on’ and see if the action follows the new test.
I don’t think the second syntax is correct.

condition: evaluates everything that follows as true or false.
condition: or evaluates true if anything that follows is true
condition: and evaluates true if everything that follows is true.

The indenting rules of YAML determines what ‘follows’ includes. I am confused with the indenting in your former code segment. This is what I think it should be:

condition:
  condition: or
    conditions:
      - first condition
      - second condition

… means that the condition: evaluates as true if the -first or the -second conditions are true

You can mix other conditions, like and:

condition:
  condition: or
    conditions:
      - first condition
      - second condition
  condition: and
    conditions:
      - third condition
      - fourth condition

… means that the condition: evaluates as true if the -first or the -second conditions are true, AND both the -third and -fourth conditions are true.

If I am wrong, we will certainly be corrected very soon.

(If you want the right answer online, provide a wrong one).

Not quite. The shorthand notation should be like this:

condition:
  or:
    - "{{ trigger.to_state.state != '' }}"
    - "{{ trigger.to_state.state != 'off' }}"
1 Like

I missed that top ‘condition:’. I see it in the docs now.
What I wasn’t sure about is if the ‘if’ in any form (long or short) will iterate correctly with just a list, and you are confirming that it will.
I suppose because they are templates nothing else is needed, they are in the end true or false (pos or 0/neg)

Thanks!

When did this change occur?

is it something that’s been around for a while and I missed it or is it in the latest release?

You missed nothing. It’s this current release, 2022.5

https://www.home-assistant.io/blog/2022/05/04/release-20225/#shorthand-notation-for-logical-conditions

Which I think is actually worth putting aside some time on the weekend and updating my entire config. So much easier to read.

2 Likes

Thanks.

Just started reading the new release thread. :slightly_smiling_face:

1 Like