Scripts, passing variables and how to check for an empty one?

Has something changed?

This used to work (calling a script with an empty variable and checking for a length of zero) but now gives me an error (I think since 118).

      - service: input_number.set_value
          entity_id: >
            {% if quiet_time_volume | length != 0 %}
            {% else %}
            {% endif %}
          value: >
            {{ quiet_time_volume | float }}

announcement: Error executing script. Unexpected error for call_service at pos 9: Error rendering data template: TypeError: object of type 'float' has no len()

Based on the error message, it’s now assuming the type, of the variable being passed, is float (not string). If the value’s type is float or int then you can’t use it with the length filter.

Try this:

  - service: input_number.set_value
      entity_id: >
        {{ 'input_number.announcement_quiet_time_volume' if quiet_time_volume is defined and quiet_time_volume != none else 'none' }}
      value: >
        {{ quiet_time_volume if quiet_time_volume is defined and quiet_time_volume != none else 0 }}
  • It checks if quiet_time_volume is defined. For example if you call the script without supplying the quiet_time_volume variable.
  • It also checks if the value of quiet_time_volume is not none. That would be the case where you supply the variable but assign it no value (quiet_time_volume: )

An acceptable way to call this script would be:

  - service: script.test_1
      quiet_time_volume: 35


Unless you’re using an old version of Home Assistant, it’s no longer necessary to use data_template when one of the options is templated. It has been deprecated and you can simply use data in all cases (with or without templated options).


Taras, you made my day!
Your answer’s just what I was looking for! Thanks!

{{ 'input_number.announcement_quiet_time_volume' if quiet_time_volume is defined and quiet_time_volume != none else 'none' }}