Script variables passed in

What does a script see in lieu of a variable if that variable is not passed in?

e.g.

- service: script.my_script
  data:
      variable_1: 'some text in here'

The script my_script can optionally receive,
variable_1
and also
variable_2 (Expected to be a string)
and also
variable_3 (Expected to be a number)

How does the script test if the variable has been passed?

I have been going through my (in same places, very old) config and I have realised that I have been using a mix of these,

(% if variable1 != '' %}

(% if variable1 %}

{% if variable 1 | length != 0 %}

I am not convinced that any of them work as expected except for

{% if variable 1 | length != 0 %}

(Obviously it is rare that I don’t explicitly define all the variables when calling a script which is why I have only just discovered this)

So, as I said,

What does a script see in lieu of a variable if that variable is not passed in?

and,

What is the preferred/recommended/correct way to check?

Thanks.

I haven’t tested this but thought it would be:

(% if variable1 != none %}

Edit: just tested it. It’s not that. It’s this:

(% if variable1 != unknown %}

Edited to add:
Whatever the case in my specific example. I would have thought that it would be ‘known’ (and maybe even documented somewhere) how a called script handles ‘missing’ variables. It seems like a fairly fundamental question.


I’m not convinced…
That seems sensible and the Template tool points to it being right but when I changed the script that had the typo that alerted me to this whole question it stopped working.

The scripts in question are non-trivial so I plan to write some simpler scripts to do some testing.


In the meantime,

With this, the script works but I have a feeling it has always worked through luck not judgement. ie.e I have always provided at least one sonos

        {% if sonos != '' %}
          {% for device in sonos -%}
            media_player.sonos_{{ device }}{%- if not loop.last -%}, {% endif %}
          {%- endfor %}
        {% else %}
          media_player.sonos_dining_room
        {% endif %}

Whereas this didn’t seem to work at all (as I said I need to do more tetsing…)

        {% if sonos != unknown %}
          {% for device in sonos -%}
            media_player.sonos_{{ device }}{%- if not loop.last -%}, {% endif %}
          {%- endfor %}
        {% else %}
          media_player.sonos_dining_room
        {% endif %}

So,

I think you were (half) right, tom_l

It looks to me like both of these work:
{% if variable == unknown %}
or simply
{% if variable %}


I tested using the following (results are shown at the bottom):

(I was slightly surprised to find that testing for is_number when no variable is passed causes an error. It is obviously an extremely artificial situation but surely the result should ‘False’?)

notify:
  - platform: file
    name: log_announcements
    filename: my-log-testing.log


