WTH Hard to maintain automation conditions

Currently automation conditions do AND, meaning that every condition should pass before continuing to actions. I would love to use conditions but when I have little more complex automation, conditions gets very complex and unreadable.

e.g. Turn on lights when it’s dark When it’s night only motion triggers lights.

I hope i wrote it down correctly but this really twists my brain. This is just simple example. One of the condition would look something like this:

NOT night OR motion - Passes on day or when there’s motion on night.

Could we have something like choose for conditions too? I know I can just use choose in actions but using conditions would really help me clear things out there by dropping at least obvious false positive triggers off.

I don’t really see a “Choose” for conditions adding a significant boost in functionality considering the flexibility of current condition types. Can you show a mock up of how you think it might look/work with your night & motion example?

conditions:
  or:
    - "{{ is_state('sun.sun', 'above_horizon') }}"
    - "{{ is_state('binary_sensor.my_motion_sensor', 'on') }}"

Or in the UI:

What am I missing here? There’s an “Or” condition already. There’s also a “Not” condition if you really want to do “Not” night instead of above horizon like I am.

1 Like

Thanks for replies! I’ll try out the OR method via UI soonish.

Here’s a stub with OR. Unfortunately I did not have to finish now. I have to say that new automation UI makes it much easier. Still it’s bit tedious to handle all valid combinations.

Maybe I miss something like

  1. “when trigger id is “motion outside” check this condition to continue” If-then condition
  2. Or conditional trigger. e.g. trigger with id motion outside when it’s dark

alias: Ulkovaloautomaatio
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.yo
    from: "off"
    to: "on"
    id: Night begins
  - platform: state
    entity_id:
      - binary_sensor.yo
    from: "on"
    to: "off"
    id: Night ends
  - type: motion
    platform: device
    device_id: cffb60ae6f65667cd74e4eff91d314bb
    entity_id: binary_sensor.etuterassin_sensori_on_off
    domain: binary_sensor
    id: Motion detected
  - type: no_motion
    platform: device
    device_id: cffb60ae6f65667cd74e4eff91d314bb
    entity_id: binary_sensor.etuterassin_sensori_on_off
    domain: binary_sensor
    id: Motion not detected
  - platform: state
    entity_id:
      - binary_sensor.ulkona_pimeaa
    from: "off"
    to: "on"
    id: Dark outside
  - platform: state
    entity_id:
      - scene.ulkona_pimeaa
    from: "on"
    to: "off"
    id: Not dark in outside
condition:
  - condition: or
    conditions:
      - condition: trigger
        id: Motion not detected
      - condition: and
        conditions:
          - condition: trigger
            id: Motion detected
          - condition: state
            entity_id: scene.ulkona_pimeaa
            state: ""
      - condition: and
        conditions:
          - condition: trigger
            id: Dark outside
          - condition: state
            entity_id: binary_sensor.yo
            state: "off"
action:
  - choose:
      - conditions: []
        sequence: []
mode: single

I do actually wish we had this one. Would be a lot easier to read if you could bundle the condition with the trigger I’d instead of stuff like this:

conditions:
  - or:
      - and:
          - "{{ trigger.id == 'a' }}"
          - ... Conditions specifically for 'a'
      - and:
          - "{{ trigger.id == 'b' }}"
          - ... Conditions specifically for 'b'
      - ...

Plus I’m not a fan of how when an automation doesn’t pass the condition it still makes a trace. Makes it very difficult to effectively debug some triggers that happen very frequently, like if you need to listen for the call_service or state_changed events.

Yeah but in other hand it’s nice to debug conditions in other situations :slight_smile: . Anyhow main motivation to use conditions is to stop the logs getting cluttered.

I believe you can do that using the first variant of Trigger Variables.

The first variant allows you to define variables that will be set when the trigger fires. The variables will be able to use templates and have access to the trigger variable.

alias: example 
trigger:
  - id: first
    platform: state
    entity_id: binary_sensor.whatever
    from: "off"
    to: "on"
    variables:
      status: "{{ ... Template Condition for the first trigger ... }}"
  - id: second 
    platform: state
    entity_id: binary_sensor.whatever
    from: "on"
    to: "off"
    variables:
      status: "{{ ... Template Condition for the second trigger ... }}"
condition:
  - '{{ status == true }}'
action:
  ... etc ...
1 Like

Oh right! Trigger variables can use full templates not limited templates so they work here. Yea that is a good idea

For jollierfinbeam’s example, the two Device Triggers probably need to be converted to State Triggers because (and I may be mistaken) I don’t believe Device Triggers support Trigger Variables.

alias: Ulkovaloautomaatio
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.yo
    from: "off"
    to: "on"
    id: Night begins
    variables:
      status: "{{ ... Template Condition .. }}"
  - platform: state
    entity_id:
      - binary_sensor.yo
    from: "on"
    to: "off"
    id: Night ends
    variables:
      status: "{{ ... Template Condition .. }}"
  - platform: state
    entity_id: binary_sensor.etuterassin_sensori_on_off
    from: "on"
    to: "off"
    id: Motion not detected
    variables:
      status: "{{ ... Template Condition .. }}"
  - platform: state
    entity_id:
      - binary_sensor.ulkona_pimeaa
    from: "off"
    to: "on"
    id: Dark outside
    variables:
      status: "{{ ... Template Condition .. }}"
  - platform: state
    entity_id:
      - scene.ulkona_pimeaa
    from: "on"
    to: "off"
    id: Not dark in outside
    variables:
      status: "{{ ... Template Condition .. }}"
condition:
  - '{{ status == true }}'
action:
  - choose:
      - conditions: []
        sequence: []
mode: single

Instead of '{{ status == true }}' it can be reduced to '{{ status }}' because status contains a Boolean value (however some users might not understand that syntax).