Distinct Variables within a template

I’ve been trying to use templates to implement some watering control - I want ESPHome to do this so when HA reboots - the solenoid will still turn off
I have five solenoids so am using templates , I am now trying to define global variables , a separate for each inclusion of the template
Is this possible ?

I tried with this code in the template


globals:
  - id: secondsleft$(sectionid)
    type: int
    restore_value: no
    initial_value: '0'

I get this error

Failed config

globals: [source /config/esphome/.solenoidboardwithparams.yaml:15]

IDs must only consist of upper/lowercase characters, the underscorecharacter and numbers. The character '$' cannot be used.
id: secondsleft$(sectionid)
type: int
restore_value: False
initial_value: 0

The include code is


packages:
  Orchard1: !include
    file: .solenoidboardwithparams.yaml
    vars:
      solenoidname : 'orchard1'
      solenoidpin : 15
      runtime : 600
      sectionid: 1

What I’m trying to achieve is a single ESP32 with 5 solenoids - each solenoid is independent . I want one button for each solenoid on Home Assistant interface (Lancelot?) - when I click the button it means the solenoid will be turned on for 5 minutes , if I click it twice I want it to turn on for 10 minutes
I also want a countdown of how long is left

Full template file


api:
  services:
    - service: openvalvefor1min${solenoidname}
      then:
        - script.execute: Watertimer${solenoidname}

button:
  - platform: template
    name: "RunWaterTimer ${solenoidname}"
    id: my_button${solenoidname}
    on_press:
    - script.execute: Watertimer${solenoidname}

globals:
  - id: secondsleft$(sectionid)
    type: int
    restore_value: no
    initial_value: '0'
  # Example for global string variable
  - id: my_global_string(sectionid)
    type: std::string
    restore_value: no
    max_restore_data_length: 24
    initial_value: '"Global value is"'


switch:
  - platform: gpio
    name:  "Water Solenoid ${solenoidname}"
    pin: GPIO${solenoidpin}
    id: WaterSolenoid${solenoidname}
    inverted: true

esphome:
    on_boot:
        priority: -100.0 # Optional: Lower priority runs earlier in the boot process
        then:
        - logger.log: "Device is booting, running startup script." # Optional logging
        - script.execute: boot_script # Call the script defined below

script:
  - id: boot_script
    mode: single
    then:
        while:
            condition:
                true
            then:
                if:
                    condition:
                        lambda: 'return id(secondsleft$(sectionid)) > 1;'
                    then:
                        logger.log: "The sensor value is below 30!"
                        light.turn_on: my_light
                        lambda: |-
                            id(secondsleft$(sectionid)) -= 1;
                    else:
                    - switch.turn_of: my_switch
            delay: 1s
      
      
  - id: Watertimer${solenoidname}
    mode: queued # allow the script to be extended
    then:
    - while:
      condition:
        switch.is_on: ventilator
      then:
      - logger.log: "Still executing"
      - delay: 1miin
      - switch.turn_on: podavac
      - delay: 1 min  # timer length
      - switch.turn_off: podavac
    - switch.turn_on: WaterSolenoid${solenoidname}
    - delay: ${runtime}sec
    - switch.turn_off: WaterSolenoid${solenoidname}

dumb - AGAIN - must look at validate output
${solenoidname} use { not (