Help using the new condition selector in a script?

I’m not new to coding scripts in YAML, and I was thinking that I’d like to make use of the new condition selector that was introduced in 2023.8—but after trying to get this to work for hours, I just can’t seem to get anywhere.

I’ve written up a quick hello-world script to try to get the syntax nailed down (below), but Home Assistant’s YAML editor just gives me this error:

Message malformed: Unexpected value for condition: 'None'. Expected and, device, not, numeric_state, or, state, sun, template, time, trigger, zone @ data['sequence'][0]['if'][0].

alias: Condition Selector Test
description: |
  Just trying to get the condition selector to work
fields:
  stopConditions:
    name: Conditions to stop the script?
    description: >-
      If you apply some conditions here, and if they evaluate to true, the
      script should stop
    selector:
      condition: 
    required: true
    advanced: true
mode: single
sequence: 
  - if:
      - condition: !input stopConditions
    then:
      - stop: >
          {{ "Stopped the script." }}
max: 10

Would anyone happen to have any ideas? (For instance, am I using the input tag correctly there?)

@Sir_Goodenough, @panhans

I’ve scoured the Home Assistant forums to try to find literally anyone who might’ve been able to get the new condition selector to work, and I think you both might be among the very few who’ve managed to unlock that rare achievement. (And I tip my hat to you both for having done so!)

I’m not sure where else I can turn to, but is there any chance that either of you might be able to help out a fellow coder toward getting the new condition selector to work? I’ve been banging my head against this one for hours, but I just can’t seem to get anywhere. :face_exhaling:

(If perhaps you might be open to taking a glance over my hello-world condition-selector attempt, I’ve posted a minimal demo/sample script at the top of this thread.)

It will only work inside the condition: block or a root condition block.

in your case…

- if: !input stopConditions

@petro Will the !input tag even pick up a variable defined under fields?

1 Like

Not sure what you mean?

Basically, !input is identical to !secret. It just takes the yaml and plops it there. In the case above, your yaml from the selector will be JSON. E.g. a state condition

{'condition': 'state', 'entity_id': 'sensor.xzy', 'state': 4}

which is the same as

condition: state
entity_id: sensor.xyz
state: 4

Which you can see why it didn’t work for OP, because the resulting yaml would be:

sequence: 
  - if:
      - condition:
          condition: state
          entity_id: sensor.xyz
          state: 4

when you want it to be…


sequence: 
  - if:
      condition: state
      entity_id: sensor.xyz
      state: 4
1 Like

@petro Ohhhhh! What you’re saying makes sense to me so far.

All the same, when I tried the below code within Home Assistant’s YAML editor, it’s giving me this error?

Message malformed: required key not provided @ data['sequence'][0]['if'][0]['conditions']

sequence:
  - if:
      !input stopConditions
    then:
      - stop: |
          {{ "Stopped the script." }}
max: 10

Am I perhaps missing a slight nuance there?

In a Blueprint, inputs are defined under the input configuration variable. But, OP is not using the Blueprint schema, they are using the standard Script schema and setting up stopConditions (with its condition selector) under the fields configuration variable.

image

2023.9.0b4

1 Like

That squiggly line is wrong. That’s most likely a bug in whatever extension you’re using for VSCode. The correct code is:

- if: !input stopConditions

What does the selection data look like? Place it in a variable and look at changed_variables.

@petro For whatever it’s worth, I tried that syntax within Home Assistant’s YAML editor just now, and it gave me the red border down the left side of the editor, implying that I had messed up something there?

Nonetheless, from there, I went to the “…” menu and chose “Edit in Visual Editor”; and then I went back to the “…” menu and chose “Edit in YAML”.

At this point, the YAML editor had seemingly rejected that part of the code—because it’s now showing me null there?

Have I messed something up here?

You most likely won’t be able to use a visual editor for this. I’m just trying to figure out what the input actually returns. You can see this in a simple script

alias: Condition Selector Test
description: |
  Just trying to get the condition selector to work
fields:
  stopConditions:
    name: Conditions to stop the script?
    description: >-
      If you apply some conditions here, and if they evaluate to true, the
      script should stop
    selector:
      condition: 
    required: true
    advanced: true
mode: single
variables:
  test: !input stopConditions
sequence: 
- stop: Stopped the script
max: 10
1 Like

Neither of these work properly.

Using !input:

"variables": {
        "test": {
          "__type": "<class 'homeassistant.util.yaml.objects.Input'>",
          "repr": "Input(name='stopConditions')"
        }
      }
