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?
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 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') }}"
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.
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?
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.
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.
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
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 }}"
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. 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