Help using the new condition selector in a script?

@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

Would love to able to use a condition selector-selected value in normal scripts (evaluate the supplied conditions). Would appreciate it if anyone can share how to accomplish this.

Is this worth filing an issue / feature request on GitHub for?

1 Like

Use the choose statement structure.
Script Syntax - Home Assistant.

@jiehan As best as I can tell, as of 2023.12.3, the condition selector doesn’t appear to be compatilbe with Home Assistant scripts—only blueprints. (See also @Didgeridrew’s findings earlier in this thread.)

And if perhaps you had wanted to file an issue / feature request on GitHub for this, I think that would be great! (And if you do, please feel free to link to it here.)

1 Like

I haven’t tested it, but perhaps try a construct like this, but replace the input with the variable containing the condition. Worth a try…

condition:
  - condition: state
    entity_id: sensor.banana
    state: "rotten"
  - condtion: and
    conditions: !input condition_selector_input

The !input tag is not working. The editor won’t allow to save the script if the tag is present (red bar on the left, and no save button):

I have no idea what you are doing there or how it relates to this topic. Also sharing code as images is just painful to see and use.

May I suggest you start a new topic and provide some of your code to help ask a full question.

That would be helpful.