Template sensor with defined variable (one time)

Hi.
This code works good

  - platform: template
    sensors:
      irrigaz_cycle1_running:
        friendly_name: "Zona 1"
        value_template: >  
          {% set ns = namespace() %}
          {% if is_state('input_boolean.irrigaz_test_mode', 'on') %}
            {% set ns.elettrovalvola1 = 'input_boolean.irrigaz_fake1' %}
          {% else %}
            {% set ns.elettrovalvola1 = 'switch.sonoff_xxx' %}
          {% endif %}                               
            
          {% if (is_state(ns.elettrovalvola1, 'on')) and (is_state('input_boolean.irrigaz_test_mode', 'on'))  %}
            acceso [test mode]
          {% elif (is_state(ns.elettrovalvola1, 'off')) and (is_state('input_boolean.irrigaz_test_mode', 'on'))  %}
            spento [test mode]
          {% elif (is_state(ns.elettrovalvola1, 'on')) and (is_state('input_boolean.irrigaz_test_mode', 'off'))  %}
            acceso
          {% elif (is_state(ns.elettrovalvola1, 'off')) and (is_state('input_boolean.irrigaz_test_mode', 'off'))  %}
            spento            
          {% endif %}                     

        icon_template: >
        
          {% set ns = namespace() %}
          {% if is_state('input_boolean.irrigaz_test_mode', 'on') %}
            {% set ns.elettrovalvola1 = 'input_boolean.irrigaz_fake1' %}
          {% else %}
            {% set ns.elettrovalvola1 = 'switch.sonoff_xxx' %}
          {% endif %}                     
        
          {% if is_state(ns.elettrovalvola1, 'on') %}
            mdi:water-pump
          {% else %}
            mdi:water-pump-off
          {% endif %}          

but I need to re-use variable “elettrovalvola1” more time in this file yaml.
Is there a way to reuse is without reporting everytime this code?

          {% set ns = namespace() %}
          {% if is_state('input_boolean.irrigaz_test_mode', 'on') %}
            {% set ns.elettrovalvola1 = 'input_boolean.irrigaz_fake1' %}
          {% else %}
            {% set ns.elettrovalvola1 = 'switch.sonoff_10004c0ee0_1' %}
          {% endif %}                     

Thanks all
MC

You can refer to this in a template to re-use state/attributes

link ?
tks