Unable to use if/else template for scripts with button call

Hi, normally my persistance and googling would have sorted this out by now but for the life of me I just can’t seem to crack it and it appears so simple. Basically I have a button that when pressed, I want to execute a script based on the state of a switch. So if the switch is off, run the script that turns it on (and then off after 30 mins). If the switch is on, then turn it off. Just can’t see where I’m going wrong. The error I keep getting is:

Failed to call service script/turn_on. sequence item 0: expected str instance, Optional found

Here is my code:

type: 'custom:button-card'
entity: switch.bathroom_relay_switch_2
icon: 'mdi:fan-off'
name: Bathroom Fan
tap_action:
  action: call-service
  service: script.turn_on
  data_template:
    entity_id: |
      {% if is_state('switch.bathroom_relay_switch_2', 'off') %}
        script.run_bathroom_fan_for_30_mins
      {% else %} 
        script.turnoff_bathroom_fan 
      {% endif %}
show_state: true
state:
  - value: 'on'
    color: 'rgb(0, 140, 255)'
    icon: 'mdi:fan'
    spin: true
styles:
  card:
    - font-size: 14px

You can’t template in lovelace. Make a script and template in the script.

Ah that would be it then! Thank you for clearing that up, I will look at an alternative solution as suggested.