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 %}
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 %}
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 %}