Conditional parameter for service call in script

Hi,

I’m building a script which can send notifications through multiple notification services.
I’m havin trouble using templates for the Companion app notification.
In my script, I have optional parameters (fields), like channel, state, tag, etc.

- service: notify.mobile_app_phone
  data:
    message: "{{ message }}"
    title: "{{ title }}"
    data:
      channel: "{{ channel | default('') }}"
      tag: "{{ tag }}"
      persistent: "{{ persistent }}"
      color: "{{ '#%02x%02x%02x' | format(color[0], color[1], color[2]) }}"

If I just use them in my script and they are not defined I get the following warning:

Template variable warning: ‘tag’ is undefined when rendering ‘{{ tag }}’

Which is fair enough, if I specify an empty string as a default value, I can get rid of this warning, however what I really want to achive, is I that I don’t even send out the channel parameter for the service call, if the channel field is not defined for the script.

I tried it a couple ways, but I cannot make it work.

Is it creating issues it just a matter of semantics for you?

You cannot template the fields; only values when indicated as such. You’ll need to make multiple scripts and call the one with the number of parameters you have.

IIRC, a value of ‘General’ is sent anyway even when channel is left blank or not included in the service call configuration.

You may also want to set a default for tag when persistent is true, since a tag is required for persistent notifications.

Something like:

- service: notify.mobile_app_phone
  data:
    message: "{{ message }}"
    title: "{{ title }}"
    data:
      channel: "{{ channel | default('General') }}"
      tag: "{{ tag or ('Persistent' if persistent else '') }}"
      persistent: "{{ persistent }}"
      color: "{{ '#%02x%02x%02x' | format(color[0], color[1], color[2]) }}"

Well yes, I suppose I can set the default values in the template to the default values (false for persistent for example) and empty string to string. I just wanted to avoid sending out empty values. It looks like it’s not a problem for my phone, but I can’t be sure it won’t cause any problem.

And is there a way to add more field-value pairs to the data field?
So I would have an advanced field where I can define more options, because obviously I can’t prepare my script for all the available options.

Well, you can always pass a text blob if you’re willing and able to parse it (e.g. CSV or JSON).