Nested conditions in value template for automation

Howdy,

Fairly new to home-assistant and getting into some more advanced activities that I know what I want to do, but can’t figure out how to implement the syntax.

I have an automation that is triggering on the status_changed type and then using a value condition to evaluate multiple variables. I have it working currently after testing multiple options, but only because I’ve eliminated the root of the issue I was facing which is nested conditions. Basically, how to use ‘and’ and ‘or’ in groups without bashing together the visual editor condition types.

For example:
variable1 and variable2 and (variable3 or variable 4)

What I tried, in many ways that would only be evaluated based on the first or variable listed. I tried paranthesis (as example above) in different formats to no avail.

Here is the curretly working And if (sensor and MACs have been redacted):

{{
  is_state('input_boolean.redacted', 'off') and
  (now() - states.input_boolean.redacted.last_changed).total_seconds() >= 60 | int and
  trigger.event.data.new_state.attributes.ap_mac in ['mac1','mac2']
}}
  

Where really what I wanted to work (so that more nested conditions beyond this use case could be implemented was:

{{
  is_state('input_boolean.redacted', 'off') and
  (now() - states.input_boolean.redacted.last_changed).total_seconds() >= 60 | int) and
  (trigger.event.data.new_state.attributes.ap_mac == 'mac1' or
  trigger.event.data.new_state.attributes.ap_mac == 'mac2')
}}

I looked through the docs and other examples and couldn’t come up with anything. Don’t mind being told I’m a loser because my search didn’t work if you can provide said link.

TIA

Not sure what you mean really. Boolean logic works exactly like you want it to, and both your code examples should give the exact same result.

I do kind of recommend using what you call the “visual editor condition types” though. That way you can see in traces exactly which conditions evaluated to what, whereas with all conditions in a single template you can only see the composite result.

- conditions:
  - "{{ is_state('input_boolean.redacted', 'off') }}"
  - "{{ (now() - states.input_boolean.redacted.last_changed).total_seconds() >= 60 }}"
  - or:
    - "{{ trigger.event.data.new_state.attributes.ap_mac == 'mac1' }}"
    - "{{ trigger.event.data.new_state.attributes.ap_mac == 'mac2' }}"

That template should work the way you wrote it, try it out in the template editor.

I will give it another go, but as far as I could tell even with the traces that is was only processing the first or value.

I meant exactly what I said and how I expected it to work, but the results were not as expected. Thought I would sanity check before I added any more gray to my beard.