Default values for script parameters?

I’m delighted to see that scripts can take parameters using the ‘fields’ clause. Each parameter can be given a description and an example value.

It would be very nice to set defaults for the parameter values there, too, I think. At present you can handle defaults to some degree with a Jinja filter. Here’s my little trial script:

parameter_test:
  alias: Parameter test
  fields:
    number:
      description: The number to go in the message
      example: 123

  sequence:
    - service: notify.mobile_app_my_iphone
      data_template:
        title: "Life, the Universe and Everything"
        message: "The answer is {{ number|default('42') }}"

But if you use the parameter in multiple places, you get duplication, and I think it would be tidier anyway to be able to do this:

  fields:
    number:
      description: The number to go in the message
      example: 123
      default: 42

Should I dig in and see if I can implement this? Is there a better solution?

8 Likes

Sounds like a great idea to me.

+1

Great idea.

At risk of this just becoming a long list of approvals…

+1
Great idea

Definitely - I just ran into several cases where this would be a great feature!

Ah, and I just ran into a big project for a client and so have less time available than I’d thought. Sorry! It’s on my list, but if anyone gets to it before me…

+1 here too
Have some use cases where this would make automation much easier
Can we make a feature request out of this? :slight_smile:

+1 for this one to, also with support for template values for the default value like this:

  fields:
    number:
      description: The number to go in the message
      example: 123
      default: 34
or something like this:
      default_template: "{{ states('input_number.myHelper') }}"
1 Like

+1 for this

+1 here too, just ran into a case for this. :crossed_fingers:

+1 for this =)

I shouldn’t have started this; I thought things were going to be quieter in 2020 and in fact they got a lot more busy, so I’m afraid anyone waiting for me to do it will need a lot of patience! Sorry!

If anyone out there has an HA development environment set up (unlike me) and can take a look at it, don’t worry that you’ll be stepping on my toes. :slight_smile:

Q

This is already on the docs (and the only place where I saw it mentioned). However it is not working. When you don’t provide a value for certain field with a default value it just behaves like if it is undefined.
Was it implemented or just added to the docs?

1 Like

Are your referring to this?

default any
The default value for this field, as shown in the UI.

The key phrase is “as shown in the UI”.

If that’s not it, where in the documentation are you referring to?

Yes, that is exactly what I’m referring to.
What do you mean by the key phrase? Does that mean that such property is only used to show in the docs and that you are responsible to ensure it within the actual script? If that is the case, that is not only confusing but also quite useless.

Regards

1 Like

It didn’t confuse me and I have found it to be useful.

However, if you believe the description should be revised, you are free to improve it because Home Assistant’s documentation can be enhanced by anyone. Scroll to the bottom the documentation page and click the Edit button (you will be led to the associated GitHub repository where you can submit a Pull Request containing your suggested revisions).

I could, but in order to do that, first I need to understand what that default is for exactly. Is my previous description accurate ? It is only for documentation? if that is the case, where does that docs appear? When I try to call the script as a service there is no default value on the documentation section.

If you have a script employing default, go to Developer Tools > Services and call the script. You’ll understand what “as shown in the UI” means.

That is exactly what I did BEFORE searching for an topic about it BEFORE I open any new topic. I am not a bad person, I tried everything I could think about before asking.

This is my script:

alexa_say:
    alias: Alexa say
    fields:
        message:
            name: Message
            description: The message you want to announce
            required: true
        echo:
            name: Echo
            description: The echo you want to target
            default: echo_1
    sequence:
        - service: notify.alexa_media
          data:
              message: '{{ message }}'
              target: 'media_player.{{ echo }}'
              data:
                  type: announce
                  method: all
    mode: single
    icon: mdi:speaker

ANd this is what I see on the developer tools.

Can you point me out where should I be seeing the default value and how it is supposed to work?

To understand how Home Assistant uses the default option, try this script:

Show code
script_example:
  alias: "Script Example"
  icon: "mdi:flask-empty-outline"
  description: "Demonstrate fields"
  fields:
    temperature:
      name: Temperature
      description: "The desired temperature"
      selector:
        number:
          min: 15
          max: 30
          step: 1
          unit_of_measurement: '°C'
          mode: slider
      default: 22
    color:
      name: Color
      description: "The desired color"
      selector:
        select:
          options:
            - Yellow
            - Green
            - Red
            - Blue
            - Orange
      default: Red
    title:
      name: Title
      description: "The title of the notification"
      selector:
        text:
      default: "Temperature Report"
    message:
      name: Message
      description: "The message of the notification"
      selector:
        text:
      default: "The temperature is"
  sequence:
    - service: notify.persistent_notification
      data:
        title: "{{ title }}"
        message: "{{ color }} {{ message }} {{ temperature }}"

It will look like this in the UI:

However, there appears to be a UI bug because if you click Call Service it will produce an empty persistent_notification; it fails to use the default values. :thinking: :man_shrugging: If you change the values, it will use them in the persistent_notification.

Anyway, that’s what the documentation means when it states “as shown in the UI”.


For more information about selectors, refer to: Selectors