Ok, I think I got you. With your current setup, you’re not going to get the proper data structure, as it’s going to return all your key/value pairs as one big string. What you need to do here is create your empty dictionary, then add the key/value pairs, based on your logic, then return the resulting dictionary at the end.
Something like this…
kodi_control:
sequence:
- service: kodi.call_method
target:
entity_id: '{{ kodi_entity }}'
data: >-
{% set my_dict = {} %}
{% set x=my_dict.__setitem__("method", {{ kodi_method }}) %}
{% if kodi_playerid is defined %}
{% set x=my_dict.__setitem__("playerid", {{ kodi_playerid }}) %}
{% endif %}
{% if kodi_action is defined %}
{% set x=my_dict.__setitem__("action", {{ kodi_action }}) %}
{% endif %}
{% if kodi_value is defined %}
{% set x=my_dict.__setitem__("value", {{ kodi_value }}) %}
{% endif %}
{% if kodi_window is defined %}
{% set x=my_dict.__setitem__("window", {{ kodi_window }}) %}
{% endif %}
{% if kodi_parameters is defined %}
{% set x=my_dict.__setitem__("parameters", {{ [ kodi_parameters ] }}) %}
{% endif %}
{{ my_dict }}
I looked at all the comments after I posted this and saw your full code. The suggestion I put there isn’t as nice as default(omit), but still probably much better than trying to go with this conditional route, which will probably increase code instead of reducing it. I think that should solve your problem. I didn’t really think about building the entire data payload on the fly until seeing your post, but it makes sense as a solution.