Error:
Message malformed: template value should be a string dictionary value @ data[‘sequence’][0][‘data’]
I was thinking I’d use an if-then block for checking {{ action is defined }}, but I was hoping there might be a cleaner way of doing it. Then, if I need to modify the notify action then I’d only have to change it once.
You can only template the value of a key: value pair. You cannot template the key part.
actions:
- action: >
{% if action defined %}
{{ action }}
{% else %}
?? You must put something here
{% endif %}
title: >
{% if action defined %}
{{ action_title }}
{% else %}
?? You must put something here
{% endif %}
uri: >
{% if action defined %}
{{ action_uri }}
{% else %}
?? You must put something here
{% endif %}
You can also template the entire data section. In this case the suggestion from TheFes is the better/easier approach. But for scenarios where you can’t supply an empty list (which are more common), you can template the entire data section and simply omit the key that you don’t want.
action: notify.mobile
data:
title: "{{ title }}"
message: "{{ message }}"
data: >
{% set ns = namespace(data_dict = dict(
tag=tag, car_ui=car_ui, notification_icon=icon)) %}
{% if action is defined %}
{% set actions_dict = [dict(action=action, title=action_title, uri=action_uri)] %}
{% set ns.data_dict = dict(actions=actions_dict, **ns.data_dict) %}
{% endif %}
{{ ns.data_dict }}