script:
  test_passing_parameters:
    sequence:
      - service: script.test_receiving_parameters
        data:
          string_var: This is a string
          number_var: 1.5


  test_receiving_parameters:
    sequence:
      - service: notify.log_announcements
        data:
          message: >
            string_var was passed in as *{{ string_var }}*


            TEST: if "string_var != ''"
            {% if string_var != '' -%}
              TRUE
            {% else -%}
              FALSE
            {% endif -%}
            TEST: if "string_var == ''"
            {% if string_var == '' -%}
              TRUE
            {% else -%}
              FALSE
            {% endif -%}
            ----------


            TEST: "if string_var"
            {% if string_var -%}
              TRUE
            {% else -%}
              FALSE
            {% endif -%}
            TEST: "if not string_var"
            {% if not string_var -%}
              TRUE
            {% else -%}
              FALSE
            {% endif -%}
            ----------


            TEST: "if is_number(string_var)"
            {% if is_number(string_var) -%}
              TRUE
            {% else -%}
              FALSE
            {% endif -%}
            TEST: "if not string_var | length == 0"
            {% if not is_number(string_var) -%}
              TRUE
            {% else -%}
              FALSE
            {% endif -%}
            ----------


            TEST: "if string_var | length != 0"
            {% if string_var | length != 0 -%}
              TRUE
            {% else -%}
              FALSE
            {% endif -%}
            TEST: "if string_var | length == 0"
            {% if string_var | length == 0 -%}
              TRUE
            {% else -%}
              FALSE
            {% endif -%}
            ----------


            TEST: "if string_var != unknown"
            {% if string_var != unknown -%}
              TRUE
            {% else -%}
              FALSE
            {% endif -%}
            TEST: "if string_var == unknown"
            {% if string_var == unknown -%}
              TRUE
            {% else -%}
              FALSE
            {% endif -%}
            =================================================================


            number_var was passed in as *{{ number_var }}*


            NOTE: Testing for the length of a number causes an error


            TEST: if "number_var != ''"
            {% if number_var != '' -%}
              TRUE
            {% else -%}
              FALSE
            {% endif -%}
            TEST: if "number_var == ''"
            {% if number_var == '' -%}
              TRUE
            {% else -%}
              FALSE
            {% endif -%}
            ----------


            TEST: "if number_var"
            {% if number_var -%}
              TRUE
            {% else -%}
              FALSE
            {% endif -%}
            TEST: "if not number_var"
            {% if not number_var -%}
              TRUE
            {% else -%}
              FALSE
            {% endif -%}
            ----------


            TEST: "if is_number(number_var)"
            {% if is_number(number_var) -%}
              TRUE
            {% else -%}
              FALSE
            {% endif -%}
            TEST: "if not is_number(number_var)"
            {% if not is_number(number_var) -%}
              TRUE
            {% else -%}
              FALSE
            {% endif -%}
            ----------


            TEST: "if number_var != unknown"
            {% if number_var != unknown -%}
              TRUE
            {% else -%}
              FALSE
            {% endif -%}
            TEST: "if number_var == unknown"
            {% if number_var == unknown -%}
              TRUE
            {% else -%}
              FALSE
            {% endif -%}
            =================================================================


            missing_var was passed in as *{{ missing_var }}*

            
            NOTE: Testing for is_number causes an error


            TEST: if "missing_var != ''"
            {% if missing_var != '' -%}
              TRUE
            {% else -%}
              FALSE
            {% endif -%}
            TEST: if "missing_var == ''"
            {% if missing_var == '' -%}
              TRUE
            {% else -%}
              FALSE
            {% endif -%}
            ----------


            TEST: "if missing_var"
            {% if missing_var -%}
              TRUE
            {% else -%}
              FALSE
            {% endif -%}
            TEST: "if not missing_var"
            {% if not missing_var -%}
              TRUE
            {% else -%}
              FALSE
            {% endif -%}
            ----------


            TEST: "if missing_var | length != 0"
            {% if missing_var | length != 0 -%}
              TRUE
            {% else -%}
              FALSE
            {% endif -%}
            TEST: "if missing_var | length == 0"
            {% if missing_var | length == 0 -%}
              TRUE
            {% else -%}
              FALSE
            {% endif -%}
            ----------


            TEST: "if missing_var != unknown"
            {% if missing_var != unknown -%}
              TRUE
            {% else -%}
              FALSE
            {% endif -%}
            TEST: "if missing_var == unknown"
            {% if missing_var == unknown -%}
              TRUE
            {% else -%}
              FALSE
            {% endif -%}
            =================================================================

Which gave a log output as follows:

string_var was passed in as *This is a string*

TEST: if "string_var != ''" TRUE
TEST: if "string_var == ''" FALSE
----------

TEST: "if string_var" TRUE
TEST: "if not string_var" FALSE
----------

TEST: "if is_number(string_var)" FALSE
TEST: "if not string_var | length == 0" TRUE
----------

TEST: "if string_var | length != 0" TRUE
TEST: "if string_var | length == 0" FALSE
----------

TEST: "if string_var != unknown" TRUE
TEST: "if string_var == unknown" FALSE
=================================================================

number_var was passed in as *1.5*

NOTE: Testing for the length of a number causes an error

TEST: if "number_var != ''" TRUE
TEST: if "number_var == ''" FALSE
----------

TEST: "if number_var" TRUE
TEST: "if not number_var" FALSE
----------

TEST: "if is_number(number_var)" TRUE
TEST: "if not is_number(number_var)" FALSE
----------

TEST: "if number_var != unknown" TRUE
TEST: "if number_var == unknown" FALSE
=================================================================

missing_var was passed in as **

NOTE: Testing for is_number causes an error

TEST: if "missing_var != ''" TRUE
TEST: if "missing_var == ''" FALSE
----------

TEST: "if missing_var" FALSE
TEST: "if not missing_var" TRUE
----------

TEST: "if missing_var | length != 0" FALSE
TEST: "if missing_var | length == 0" TRUE
----------

TEST: "if missing_var != unknown" FALSE
TEST: "if missing_var == unknown" TRUE
=================================================================

nooo, that only works because both are undefined.

@klogg

{% if variable_1 is defined %}
1 Like

Obviously.
(He says with a wry smile)

Thanks. Again.

1 Like