Issue: Different Mqtt topic based on several sensor state

Hi all,
I’m trying to create an automation that push an Mqtt topic based on the state of various sensors.
After several attempt, this is how my code looks right now

- service: mqtt.publish
      data_template:
        topic: >
          {%- set sensors = ["binary_sensor.soil_1", "binary_sensor.soil_2", "binary_sensor.soil_3", "binary_sensor.soil_4", "binary_sensor.soil_5"] -%}
          {%- for sensor in sensors if is_state(sensor, 'on') -%}
            {% if loop.length < 1-%}
              {% if loop.first-%}
                wemosgarden/soilsensorled/green
              {% endif %}
            {% elif loop.length >= 3-%}
              {% if loop.first-%}
                wemosgarden/soilsensorled/red
              {% endif %}
            {% else %}
              wemosgarden/soilsensorled/yellow
            {% endif %}
          {%- endfor -%}

If I try this setup in the template editor it works, and the automation load without error.
But when I trigger the automation the topic isn’t published.
What am I doing wrong?

thanks in advance for any hint

don’t you need a payload?

No I just need to publish the topic.
Then the message is read by an esp32 board to activate different GPIO output.

However I’ve tried using payload_template like this:

- service: mqtt.publish
      data_template:
        topic: wemosgarden/soilsensorled/
        payload_template: >-
          {%- set sensors = ["binary_sensor.soil_1", "binary_sensor.soil_2", "binary_sensor.soil_3", "binary_sensor.soil_4", "binary_sensor.soil_5"] -%}
          {%- for sensor in sensors if is_state(sensor, 'on') -%}
            {% if loop.length < 1-%}
              {% if loop.first-%}
                green
              {% endif %}
          .....

this way the topic is published but not the payload.
So the problem is in the template, but it give no errors and I can’t figure out what I’m doing wrong.