Using template for MQTT switch payload

I have a switch (technically a sprinkler valve) that I run over MQTT. I have a helper called input_number.how_long that I want to use to set the time for this switch. In my configuration.yml file I have this entry:

switch:
  - platform: mqtt
    unique_id: "sprinkler0"
    name: "Front Garden Sprinkler"
    state_topic: "outside/lawn/status"
    command_topic: "outside/lawn/action"
    payload_on: >
        {zone: 0, time: {{ states('input_number.how_long') | multiply(60) }} }
    payload_off: "{zone: 0, time: 0}"
    state_on: "Zone 0 added"
    state_off: "Zone 0 off"
    qos: 0  

I have tested this in the Template section of Developer Tools, and it produces a number exactly as I hoped. When I use the switch, the MQTT payload is not parsed into a number, but comes through directly with the template text.

Is this possible to do in a payload?

From what I have found about templating, it seems I should be able to stick them in almost wherever.

I think the problem is the switch cant do the templateing on the payload

so

what about change it to a switch template and then using a script to pass a data

some like

im just doing this off top of head need some testing and beater logic

and this would be my work flow getting the problem sorted

  - platform: template
    switches:
      garden_sprinkler: 
        friendly_name:  'Garden Sprinkler'
        value_template: '{{ need some logic to check the state }}'
        turn_on:
          service: script.turn_on
          data:
            entity_id: script.garden_sprinkler_on
        turn_off:
          service: script.turn_on
          data:
            entity_id: script.garden_sprinkler_off

thats the switch bit done know the payload bit (script)

garden_sprinkler_on:
  alias: Garden Sprinkler on
  sequence:
  - service: mqtt.publish
    data_template:
      topic: outside/lawn/action
      payload: "{zone: 0, time: {{ states('input_number.how_long') | multiply(60) }} }"
garden_sprinkler_off:
  alias: Garden Sprinkler off
  sequence:
  - service: mqtt.publish
    data_template:
      topic: outside/lawn/action
      payload: "{zone: 0, time: 0}"

still can’t think of the logic of the check for if it ON

and yes could of used choose but that would be mark2 and once I get my head around it

thinking …

EDIT: ADDED
what about a input_boolean

  garden_sprinkler:
    name: Garden Sprinkler
    initial: off

then we can add the

  - data:
      entity_id: input_boolean.garden_sprinkler
    service: input_boolean.turn_on

to the on script

and

  - data:
      entity_id: input_boolean.garden_sprinkler
    service: input_boolean.turn_off

to the off script

now we can change the

value_template: '{{ need some logic to check the state }}'

to

value_template: input_boolean.garden_sprinkler

thats my 5 cents

updated script

garden_sprinkler_on:
  alias: Garden Sprinkler on
  sequence:
  - service: mqtt.publish
    data_template:
      topic: outside/lawn/action
      payload: "{zone: 0, time: {{ states('input_number.how_long') | multiply(60) }} }"
  - data: 
      entity_id: input_boolean.garden_sprinkler
    service: input_boolean.turn_on
garden_sprinkler_off:
  alias: Garden Sprinkler off
  sequence:
  - service: mqtt.publish
    data_template:
      topic: outside/lawn/action
      payload: "{zone: 0, time: 0}"
  - data:
      entity_id: input_boolean.garden_sprinkler
    service: input_boolean.turn_off

well after all that and reading your post again and copy pasting some of my scripts i see you missing a pair or quotes

    payload_on: >
        "{zone: 0, time: {{ states('input_number.how_long') | multiply(60) }} }"

You can’t use templates in the payload_on option.

From the documentation for MQTT Switch:

payload_on string (optional, default: ON)

The payload that represents on state. If specified, will be used for both comparing to the value in the state_topic (see value_template and state_on for details) and sending as on command to the command_topic.

It’s a string value so any template you put in it will not be evaluated but used literally.

Use myle’s suggestion where the script employs the mqtt.publish service to publish a payload (which can be templated).

Thanks for the help!
This is working now, here is what I have:

switch:
  - platform: template
    switches:
      sprinkler0:
        friendly_name: "Front Garden Sprinkler"
        value_template: '{{ states("input_boolean.sprinkler0") }}'
        turn_on:
          service: script.activate_sprinkler
          data:
            zone: 0
        turn_off:
          service: mqtt.publish
          data:
            topic: "outside/lawn/action"
            payload: "{zone: 0, time: 0}"

I’m using the vairable so I only have 1 script I can use with all of my zones. This is my script:

activate_sprinkler:
  fields:
    zone:
      name: Zone
      description: Which zone to activate
      default: 0
      example: 0
      required: true
      selector:
        number:
          min: 0
          max: 8
          step: 1
          mode: slider
  sequence:
  - service: mqtt.publish
    data:
      topic: outside/lawn/action
      payload_template: '{ zone: {{ zone }}, time: {{ states("input_number.how_long") | multiply(1) | int }}  }'
  mode: queued
  alias: Activate Sprinkler
  icon: hass:water
  max: 10

There are probably better ways to do this, but at least it is working now. Thank you for your help!