Add YAML fields only if variable is set

Hi,

I have the following code:

action: |
  {% if target is defined and target == "xxxx" %}
    notify.mobile_app_xxxx_handy
  {% elif target is defined and target == "yyyy" %}
    mobile_app_iphone_von_yyyy
  {% else %}
    notify.all_phones
  {% endif %}
data:
  message: "{{ body }}"
  title: "{{ header }}"
  data:
    subtitle: "{{ message }}"
    url: "{{ url }}"
    actions: |
      {% if action1_title is defined and action1_action is defined %}
      - action: "{{ action1_action }}"
        title: "{{ action1_title }}"
      {% endif %}
      {% if action2_title is defined and action2_action is defined %}
      - action: "{{ action2_action }}"
        title: "{{ action2_title }}"
      {% endif %}

The part with the actions is not working, because the result is:

actions: |-
        - action: "TEST_123"
          title: "Test"

So the characters “|” and “-” were added after “actions:”. Due to those signs the action button is not visible at the iOS notification.

So how can I add action buttons (title and id) only if I set them in the variables (action1_title, action1_action and action2_title, action2_action) ? I don’t want to have multiple condition in my automations / scripts, because then I have duplicated code.

You can’t write YAML with Jinja, but you can write the equivalent data structures.

Try:

data:
  subtitle: "{{ message }}"
  url: "{{ url }}"
  actions: >
    {% if action1_title is defined and action1_action is defined %}
      {{ {"action": action1_action, "title": action1_title} }}
    {% elif action2_title is defined and action2_action is defined %}
      {{ {"action": action2_action, "title": action2_title} }}
    {% endif %}

Thanks for your help!

It looks better now, but with your code it is still not working, because the “-” in front of “action:” is missing:

actions:
        action: TEST_123
        title: Test

If I add the “-”:

{{ {"- action": action1_action, "title": action1_title} }}

The result is:

actions:
        '- action': TEST_123
        title: Test

I repeat, you are not trying to build YAML, but the data structure (JSON, if you like) that the YAML is representing.

Perhaps it should have been:

  actions: >
    {% if action1_title is defined and action1_action is defined %}
      {{ [{"action": action1_action, "title": action1_title}] }}
    {% elif action2_title is defined and action2_action is defined %}
      {{ [{"action": action2_action, "title": action2_title}] }}
    {% endif %}

Awesome! Thank you!

I modified your code, because I want action1 OR action1+action2:

actions: |
      {% if action1_title is defined and action1_action is defined and action2_title is not defined and action2_action is not defined%}
      {{ [{"action": action1_action, "title": action1_title}] }}
      {% endif %}
      {% if action1_title is defined and action1_action is defined and action2_title is defined and action2_action is defined %}
      {{ [{"action": action1_action, "title": action1_title},{"action": action2_action, "title": action2_title}] }}
      {% endif %}
1 Like