Using a script variable (field) in a choose action

I’m trying to check (test) whether a value for a variable (field) has been passed to a script and execute the correct choose sequence.

It’s my understanding that all script variables are optional. Just to emphasise: When I say variable, I refer to what’s defined under a script’s fields and are passed to a service like this (and not variables that are defined within a script):

service: script.turn_on
target:
  entity_id: script.test
data:
  variables:  # <--- this is a script field
    param1: "ignore"

Here is my current script:

test:
  description: "Test script"
  fields:
    param1:
      description: "Parameter 1"
      example: "Any text"
  sequence:
    - choose:
        - conditions:
            - condition: template
              value_template: "{{ true if param1 == 'ignore' else false }}"
          sequence:
            - service: persistent_notification.create
              data:
                title: "Testing"
                message: "Test: no param"
      default:
        - service: persistent_notification.create
          data:
            title: "Testing"
            message: "Test: '{{ param1 }}'"
            - condition: variable_value # like a state but not really
              variable: param1
              state: ""  # or None or something else?

I don’t think it’s possible (yet) and if not, I’d log a feature request but thought I’d ask because maybe there are other workarounds or clever tricks.

My options as it stands seem to be:

  1. Use a dummy value but then I need to pass this value everywhere I use the script which defeats the purpose of having an optional value (i.e. my script above).
  2. Make two separate scripts but at the cost of some duplication.

These posts were the closest I could find but they’re solving different issues:

I did see in the second post this is used but I was unable to confirm whether this actually works (I read and re-read the script docs several times to see if I’m missing something:

   - conditions:
       - condition: state
         entity_id: variable.scene_arbeitszimmer
         state: 'Hell'

Docs:

A second, somewhat related question is why this would result in the following errors:

test:
  description: "Test script"
  fields:
    param1:
      description: "Parameter 1"
      example: "Any text"
      required: false
      default: "nothing"

Errors:

[required] is an invalid option for [script]
[default] is an invalid option for [script]

I know these are purely for the UI and won’t solve my issue but I was curious to understand where I’m going wrong as I’m unable to spot the difference compared to the docs.

1 Like

I never saw those field before so I assume they are pretty new. Did you already update to 2021.4?

And I did a quick test, you can check is a parameter is passed by checking is a (jinja) variable is defined

test:
  description: "Test script"
  fields:
    param1:
      description: "Parameter 1"
      example: "Any text"
  sequence:
    - choose:
        - conditions:
            - condition: template
              value_template: "{{ param1 is not defined }}"
          sequence:
            - service: persistent_notification.create
              data:
                title: "Testing"
                message: "Test: no param"
      default:
        - service: persistent_notification.create
          data:
            title: "Testing"
            message: "Test: '{{ param1 }}'"
3 Likes

Now I feel really silly, because I didn’t think to check the Jinja2 docs. That’s it! Thank you very much.

I’ve first used it almost a year ago so it’s been there for a while (I’ll upgrade later to day but currently I’m on 2021.3.2). Because I’m curious, have you been passing parameters to a script in some other way?

1 Like

For those of us not familiar with Jninja, how do you test variables ???

Can you be more specific, with an example?

You guys were talking about testing variables using jninja. Where is that done? I tried it in the template tester in dev tools but it resulted in a ‘null’ value.

If you want to have an optional value passed to e.g. a script, you’d test wherever you use it in a Jinja template to check if it’s defined.

It’s in the post marked as the solution. This is just an illustration. You’d need to share your script if you need more help. All this does is to set the value to true if param1 wasn’t provided. Use it in an if statement to do more.

value_template: "{{ param1 is not defined }}"

Yes, I’ve got that… I thought you guys had discovered a way to see the values of variables that are defined in scripts. Kind of like how you can poke thru and see variables in VSCODE by stepping thru code and watching the variables value change.

I havent found a way to do that yet!

Send the values to a an event and display the event sensor in the dashboard.
That’s real time.

You should be able to get all you need in the traces. They break out at every node and you can see the results and sometimes the values at those stages.

1 Like

Or, another way: send a persistent notification with the values you’d like to see.

1 Like