Full Trace
{
  "trace": {
    "last_step": "sequence/0",
    "run_id": "9becdf00e283bb3dcfa9a22fa1533249",
    "state": "stopped",
    "script_execution": "finished",
    "timestamp": {
      "start": "2023-09-05T17:47:40.451152+00:00",
      "finish": "2023-09-05T17:47:40.459880+00:00"
    },
    "domain": "script",
    "item_id": "ztest_condition_selector_test_petro",
    "trace": {
      "sequence/0": [
        {
          "path": "sequence/0",
          "timestamp": "2023-09-05T17:47:40.456382+00:00",
          "changed_variables": {
            "test": {
              "__type": "<class 'homeassistant.util.yaml.objects.Input'>",
              "repr": "Input(name='stopConditions')"
            },
            "this": {
              "entity_id": "script.ztest_condition_selector_test_petro",
              "state": "off",
              "attributes": {
                "last_triggered": "2023-09-05T17:45:01.900376+00:00",
                "mode": "single",
                "current": 0,
                "friendly_name": "Condition Selector Test"
              },
              "last_changed": "2023-09-05T17:45:01.903529+00:00",
              "last_updated": "2023-09-05T17:45:01.903529+00:00",
              "context": {
                "id": "01H9K75769B5H88CA9F5N636MQ",
                "parent_id": null,
                "user_id": "1908acd22f0c4572a08b5df66dd349b1"
              }
            },
            "stopConditions": [
              {
                "condition": "state",
                "entity_id": "light.behind_tv",
                "state": "on"
              }
            ],
            "context": {
              "id": "01H9K7A2113R04W494E51CZT3S",
              "parent_id": null,
              "user_id": "1908acd22f0c4572a08b5df66dd349b1"
            }
          },
          "result": {
            "stop": "Stopped the script",
            "error": false
          }
        }
      ]
    },
    "config": {
      "alias": "Condition Selector Test",
      "description": "Just trying to get the condition selector to work\n",
      "fields": {
        "stopConditions": {
          "name": "Conditions to stop the script?",
          "description": "If you apply some conditions here, and if they evaluate to true, the script should stop",
          "selector": {
            "condition": null
          },
          "required": true,
          "advanced": true
        }
      },
      "mode": "single",
      "variables": {
        "test": {
          "__type": "<class 'homeassistant.util.yaml.objects.Input'>",
          "repr": "Input(name='stopConditions')"
        }
      },
      "sequence": [
        {
          "stop": "Stopped the script"
        }
      ],
      "max": 10
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01H9K7A2113R04W494E51CZT3S",
      "parent_id": null,
      "user_id": "1908acd22f0c4572a08b5df66dd349b1"
    }
  },
  "logbookEntries": [
    {
      "when": 1693936060.451347,
      "state": "on",
      "entity_id": "script.ztest_condition_selector_test_petro",
      "context_user_id": "1908acd22f0c4572a08b5df66dd349b1"
    },
    {
      "when": 1693936060.456819,
      "state": "off",
      "entity_id": "script.ztest_condition_selector_test_petro",
      "context_user_id": "1908acd22f0c4572a08b5df66dd349b1",
      "context_state": "on",
      "context_entity_id": "script.ztest_condition_selector_test_petro"
    }
  ]
}

Using {{}}, like a normal variable from fields

"variables": {
        "test": "{{ stopConditions }}"
      }
