How to use Condition Selector in Blueprints?

I’ve spent a lot of time and getting no where… anyone have an example of how to use Condition Selector … I need it to be ‘dynamically evaluated’ after the trigger occurs. It seems that I cannot find a way to either return a Boolean (HA does the processing on-the-fly and returns true/false to the automation) or get the conditions to do in-place evaluation in the YAML.

one of many methods I’ve tried…

                  - choose:
                      # Path 1: No additional conditions (empty) - Profile passes
                      - conditions:
                          - condition: template
                            value_template: >
                              {% if condition_1_entity_condition is defined and condition_1_entity_condition %}
                                {% if condition_1_entity_condition is string %}
                                  {{ condition_1_entity_condition == '[]' or condition_1_entity_condition == '' }}
                                {% elif condition_1_entity_condition is iterable %}
                                  {{ condition_1_entity_condition|length == 0 }}
                                {% else %}
                                  false
                                {% endif %}
                              {% else %}
                                true
                              {% endif %}
                        sequence:
                          # All checks passed - set Profile 1
                          - variables:
                              current_profile: 1
                              active_time_delay: "{{ condition_1_time_delay }}"
                              active_brightness: "{{ condition_1_brightness }}"
                      # Path 2: Additional conditions exist - evaluate them with choose/if
                      - conditions:
                          - condition: template
                            value_template: >
                              {% if condition_1_entity_condition is defined and condition_1_entity_condition %}
                                {% if condition_1_entity_condition is string %}
                                  {{ condition_1_entity_condition != '[]' and condition_1_entity_condition != '' }}
                                {% elif condition_1_entity_condition is iterable %}
                                  {{ condition_1_entity_condition|length > 0 }}
                                {% else %}
                                  true
                                {% endif %}
                              {% else %}
                                false
                              {% endif %}
                        sequence:
                          # Use choose to evaluate the dynamic conditions
                          - choose:
                              - conditions: "{{ condition_1_entity_condition }}"
                                sequence:
                                  # Additional conditions passed - set Profile 1
                                  - variables:
                                      current_profile: 1
                                      active_time_delay: "{{ condition_1_time_delay }}"
                                      active_brightness: "{{ condition_1_brightness }}"

Is there a reason you aren’t just using the conditions in their input form?

What do you mean ‘input form’; do you mean…

Use the input directly in the main condition section of the automation:

condition: !input custom_conditions

The blueprint has a complex, and large, set of conditions to check - in addition to this one Condition Selector.

I’m using - condition: "{{ condition_X_entity_condition }}" inside the action sequence.

If I use old-skool Entity Selector with text inputs for State, it works fine… but I’m trying to provide a more modern, and flexible, blueprint using Condition Selector.

The blueprint is 1800 lines but a lot is replication since YAML/templates do not allow for Functions like traditional languages (or at least that I’ve found).

Here’s my 1st attempt (I’ve have multiple tries since this) using Condition Selector… ✨ Smart Light Timer by LTek · GitHub

Here’s a quick and dirty script blueprint as an example:

blueprint:
  name: Test Condition Inputs
  description: Things you want to test.
  domain: script
  input:
    optional_custom_conditions:
      selector:
        condition:
      name: Optional Condition(s)
      description: Set some conditions if you want.
      default: 
        - condition: template
          value_template: "{{true}}"
mode: queued
sequence:
- action: persistent_notification.create
  data:
    message: This is the Pre conditions message
    title: Testing script
- if: !input optional_custom_conditions
  then:
    # These actions will be executed either when there aren't 
    # any custom conditions or when there are and they all pass
    - action: persistent_notification.create
      data:
        message: This is the Post conditions message
        title: Testing script

And if you need to combine more “static” conditions with the custom ones, use a cheeky And condition as a hook for the input:

- if: 
    - condition: template
      value_template: "{{ foo != ham }}"
    - and: !input custom_conditions
  then: ...

Note: It doesn’t actually need a default to work, but the UI will give you a misconfiguration error when setting up the script/automation from the blueprint if there isn’t one… the minimum for it to work seems to be default: []. I just use a real condition in hope that, if that ever gets changed, the blueprint keeps working. :crossed_fingers:

1 Like

@Didgeridrew

I got it working thank you!

I added a default of - condition: template value_template: "{{true}}"
… I was having a problem where the conditions were always true, or false - - depending on my method. This resolved one part, since I want it to be True when empty (no condition wanted to be evaluates)

I also used - if: !input condition_X_entity_condition