command_on_template: >
{% if brightness is defined %}
{{ "BRI" + brightness }}
{% else %}
{% if effect is defined %}
{{ "EFF" + effect }}
{% else %}
{% if state is defined %}
{{ "P_ON" }}
{% endif %}
{% endif %}
{% endif %}
The plus operator (+) is normally used to add numbers (arithmetic addition). However, if all items are strings then the plus operator will combine them (string concatenation). If one of the items is a string ("BRI") and the other is a number (45) then the plus operator will fail.
The tilde operator (~) is used to combine strings (string concatenation). If one of the items is not a string but a number, it will convert the number to a string.
command_on_template: >
{% if brightness is defined %}
{{ "BRI" ~ brightness }}
{% else %}
{% if effect is defined %}
{{ "EFF" ~ effect }}
{% else %}
{% if state is defined %}
{{ "P_ON" }}
{% endif %}
{% endif %}
{% endif %}