Full Trace
{  "trace": {
    "last_step": "sequence/0",
    "run_id": "a7016a0938e5f3470020a7aedd6a455d",
    "state": "stopped",
    "script_execution": "finished",
    "timestamp": {
      "start": "2023-09-05T19:45:59.316183+00:00",
      "finish": "2023-09-05T19:45:59.324772+00:00"
    },
    "domain": "script",
    "item_id": "ztest_condition_selector_test_petro",
    "trace": {
      "sequence/0": [
        {
          "path": "sequence/0",
          "timestamp": "2023-09-05T19:45:59.322925+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "script.ztest_condition_selector_test_petro",
              "state": "off",
              "attributes": {
                "last_triggered": "2023-09-05T19:45:52.426055+00:00",
                "mode": "queued",
                "current": 0,
                "max": 10,
                "friendly_name": "Condition Selector Test"
              },
              "last_changed": "2023-09-05T19:45:52.432712+00:00",
              "last_updated": "2023-09-05T19:45:52.432712+00:00",
              "context": {
                "id": "01H9KE2FS6RJCEBCVVG0ECGK4K",
                "parent_id": null,
                "user_id": "1908acd22f0c4572a08b5df66dd349b1"
              }
            },
            "stopConditions": [
              {
                "condition": "state",
                "entity_id": "light.behind_tv",
                "state": "on"
              }
            ],
            "test": [
              {
                "condition": "state",
                "entity_id": "light.behind_tv",
                "state": "on"
              }
            ],
            "context": {
              "id": "01H9KE2PGJ8VVKQGH037V880VW",
              "parent_id": null,
              "user_id": "1908acd22f0c4572a08b5df66dd349b1"
            }
          },
          "result": {
            "stop": "Stopped the script",
            "error": false
          }
        }
      ]
    },
    "config": {
      "alias": "Condition Selector Test",
      "description": "Just trying to get the condition selector to work",
      "fields": {
        "stopConditions": {
          "name": "Conditions to stop the script?",
          "description": "If you apply some conditions here, and if they evaluate to true, the script should stop",
          "selector": {
            "condition": null
          },
          "required": true,
          "advanced": true
        }
      },
      "variables": {
        "test": "{{ stopConditions }}"
      },
      "sequence": [
        {
          "stop": "Stopped the script"
        }
      ],
      "mode": "queued",
      "max": 10
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01H9KE2PGJ8VVKQGH037V880VW",
      "parent_id": null,
      "user_id": "1908acd22f0c4572a08b5df66dd349b1"
    }
  },
  "logbookEntries": [
    {
      "when": 1693943159.316543,
      "state": "on",
      "entity_id": "script.ztest_condition_selector_test_petro",
      "context_user_id": "1908acd22f0c4572a08b5df66dd349b1"
    },
    {
      "when": 1693943159.323439,
      "state": "off",
      "entity_id": "script.ztest_condition_selector_test_petro",
      "context_user_id": "1908acd22f0c4572a08b5df66dd349b1",
      "context_state": "on",
      "context_entity_id": "script.ztest_condition_selector_test_petro"
    }
  ]
}

It seems that the Condition selector isn’t really compatible with standard scripts at this point… It works with Blueprint scripts like the following variation on your script:

blueprint:
  name: Test Condition Selector B.P.
  description: ''
  domain: script
  input:
    stopConditions:
      name: Conditions to stop the script?
      description: >-
        If you apply some conditions here, and if they evaluate to true, the
        script should stop
      selector:
        condition:

sequence: 
  - if: !input stopConditions
    then:
      - stop: "Stopped the script due to conditions."
    else:
      - service: light.toggle
        target:
          entity_id: light.example
mode: queued
max: 10

But that’s not the same functionality you would get if it was available as a normal fields variable and you want to call it from another script or automation.

1 Like

[…]

I can’t disagree with your conclusion, @Didgeridrew. And big thanks to both you and to @petro for your valuable feedback and discussion here about this.

For whatever it’s worth, I happen to be the developer of an open-source light-fader script, and I had been hoping to be able to add support for arbitrary user-selected conditions for when the script would halt itself. (NGL—I thought that that could’ve been a pretty nifty feature!)

Given the current state of the condition selector, though, it’s kinda looking like that isn’t really going to be feasible, eh? (And in this case, I can’t really convert the script to a blueprint since some of its users, including me, need to be able to call the fader from another script or automation.)

I’ve only used that with blueprints, not on a bare script and not by passing the info thru a field.
There is no reason I can think of not to use a script Blueprint there, though. Script BP’s will also pass field data in.

For example:

Do you happen to know of any way to call a Blueprint from another script or automation?

(I’m not trying to be contrary or anything—I’m just honestly not aware of any way of doing that.)

The BP is the framework. You generate a script by calling the script BP and setting your inputs, if you have any. You can then call the generated script (that uses the script BP in the bg) to do it’s thing.
The generated script becomes a regular script to HA.

So generate a script BP framework that will generate all the conditions you want and spawn them, and the logic that was going to vary the condition can now vary the service call with the different conditions in it, and you have fields that will pass the rest of your data thru live.

In the meantime, dream up an HA solution as you have a good usecase and do a feature request or if you think there is a bug, then an issue.

Round-about but that could get you there.

1 Like

@Sir_Goodenough I really appreciate your thoughts and your input on this.

As it happens, I have a home theater, and between the various lights involved and the various dimming scenarios (“guests arriving,” “movie starting shortly,” “movie has started,” “movie credits,” and so on), I’d probably end up with over a dozen instantiations of the Blueprint configurations if I were to go that route.

All the same, I still very much appreciate your kindness in having chimed in about all this.

1 Like

Sounds like 1 automation blueprint to me.

This one might give you some ideas…
Obviously the triggers and actions would be different, but this handles like a hundred possible actions.
I’m sure somnething similar could handle your theaters dozens.

Glad I could be of some help.

1 Like